Skip to content

Commit 4f52a98

Browse files
authored
Merge pull request onlyliuxin#24 from akinaru-lu/master
2-26 assignment
2 parents 6fd6284 + 3081df9 commit 4f52a98

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

group08/729770920/2-26/src/com/coding/basic/ArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void add(int index, E e){
2424
throw new IndexOutOfBoundsException(Integer.toString(index));
2525
}
2626
if (data.length == size) {
27-
ensureCapacity(size * 2 + 1);
27+
ensureCapacity(size + size >> 1 + 1);
2828
}
2929
for (int i = size++; i > index; --i) {
3030
data[i] = data[i - 1];
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.coding.basic;
2+
3+
public class BinaryTree<E extends Comparable<? super E>> {
4+
BinaryTreeNode root = null;
5+
6+
public BinaryTree() {
7+
}
8+
9+
public boolean isEmpty() {
10+
return root == null;
11+
}
12+
13+
public void insert(E e) {
14+
root = insert(root, e);
15+
}
16+
17+
private BinaryTreeNode<E> insert(BinaryTreeNode<E> node, E e) {
18+
if (node == null) {
19+
node = new BinaryTreeNode(e, null, null);
20+
}
21+
int compareResult = ((Comparable) e).compareTo(node.data);
22+
if (compareResult < 0) {
23+
node.left = insert(node.left, e);
24+
} else if (compareResult > 0) {
25+
node.right = insert(node.right, e);
26+
}
27+
return node;
28+
}
29+
30+
public void clear() {
31+
root = null;
32+
}
33+
34+
public boolean contains(E e) {
35+
return contains(root, e);
36+
}
37+
38+
private boolean contains(BinaryTreeNode<E> node, E e) {
39+
if (node == null) {
40+
return false;
41+
}
42+
int compareResult = ((Comparable) e).compareTo(node.data);
43+
if (compareResult < 0) {
44+
return contains(node.left, e);
45+
} else if (compareResult > 0) {
46+
return contains(node.right, e);
47+
}
48+
// matching
49+
return true;
50+
}
51+
52+
private BinaryTreeNode<E> findMin(BinaryTreeNode<E> node) {
53+
if (node != null) {
54+
while (node.left != null) {
55+
node = node.right;
56+
}
57+
}
58+
return node;
59+
}
60+
61+
private BinaryTreeNode<E> findMax(BinaryTreeNode<E> node) {
62+
if (node != null) {
63+
while (node.right != null) {
64+
node = node.right;
65+
}
66+
}
67+
return node;
68+
}
69+
70+
public void remove(E e) {
71+
root = remove(root, e);
72+
}
73+
74+
private BinaryTreeNode<E> remove(BinaryTreeNode<E> node, E e) {
75+
if (node == null) {
76+
return node;
77+
}
78+
int compareResult = ((Comparable) e).compareTo(node.data);
79+
if (compareResult < 0) {
80+
node.left = remove(node.left, e);
81+
} else if (compareResult > 0) {
82+
node.right = remove(node.right, e);
83+
}
84+
// matching
85+
if (node.left != null && node.right != null) { // two children
86+
node.data = (E) findMax(node.right).data;
87+
node.right = remove(node.right, node.data);
88+
} else { // one child
89+
node = (node.left != null) ? node.left : node.right;
90+
}
91+
return node;
92+
}
93+
94+
private class BinaryTreeNode<E> {
95+
E data;
96+
BinaryTreeNode left;
97+
BinaryTreeNode right;
98+
99+
public BinaryTreeNode(E e, BinaryTreeNode<E> l, BinaryTreeNode<E> r) {
100+
data = e;
101+
left = l;
102+
right = r;
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)