简单二叉树的理解和实现
首先,什么是数据结构?数据结构是数据的逻辑结构,而不是物理结构(数据的真是存储结构),逻辑结构可以帮助我们更好的去处理大批量的数据。接触过数据结构的人都知道树这种结构。最常见的就是二叉树,主要是用来进行数据的查找,且时间的平均复杂度为log(n),原理有点类似二分查找。二叉树的提出和对象比较Comparable是分不开的。二叉树包含两个子结点的引用,左子树left和右子树right,当要存入数据对象的时候,比当前结点对象值小的或相等的放入left,否则放入right。数据对象需继承Comparable接口。
1 interface IBinaryTree<T extends Comparable<T>> { 2 void add(T data); 3 4 int getCount(); 5 6 Object[] getList(); 7 8 Object[] getList2(); 9 10 void add2(T data); 11 12 } 13 14 class BinaryTreeImp<T extends Comparable<T>> implements IBinaryTree<T> { 15 private Node root; 16 private int count; 17 private Object[] list; 18 private int foot; 19 private Node cacheNode; 20 21 @Override 22 public void add(T data) { 23 if (data == null) { 24 return; 25 } 26 Node newNode = new Node(data); 27 if (this.root == null) { 28 this.root = newNode; 29 } else { 30 this.root.addNode(newNode); 31 } 32 this.count++; 33 } 34 35 @Override 36 public int getCount() { 37 return this.count; 38 } 39 40 //中序遍历 41 @Override 42 public Object[] getList() { 43 if (this.root == null) { 44 return null; 45 } 46 list = new Object[this.count]; 47 this.foot = 0; 48 this.root.toList(); 49 return list; 50 } 51 52 //中序遍历方法二 53 @Override 54 public Object[] getList2() { 55 if (this.root == null) { 56 return null; 57 } 58 list = new Object[this.count]; 59 this.foot = 0; 60 getAll(this.root); 61 return list; 62 } 63 64 //递归核心 65 public void getAll(Node node) { 66 if (node == null) 67 return; 68 getAll(node.left); 69 list[foot++] = node.data; 70 //System.out.println(node.data); 71 getAll(node.right); 72 } 73 74 //增加数据的方法二 75 @Override 76 public void add2(T data) { 77 if (data == null) { 78 return; 79 } 80 Node newNode = new Node(data); 81 this.cacheNode = this.root; 82 if (this.root == null) { 83 this.root = newNode; 84 } else { 85 addNode2(newNode); 86 } 87 this.count++; 88 } 89 90 91 92 private void addNode2(Node newNode) { 93 if (cacheNode.data.compareTo(newNode.data) <= 0) { 94 if (cacheNode.left == null) { 95 cacheNode.left = newNode; 96 } else { 97 cacheNode = cacheNode.left; 98 addNode2(newNode); 99 } 100 } else { 101 if (cacheNode.right == null) { 102 cacheNode.right = newNode; 103 } else { 104 cacheNode = cacheNode.right; 105 addNode2(newNode); 106 } 107 } 108 } 109 110 111 private class Node { 112 private Node left; 113 private Node right; 114 private Node parent; 115 private T data; 116 117 public Node(T data) { 118 this.data = data; 119 } 120 121 public void addNode(Node newNode) { 122 if (this.data.compareTo(newNode.data) <= 0) { 123 if (this.left == null) { 124 this.left = newNode; 125 } else { 126 this.left.addNode(newNode); 127 } 128 } else { 129 if (this.right == null) { 130 this.right = newNode; 131 } else { 132 this.right.addNode(newNode); 133 } 134 } 135 } 136 137 public void toList() { 138 if (this.left != null) { 139 this.left.toList(); 140 } 141 list[foot++] = this.data; 142 //System.out.println(this.data); 143 if (this.right != null) { 144 this.right.toList(); 145 } 146 } 147 148 } 149 150 151 } 152 153 154 class Persion implements Comparable<Persion> { 155 private String name; 156 private int age; 157 158 public Persion(String name, int age) { 159 this.name = name; 160 this.age = age; 161 } 162 163 public String getName() { 164 return name; 165 } 166 167 public void setName(String name) { 168 this.name = name; 169 } 170 171 public int getAge() { 172 return age; 173 } 174 175 public void setAge(int age) { 176 this.age = age; 177 } 178 179 180 @Override 181 public int compareTo(Persion o) { 182 183 return o.age - this.age; 184 } 185 186 @Override 187 public String toString() { 188 return "Persion{" + 189 "name='" + name + '\'' + 190 ", age=" + age + 191 '}'; 192 } 193 }