数的节点
1 package com.ydp.tree.AVLTree; 2 public class Node{ 3 private int data = 0; 4 private Node lchild = null; 5 private Node rchild = null; 6 private Node parent = null; 7 8 public Node(){}; 9 public Node(int data){ 10 this.data = data; 11 } 12 public Node(int data,Node parent){ 13 this.data = data; 14 this.parent = parent; 15 } 16 17 public boolean hasLChild(){ 18 return lchild != null; 19 } 20 21 public boolean hasRChild(){ 22 return rchild != null; 23 } 24 //当前节点树的深度 25 public int getTreeDepth(){ 26 return getTreeDepth(this); 27 } 28 //指定节点树的深度 29 protected int getTreeDepth(Node node){ 30 int depth = 0; 31 if(node != null){ 32 int ldepth = getTreeDepth(node.getLchild()); 33 int rdepth = getTreeDepth(node.getRchild()); 34 depth =1 + (ldepth>rdepth?ldepth:rdepth); 35 } 36 return depth; 37 } 38 39 public int getData() { 40 return data; 41 } 42 public void setData(int data) { 43 this.data = data; 44 } 45 public Node getLchild() { 46 return lchild; 47 } 48 public void setLchild(Node lchild) { 49 this.lchild = lchild; 50 } 51 public Node getRchild() { 52 return rchild; 53 } 54 public void setRchild(Node rchild) { 55 this.rchild = rchild; 56 } 57 public Node getParent() { 58 return parent; 59 } 60 public void setParent(Node parent) { 61 this.parent = parent; 62 } 63 64 public boolean isLChild(){ 65 66 return this.parent.hasLChild()?this.parent.getLchild().getData()==this.data:false; 67 } 68 69 public boolean equals(Node node){ 70 return this.data == node.getData(); 71 } 72 73 }
平衡二叉树的实现
1 package com.ydp.tree.AVLTree; 2 3 import java.util.Stack; 4 5 6 7 8 9 public class AVLTree { 10 11 Node root = null; 12 13 14 public static void main(String[] args) { 15 AVLTree tree = new AVLTree(); 16 tree.insert(50); 17 tree.insert(40); 18 tree.insert(30); 19 tree.print(); 20 21 tree = new AVLTree(); 22 tree.insert(50); 23 tree.insert(40); 24 tree.insert(60); 25 tree.insert(30); 26 tree.insert(45); 27 tree.insert(20); 28 tree.print(); 29 30 tree = new AVLTree(); 31 tree.insert(50); 32 tree.insert(60); 33 tree.insert(70); 34 tree.print(); 35 36 tree = new AVLTree(); 37 tree.insert(50); 38 tree.insert(40); 39 tree.insert(60); 40 tree.insert(55); 41 tree.insert(70); 42 tree.insert(80); 43 tree.print(); 44 45 tree = new AVLTree(); 46 tree.insert(50); 47 tree.insert(40); 48 tree.insert(45); 49 tree.print(); 50 51 tree = new AVLTree(); 52 tree.insert(50); 53 tree.insert(40); 54 tree.insert(60); 55 tree.insert(30); 56 tree.insert(45); 57 tree.insert(47); 58 tree.print(); 59 60 tree = new AVLTree(); 61 tree.insert(50); 62 tree.insert(60); 63 tree.insert(55); 64 tree.print(); 65 66 tree = new AVLTree(); 67 tree.insert(50); 68 tree.insert(40); 69 tree.insert(55); 70 tree.insert(53); 71 tree.insert(60); 72 tree.insert(70); 73 tree.print(); 74 75 76 77 } 78 79 public void print(){ 80 System.out.println("树的深度:"+this.getRoot().getTreeDepth()); 81 this.preOrder(); 82 System.out.println(); 83 this.midOrder(); 84 System.out.println(" "); 85 } 86 87 //插入节点数据 88 public void insert(int data){ 89 if(this.root == null){ 90 this.root = new Node(data); 91 }else{ 92 insert(data,this.root); 93 } 94 } 95 //递归插入,将数据插入到合适的位置 96 protected void insert(int data,Node node){ 97 if(data>node.getData()){ 98 if(node.hasRChild()){ 99 insert(data,node.getRchild()); 100 }else{ 101 node.setRchild(new Node(data,node)); 102 } 103 if(getTreeDepth(node.getRchild())-getTreeDepth(node.getLchild())==2){ 104 if(data>node.getRchild().getData()){ 105 leftRotate(node); 106 107 }else{ 108 rightLeftRotate(node); 109 } 110 } 111 112 }else if(data<node.getData()){ 113 if(node.hasLChild()){ 114 insert(data,node.getLchild()); 115 }else{ 116 node.setLchild(new Node(data,node)); 117 } 118 if(getTreeDepth(node.getLchild())-getTreeDepth(node.getRchild())==2){ 119 if(data<node.getLchild().getData()){ 120 rightRotate(node); 121 }else{ 122 leftRightRotate(node); 123 124 } 125 } 126 127 } 128 } 129 130 public Node getRoot() { 131 return root; 132 } 133 134 public void setRoot(Node root) { 135 this.root = root; 136 } 137 138 //顺时针旋转 139 public void rightRotate(Node node){ 140 System.out.println("顺时针:"+node.getData()); 141 Node tmp = node.getLchild(); 142 if(node.getParent() == null){ 143 this.root=node.getLchild(); 144 }else{ 145 if(node.isLChild()){ 146 node.getParent().setLchild(tmp); 147 }else{ 148 node.getParent().setRchild(tmp); 149 } 150 } 151 tmp.setParent(node.getParent()); 152 node.setLchild(tmp.getRchild()); 153 node.setParent(tmp); 154 tmp.setRchild(node); 155 } 156 //先顺后逆时针 157 public void rightLeftRotate(Node node){ 158 System.out.println("先顺后逆时针:"+node.getData()); 159 rightRotate(node.getRchild()); 160 leftRotate(node); 161 } 162 163 //逆时针 164 public void leftRotate(Node node){ 165 System.out.println("逆时针:"+node.getData()); 166 167 Node tmp = node.getRchild(); 168 if(node.getParent() == null){ 169 this.root=node.getRchild(); 170 }else{ 171 if(node.isLChild()){ 172 node.getParent().setLchild(tmp); 173 }else{ 174 node.getParent().setRchild(tmp); 175 } 176 177 } 178 tmp.setParent(node.getParent()); 179 node.setRchild(tmp.getLchild()); 180 node.setParent(tmp); 181 tmp.setLchild(node); 182 183 } 184 185 //逆时针 186 public void leftRightRotate(Node node){ 187 System.out.println("先逆后顺时针:"+node.getData()); 188 leftRotate(node.getLchild()); 189 rightRotate(node); 190 } 191 192 193 194 //先序遍历 195 public void preOrder(){ 196 Stack<Node> stack = new Stack<Node>(); 197 Node node = root; 198 while(node != null || !stack.empty()){ 199 while(node != null){ 200 System.out.print(node.getData()+" "); 201 stack.push(node); 202 node = node.getLchild(); 203 } 204 node = stack.pop(); 205 node = node.getRchild(); 206 } 207 } 208 209 //中序遍历 210 public void midOrder(){ 211 Stack<Node> stack = new Stack<Node>(); 212 Node node = root; 213 while(node != null || !stack.empty()){ 214 while(node != null){ 215 stack.push(node); 216 node = node.getLchild(); 217 } 218 node = stack.pop(); 219 System.out.print(node.getData()+" "); 220 node = node.getRchild(); 221 } 222 } 223 224 protected int getTreeDepth(Node node){ 225 int depth = 0; 226 if(node != null){ 227 int ldepth = getTreeDepth(node.getLchild()); 228 int rdepth = getTreeDepth(node.getRchild()); 229 depth =1 + (ldepth>rdepth?ldepth:rdepth); 230 } 231 return depth; 232 } 233 }