Skip to content

Commit 41d3a49

Browse files
committed
commit
1 parent b32ca24 commit 41d3a49

File tree

41 files changed

+432
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+432
-12
lines changed

.metadata/.log

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ Java Model Exception: Java Model Status [Timed out while retrieving the attached
2121
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
2222
!SUBENTRY 1 org.eclipse.jdt.core 4 1012 2017-09-22 10:30:28.513
2323
!MESSAGE Timed out while retrieving the attached javadoc for Scanner [in Scanner.class [in java.util [in /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar]]]
24+
!SESSION 2017-09-24 15:01:19.261 -----------------------------------------------
25+
eclipse.buildId=debbuild
26+
java.version=1.7.0_131
27+
java.vendor=Oracle Corporation
28+
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=zh_CN
29+
Command-line arguments: -os linux -ws gtk -arch x86_64
30+
31+
!ENTRY org.eclipse.core.resources 2 10035 2017-09-24 15:01:27.343
32+
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package binarytree;
2+
3+
import binarytree.BinaryTree.Node;
4+
5+
public class TestClass {
6+
public static void main(String[] args) {
7+
BinaryTree<Integer> bt=new BinaryTree<>();
8+
bt.insert(5);
9+
bt.insert(9);
10+
bt.insert(6);
11+
bt.insert(3);
12+
bt.insert(5);
13+
bt.insert(9);
14+
bt.insert(6);
15+
bt.insert(2);
16+
Node node=bt.root;
17+
bt.print(node);
18+
}
19+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root = null;
5+
6+
public class Node<T extends Comparable<T>> {
7+
Node leftChild;
8+
Node rightChild;
9+
Node parent;
10+
T key;
11+
}
12+
13+
public void print(Node node) {
14+
System.out.println(node.leftChild.key);
15+
}
16+
17+
// public void delete(T key){
18+
// Node newNode=new Node();
19+
// newNode.key=key;
20+
// Node tem=root;
21+
//
22+
// while(newNode.key!=tem.key){
23+
// if (newNode.key.compareTo(tem.key) < 0) {
24+
// tem = tem.leftChild;
25+
// } else {
26+
// tem = tem.rightChild;
27+
// }
28+
// }
29+
// //tem是最后和给定值相等的节点。
30+
// if (tem.rightChild==null)
31+
// transplant();
32+
// else if tem.rightChild==null
33+
// transplant();
34+
// else y=treeMinimum(tem.rightChild) //右孩子中的最小值。
35+
// if (y.p!=z)
36+
// transplant(t,y,y.right)
37+
// y.right=z.right
38+
// y.right.p=y
39+
// Transplant(T,z,y)
40+
// y.left.p=z.left
41+
// }
42+
//
43+
// private void transplant() {
44+
// // TODO Auto-generated method stub
45+
//
46+
// }
47+
48+
public void insert(T key) { // 向二叉树中插入
49+
Node newNode = new Node();
50+
newNode.key = key;
51+
Node tem = root;
52+
Node p = null;
53+
while (tem != null) {
54+
p = tem;
55+
if (newNode.key.compareTo(tem.key) < 0) {
56+
tem = tem.leftChild;
57+
} else {
58+
tem = tem.rightChild;
59+
}
60+
}
61+
newNode.parent = p;
62+
63+
if (p == null) { //二叉树为空时。
64+
root = newNode;
65+
66+
} else if ((newNode.key).compareTo(p.key) < 0) {
67+
68+
p.leftChild = newNode;
69+
} else {
70+
p.rightChild = newNode;
71+
}
72+
}
73+
74+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root = null;
5+
6+
public class Node<T extends Comparable<T>> {
7+
Node leftChild;
8+
Node rightChild;
9+
Node parent;
10+
T key;
11+
}
12+
13+
public void print(Node node) {
14+
System.out.println(node.leftChild.key);
15+
}
16+
17+
public void delete(T key){
18+
Node temNode=new Node();
19+
Node newNode=new Node();
20+
newNode.key=key;
21+
temNode=root;
22+
while(newNode.key!=temNode.key){
23+
if(temNode.key.compareTo(newNode.key)<0){
24+
temNode=temNode.rightChild;
25+
}else
26+
temNode=temNode.leftChild;
27+
}
28+
if(temNode.leftChild==null){
29+
transplant(temNode,temNode.rightChild);
30+
}else if(temNode.rightChild==null){
31+
transplant(temNode, temNode.leftChild);
32+
}
33+
}
34+
// public void delete(T key){
35+
//// Node newNode=new Node();
36+
//// newNode.key=key;
37+
//// Node tem=root;
38+
////
39+
////
40+
//// if (newNode.key.compareTo(tem.key) < 0) {
41+
//// tem = tem.leftChild;
42+
//// } else {
43+
//// tem = tem.rightChild;
44+
//// }
45+
//// }
46+
//tem是最后和给定值相等的节点。
47+
// if (tem.rightChild==null)
48+
// transplant();
49+
// else if tem.rightChild==null
50+
// transplant();
51+
// else y=treeMinimum(tem.rightChild) //右孩子中的最小值。
52+
// if (y.p!=z)
53+
// transplant(t,y,y.right)
54+
// y.right=z.right
55+
// y.right.p=y
56+
// Transplant(T,z,y)
57+
// y.left.p=z.left
58+
// }
59+
//
60+
// private void transplant() {
61+
// // TODO Auto-generated method stub
62+
//
63+
// }
64+
65+
private void transplant(Node temNode, Node rightChild) {
66+
// TODO Auto-generated method stub
67+
68+
}
69+
70+
public void insert(T key) { // 向二叉树中插入
71+
Node newNode = new Node();
72+
newNode.key = key;
73+
Node tem = root;
74+
Node p = null;
75+
while (tem != null) {
76+
p = tem;
77+
if (newNode.key.compareTo(tem.key) < 0) {
78+
tem = tem.leftChild;
79+
} else {
80+
tem = tem.rightChild;
81+
}
82+
}
83+
newNode.parent = p;
84+
85+
if (p == null) { //二叉树为空时。
86+
root = newNode;
87+
88+
} else if ((newNode.key).compareTo(p.key) < 0) {
89+
90+
p.leftChild = newNode;
91+
} else {
92+
p.rightChild = newNode;
93+
}
94+
}
95+
96+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package binarytree;
2+
3+
import binarytree.BinaryTree.Node;
4+
5+
public class TestClass {
6+
public static void main(String[] args) {
7+
BinaryTree<Integer> bt=new BinaryTree<>();
8+
bt.insert(5);
9+
bt.insert(9);
10+
bt.insert(6);
11+
bt.insert(3);
12+
bt.insert(5);
13+
bt.insert(9);
14+
bt.insert(6);
15+
bt.insert(2);
16+
Node node=bt.root;
17+
bt.print(node);
18+
19+
bt.delete(6);
20+
bt.print(node);
21+
}
22+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root = null;
5+
6+
public class Node<T extends Comparable<T>> {
7+
Node leftChild;
8+
Node rightChild;
9+
Node parent;
10+
T key;
11+
}
12+
13+
public void print(Node node) {
14+
System.out.println(node.leftChild.key);
15+
}
16+
17+
public void delete(T key){
18+
Node temNode=new Node();
19+
Node newNode=new Node();
20+
newNode.key=key;
21+
temNode=root;
22+
while(newNode.key!=temNode.key){
23+
if(temNode.key.compareTo(newNode.key)<0){
24+
temNode=temNode.rightChild;
25+
}else
26+
temNode=temNode.leftChild;
27+
}
28+
if(temNode.leftChild==null){
29+
transplant(temNode,temNode.rightChild);
30+
}else if(temNode.rightChild==null){
31+
transplant(temNode, temNode.leftChild);
32+
}
33+
else{
34+
Node minRight=minimum(temNode.rightChild);
35+
if(minRight.parent!=temNode){
36+
transplant(minRight, minRight.rightChild);
37+
minRight.rightChild=temNode.rightChild;
38+
minRight.rightChild.parent=minRight;
39+
}
40+
transplant(temNode, minRight);
41+
minRight.leftChild=temNode.leftChild;
42+
minRight.leftChild.parent=minRight;
43+
}
44+
}
45+
// public void delete(T key){
46+
//// Node newNode=new Node();
47+
//// newNode.key=key;
48+
//// Node tem=root;
49+
////
50+
////
51+
//// if (newNode.key.compareTo(tem.key) < 0) {
52+
//// tem = tem.leftChild;
53+
//// } else {
54+
//// tem = tem.rightChild;
55+
//// }
56+
//// }
57+
//tem是最后和给定值相等的节点。
58+
// if (tem.rightChild==null)
59+
// transplant();
60+
// else if tem.rightChild==null
61+
// transplant();
62+
// else y=treeMinimum(tem.rightChild) //右孩子中的最小值。
63+
// if (y.p!=z)
64+
// transplant(t,y,y.right)
65+
// y.right=z.right
66+
// y.right.p=y
67+
// Transplant(T,z,y)
68+
// y.left.p=z.left
69+
// }
70+
//
71+
// private void transplant() {
72+
// // TODO Auto-generated method stub
73+
//
74+
// }
75+
76+
private Node minimum(Node rightChild) {
77+
// TODO Auto-generated method stub
78+
while(rightChild.leftChild!=null){
79+
rightChild=rightChild.leftChild;
80+
}
81+
return rightChild;
82+
}
83+
84+
private void transplant(Node temNode, Node rightChild) {
85+
// TODO Auto-generated method stub
86+
if(temNode.parent==null){
87+
root=rightChild;
88+
}else if(temNode.key.compareTo(temNode.parent.leftChild.key)==0){
89+
temNode.parent.leftChild=rightChild;
90+
}else{
91+
temNode.parent.leftChild=temNode;
92+
}
93+
if(rightChild!=null){
94+
rightChild.parent=temNode.parent;
95+
}
96+
}
97+
98+
99+
100+
101+
102+
103+
public void insert(T key) { // 向二叉树中插入
104+
Node newNode = new Node();
105+
newNode.key = key;
106+
Node tem = root;
107+
Node p = null;
108+
while (tem != null) {
109+
p = tem;
110+
if (newNode.key.compareTo(tem.key) < 0) {
111+
tem = tem.leftChild;
112+
} else {
113+
tem = tem.rightChild;
114+
}
115+
}
116+
newNode.parent = p;
117+
118+
if (p == null) { //二叉树为空时。
119+
root = newNode;
120+
121+
} else if ((newNode.key).compareTo(p.key) < 0) {
122+
123+
p.leftChild = newNode;
124+
} else {
125+
p.rightChild = newNode;
126+
}
127+
}
128+
129+
}

0 commit comments

Comments
 (0)