1212public class BinarySeachTree <T > extends BinaryTree <T > {
1313 private Comparator <T > comparator ;
1414
15- public BinarySeachTree (BinarySeachTree . BinaryTreeNode < T > root ) {
15+ public BinarySeachTree (T root ) {
1616 this (root , null );
1717 }
1818
19- public BinarySeachTree (BinarySeachTree . BinaryTreeNode < T > root , Comparator <T > comparator ) {
19+ public BinarySeachTree (T root , Comparator <T > comparator ) {
2020 super (root );
2121 this .comparator = comparator ;
2222 }
2323
24- private int compare (BinarySeachTree .BinaryTreeNode <T > a , BinarySeachTree .BinaryTreeNode <T > b ) {
24+ protected int compare (BinarySeachTree .BinaryTreeNode <T > a , BinarySeachTree .BinaryTreeNode <T > b ) {
2525 if (comparator != null ) {
2626 return comparator .compare (a .element , b .element );
2727 } else if (a .element instanceof Comparable && b .element instanceof Comparable ) {
2828 return ((Comparable ) a .element ).compareTo (b .element );
2929 } else {
3030 throw new RuntimeException ("can't compare " + a .element .getClass ());
3131 }
32+ }
3233
34+ protected int compare (T a , T b ) {
35+ if (comparator != null ) {
36+ return comparator .compare (a , b );
37+ } else if (a instanceof Comparable && b instanceof Comparable ) {
38+ return ((Comparable ) a ).compareTo (b );
39+ } else {
40+ throw new RuntimeException ("can't compare " + a .getClass () + " " + b .getClass ());
41+ }
3342 }
3443
3544 /**
@@ -38,8 +47,8 @@ private int compare(BinarySeachTree.BinaryTreeNode<T> a, BinarySeachTree.BinaryT
3847 * @param x
3948 * @return
4049 */
41- public boolean contains (BinaryTreeNode < T > x ) {
42- return contains (root , x );
50+ public boolean contains (T x ) {
51+ return contains (root , new BinaryTreeNode < T >( x ) );
4352 }
4453
4554 /**
@@ -49,8 +58,8 @@ public boolean contains(BinaryTreeNode<T> x) {
4958 * @param x
5059 * @return
5160 */
52- public boolean contains (BinaryTreeNode <T > cur , BinaryTreeNode <T > x ) {
53- if (x == null || cur == null ) {
61+ protected boolean contains (BinaryTreeNode <T > cur , BinaryTreeNode <T > x ) {
62+ if (x == null || cur == null || cur . hintcount < 0 ) {
5463 return false ;
5564 }
5665 int compareresult = compare (cur , x );
@@ -63,14 +72,38 @@ public boolean contains(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
6372 }
6473 }
6574
75+ /**
76+ * 查找节点
77+ *
78+ * @param cur
79+ * @param x
80+ * @return
81+ */
82+ protected BinaryTreeNode <T > findNode (BinaryTreeNode <T > cur , T x ) {
83+ if (x == null || cur == null || cur .hintcount < 0 ) {
84+ return null ;
85+ }
86+ int compareresult = compare (cur .element , x );
87+ if (compareresult == 0 ) {
88+ return cur ;
89+ } else if (compareresult < 0 ) {
90+ return findNode (cur .right , x );
91+ } else {
92+ return findNode (cur .left , x );
93+ }
94+ }
95+
6696 /**
6797 * 从根节点插入一个新数据
6898 *
6999 * @param x
70100 * @return
71101 */
72- public BinaryTreeNode <T > insert (BinaryTreeNode <T > x ) {
73- return insert (root , x );
102+ public void insert (T x ) {
103+ if (x == null ) {
104+ return ;
105+ }
106+ insert (root , new BinaryTreeNode <T >(x ));
74107 }
75108
76109 /**
@@ -89,6 +122,8 @@ protected BinaryTreeNode<T> insert(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
89122 cur .right = insert (cur .right , x );
90123 } else if (compareresult > 0 ) {
91124 cur .left = insert (cur .left , x );
125+ } else {
126+ cur .hintcount ++;
92127 }
93128 return cur ;
94129 }
@@ -98,8 +133,8 @@ protected BinaryTreeNode<T> insert(BinaryTreeNode<T> cur, BinaryTreeNode<T> x) {
98133 *
99134 * @return
100135 */
101- public BinaryTreeNode < T > findMin () {
102- return findMin (root );
136+ public T findMin () {
137+ return findMin (root ). element ;
103138 }
104139
105140 /**
@@ -108,7 +143,7 @@ public BinaryTreeNode<T> findMin() {
108143 * @param node
109144 * @return
110145 */
111- public BinaryTreeNode <T > findMin (BinaryTreeNode <T > node ) {
146+ protected BinaryTreeNode <T > findMin (BinaryTreeNode <T > node ) {
112147 if (node == null ) {
113148 return null ;
114149 } else if (node .left == null ) {
@@ -122,8 +157,8 @@ public BinaryTreeNode<T> findMin(BinaryTreeNode<T> node) {
122157 *
123158 * @return
124159 */
125- public BinaryTreeNode < T > findMax () {
126- return findMax (root );
160+ public T findMax () {
161+ return findMax (root ). element ;
127162 }
128163
129164 /**
@@ -132,7 +167,7 @@ public BinaryTreeNode<T> findMax() {
132167 * @param node
133168 * @return
134169 */
135- public BinaryTreeNode <T > findMax (BinaryTreeNode <T > node ) {
170+ protected BinaryTreeNode <T > findMax (BinaryTreeNode <T > node ) {
136171 if (node == null ) {
137172 return null ;
138173 } else if (node .right == null ) {
@@ -147,8 +182,26 @@ public BinaryTreeNode<T> findMax(BinaryTreeNode<T> node) {
147182 * @param x
148183 * @return
149184 */
150- public BinaryTreeNode <T > remove (BinaryTreeNode <T > x ) {
151- return remove (root , x );
185+ public void remove (T x ) {
186+ BinaryTreeNode <T > node = findNode (root , x );
187+ if (node == null ) {
188+ return ;
189+ } else if (node .hintcount <= 0 ) {
190+ remove (root , new BinaryTreeNode <T >(x ));
191+ } else {
192+ lazyRemove (node );
193+ }
194+ }
195+
196+ public void lazyRemove (T x ) {
197+ BinaryTreeNode <T > node = findNode (root , x );
198+ if (node != null ) {
199+ node .hintcount --;
200+ }
201+ }
202+
203+ protected void lazyRemove (BinaryTreeNode <T > x ) {
204+ x .hintcount --;
152205 }
153206
154207 /**
@@ -158,7 +211,7 @@ public BinaryTreeNode<T> remove(BinaryTreeNode<T> x) {
158211 * @param x
159212 * @return
160213 */
161- public BinaryTreeNode <T > remove (BinaryTreeNode <T > cur , BinaryTreeNode <T > x ) {
214+ protected BinaryTreeNode <T > remove (BinaryTreeNode <T > cur , BinaryTreeNode <T > x ) {
162215 if (cur == null || x == null ) {
163216 return cur ;
164217 }
0 commit comments