java实现平衡二叉树的建立
2013-08-09 21:06
435 查看
有关平衡二叉树的概念以及平衡二叉树的旋转操作:平衡二叉树
下边看java代码的实现。其实原来人家用c#已经实现过了,我只是改成java版的(说实话,刚开始看他写的代码的时候,我以为是java)class Node { int data;//数据 int BF;//平衡因子 Node leftChild;//左娃娃 Node rightChild;//右娃娃 public Node(int data) { super(); this.data = data; } } public class 平衡二叉树的建立 { private static Node head;//表示头节点 private static Node[] path = new Node[32];//记录访问路径上的节点 private static int p ;//表示当前访问到的节点在path中的索引 public static boolean addNode(int value){ if(head == null){ head = new Node(value); //不要忘了平衡因子的修改 head.BF = 0;//刚开始head的平衡因子为0 return true; } p = 0; Node prev = null;//上一次访问到的节点 Node current = head;//当前访问的节点 //寻找插入位置 while(current != null){ path[p++] = current; if(current.data == value){ return false;//若value与当前数据相同,则返回false,无需插入 } prev = current; //当前值小于左孩子,继续访问左孩子 //否则访问右孩子 current = (value < prev.data) ? prev.leftChild : prev.rightChild; } current = new Node(value); //不要忘了平衡因子的修改current.BF = 0 current.BF = 0; if(value < prev.data){ prev.leftChild = current; }else{ prev.rightChild = current; } path[p] = current;//将当前节点插入到path中的最后 int bf = 0; //修改插入节点至根节点路径上的各个节点的平衡因子 while( p > 0){ bf = (value < path[p-1].data) ? 1 : -1; path[--p].BF += bf; bf = path[p].BF;//取出当前的平衡因子 //若当前平衡因子为0,则表示该子树已平衡,无需递归了 //若为2或者-2,表示要对改子树进行旋转操作 if( bf == 0){ return true; } else if(bf == 2 || bf == -2){ 旋转(bf); return true; } } return true; } //对树的旋转操作 private static boolean 旋转(int bf) { Node root = path[p]; Node newRoot = null; boolean tallChange = true; if(bf == 2){ if(root.leftChild.BF == 1){//进行LL旋转操作() newRoot = LL(root); }else if(root.leftChild.BF == -1){//进行LR旋转操作 newRoot = LR(root); } }else if(bf == -2){ if(root.rightChild.BF == -1){//进行RR旋转操作 newRoot = RR(root); }else if(root.rightChild.BF == 1){ newRoot = RL(root); } } //更改新的子树 if(p > 0){ if(root.data < path[p-1].data){ path[p-1].leftChild = newRoot; }else{ path[p-1].rightChild = newRoot; } }else{ head = newRoot; } return tallChange; } private static Node RL(Node root) { Node rootNext = root.rightChild; Node newRoot = rootNext.leftChild; root.rightChild = newRoot.leftChild; rootNext.leftChild = newRoot.rightChild; newRoot.leftChild = root; newRoot.rightChild = rootNext; switch (newRoot.BF) { case 1: root.BF = 0; rootNext.BF = -1; break; case -1: root.BF = 1; rootNext.BF=0; break; } return newRoot; } private static Node RR(Node root) {//RR型旋转操作 Node rootNext = root.rightChild; root.rightChild = rootNext.leftChild; rootNext.leftChild = root; //不要忘了平衡因子的修改 if(rootNext.BF == -1){ rootNext.BF = 0; root.BF = 0; } return rootNext; } private static Node LR(Node root) { Node rootNext = root.leftChild; Node newRoot = rootNext.rightChild; root.leftChild = newRoot.rightChild; rootNext.rightChild = newRoot.leftChild; newRoot.leftChild = rootNext; newRoot.rightChild = root; //改变平衡因子 switch (newRoot.BF) { case 1: root.BF = -1; rootNext.BF = 0; break; case -1: root.BF = 0; rootNext.BF = 1; break; } newRoot.BF = 0; return newRoot; } private static Node LL(Node root) {//LL型旋转操作 Node rootNext = root.leftChild; root.leftChild = rootNext.rightChild; rootNext.rightChild = root; //旋转后不要忘了平衡因子的修改 if(rootNext.BF == 1){ rootNext.BF = 0; root.BF = 0; } return rootNext; } public static void main(String[] args) { addNode(1); addNode(2); addNode(3); addNode(4); addNode(5); addNode(6); addNode(7); print(head); } private static void print(Node root) {//按照先序遍历 肯定是顺序输出,即为1,2,3,4,5,6,7 if(root.leftChild !=null){ print(root.leftChild); } System.out.println(root.data); if(root.rightChild!=null){ print(root.rightChild); } } }
相关文章推荐
- 平衡二叉树平衡二叉树(AVL)--查找、删除、插入(Java实现)
- java实现二叉排序树的建立,查找,插入,删除
- java 平衡二叉树的实现
- 基于rsync同步算法的文件同步系统JAVA实现(三)——校验码哈希表的建立
- 二叉排序树的建立和先序遍历和层次遍历问题之java实现
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 数据结构与算法分析笔记与总结(java实现)--二叉树13:平衡二叉树
- 二叉树建立以及先序、中序、后序、层次遍历(JAVA 实现)
- Java 实现二叉树的建立以及前、中、后序遍利
- java实现 二叉树的深度&判断二叉树是否是平衡二叉树
- 树——平衡二叉树插入和查找的JAVA实现
- java --平衡二叉树实现
- java实现二叉树的建立及遍历方法
- 第三章:通话连接的作用以及建立过程之三TelephonyConnectionService.java与RIL.java交互实现通话底层拨号功能
- 【LeetCode-面试算法经典-Java实现】【110-Balanced Binary Tree(平衡二叉树)】
- Java实现-验证平衡二叉树
- 平衡二叉树java实现
- 用JAVA实现的类FileOperate,包括文件建立,查询,删除等。。。(继续更新中)
- 编译原理:递归向下分析程序建立语法分析树的Java实现(一)
- 平衡二叉树 JAVA实现 亲测可用