java 队列和二叉树代码
来源:互联网 发布:注册淘宝也是支付宝 编辑:程序博客网 时间:2024/06/05 17:57
队列
public class MyQueue{private Object datas[];private int pushIndex;//入队的下标private int popIndex; //出队的下标private int counts;//记录数据的个数public MyQueue(int size){datas = new Object[size];}public MyQueue(){this(10);}public boolean isEmpty(){return counts == 0;}public String toString(){StringBuilder mess = new StringBuilder();for (int i = popIndex; i <= counts; i++) {mess.append(datas[i % datas.length] + ",");}return mess + "";}public boolean isFull(){return counts == datas.length;}/** * 入队列 * @param data */public void push(Object data) {if (isFull()) {return;}datas[pushIndex++ % datas.length] = data;counts++;}public Object popup(){Object data = datas[popIndex++ % datas.length];counts--;return data;}}
测试
//初始化队列MyQueue queue = new MyQueue(6);//六个人入队for (int i = 0;i < 6; i++) {queue.push(i + 1);}//数数int counts = 0;//计数器while (!queue.isEmpty()) {Object d = queue.popup();counts++;//判断if (counts % 3 == 0) {System.out.println(d);} else {queue.push(d);//再放进队列}}
二叉树
public class MyTree{private Noderoot;// 根节点private class Node{Nodeparrent;// 父节点Nodeleft;// 左儿子Noderight;// 右儿子Objectdata;public Node(Object data) {this.data = data;}}/** * @param data * 传递的数据 * @return 父节点的值 */private Node findParrent(Object data, Node currentNode) {// 从根节点找Node temp = currentNode;Node parrent = currentNode;// 循环找while (temp != null) {parrent = temp;// 比较if (compare(data, temp.data)) {// data 大于 当前节点temp = temp.right;} else {// data 小于 当前节点temp = temp.left;}}return parrent;}public void update(Object oldData,Object newData){remove(oldData);add(newData);}/** * 添加数据 * * @param data * 要添加的数据 */public void add(Object data) {// 判断该数据是否存在if (contains(data))return;// 1.把数据放到节点中Node node = new Node(data);// 2.把节点链接到二叉树中// 是否有根节点if (root == null) {root = node;// 保存到根节点中} else {// 找位置,找父节点,比较父节点的值,小左边 大右边Node parrent = findParrent(data, root);// 设置新增节点的父节点node.parrent = parrent;// 比较if (compare(data, parrent.data)) {// 自己比父节点大parrent.right = node;} else {// 自己比父节点小parrent.left = node;}}}/** * @param data * @return 是否包含该数据 */public boolean contains(Object data) {return null != find(data);}private Node find(Object data) {Node temp = root;// 从根节点找while (temp != null) {// 判断数据if (temp.data.equals(data)&& temp.data.hashCode() == data.hashCode()) {// 找到数据break;} else if (compare(data, temp.data)) {// true data > temp// 从右边找temp = temp.right;} else {// false data < temp// 从坐标边找temp = temp.left;}}return temp;}public void remove(Object data) {// 1. 查找数据是否存在Node temp = find(data);// 2. 存在:找到数据节点if (temp != null) {// 存在// 3. 删除节点// 1. 根节点if (temp == root) {// 11 没有儿子if (temp.left == null && temp.right == null) {root = null;} else if (temp.right == null) {root = root.left;root.parrent = null;// 12 只有左儿子} else if (temp.left == null) {// 13 只有右儿子root = root.right;root.parrent = null;} else {// 14 两个儿子都有// 保留左儿子Node left = getLeft(temp);// left成为新的根节点root = left;left.parrent = null;}} else {// 2. 非根节点if (temp.left == null && temp.right == null) {// 21 没有儿子if (compare(temp.data, temp.parrent.data)) {//在父节点右边temp.parrent.right = null;} else {//在父节点左边temp.parrent.left = null;}} else if (temp.right == null) {// 22 只有左儿子if (compare(temp.data, temp.parrent.data)) {//在父节点右边temp.parrent.right = temp.left;temp.left.parrent = temp.parrent;} else {//在父节点左边temp.parrent.left = temp.left;temp.left.parrent = temp.parrent;}} else if (temp.left == null) {// 23 只有右儿子if (compare(temp.data, temp.parrent.data)) {//在父节点右边temp.parrent.right = temp.right;temp.right.parrent = temp.parrent;} else {//在父节点左边temp.parrent.left = temp.right;temp.right.parrent = temp.parrent;}} else {// 24 两个儿子都有Node left = getLeft(temp);//上面还有父节点(爷爷)if (compare(left.data, temp.parrent.data)) {//比爷爷节点大temp.parrent.right = left;left.parrent = temp.parrent;} else {//比爷爷节点小temp.parrent.left = left;left.parrent = temp.parrent;}}}}}/** * @param node * 要删除的节点 * @return 左儿子节点 */private Node getLeft(Node node) {// 保留左儿子Node left = node.left;// 处理右节点Node rightNewParrent = findParrent(node.right.data, left);rightNewParrent.right = node.right;// 把删除节点的右节点放到删除节点的左儿子最右边node.right.parrent = rightNewParrent;return left;}/** * @param o1 * 第一个值 * @param o2 * 第二个值 * @return 如果o1 大于 o2 返回true 否则false */public boolean compare(Object o1, Object o2) {boolean res = false;// 判断o1 有没有实现比较器if (o1 instanceof Comparable) {Comparable c1 = (Comparable) o1;Comparable c2 = (Comparable) o2;if (c1.compareTo(c2) > 0) {res = true;} else {// 默认值就是false}} else {// 传递的对象没有比较器res = o1.toString().compareTo(o2.toString()) > 0 ? true : false;}return res;}// 递归打印public void print() {print(root);}public void print(Node node) {if (node == null) {return;} else {// 遍历 中序print(node.left);System.out.println(node.data + ",");print(node.right);}}}
测试
MyTree trees = new MyTree();int[] datas = {55,33,44,88,66,99};for (int d : datas) {trees.add(d);}trees.print();System.out.println();//测试删除trees.update(33,77);trees.print();
0 0
- java 队列和二叉树代码
- 二叉树、栈、队列、链表的Java代码实现
- 用java实现二叉查找树、堆和优先队列
- Java二叉树代码
- 二叉树遍历之栈和队列
- 栈,队列和二叉树的实现
- 二叉树遍历(java代码)
- 二叉树的序列化和反序列化java借助队列实现
- C语言二叉树与队列实现基础代码
- 优先队列和二叉堆
- 二叉堆和优先队列
- 队列,栈,二叉树
- 【数据结构 队列 + 二叉树】
- 队列和堆栈实现二叉树的遍历
- 基于栈和队列实现二叉树的遍历
- 栈和队列在遍历二叉树中的使用
- 【NOIp复习】数据结构之栈、队列和二叉树
- Java代码实现二叉搜索树-----插入
- Struts 关于validator的regex验证
- ios 导航栏,标签栏,工具栏和状态栏
- 在屏幕中实现自动输入文本效果
- 二分搜索法简单分析与总结(转)
- nyoj 1027 防守阵地 <思维>
- java 队列和二叉树代码
- 上传本地代码到github上
- cudaDeviceReset
- linux 操作系统中ping测试网络连通性命令的使用
- android面试专题(9)
- HOJ 1002题 题解
- 开通博客啦
- 【POJ2528】Mayor's Posters-线段树+离散化
- light oj 1008 Fibsieve`s Fantabulous Birthday