Skip to content

Commit 1802c4a

Browse files
committed
binary tree
1 parent f949917 commit 1802c4a

File tree

97 files changed

+3265
-0
lines changed

Some content is hidden

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

97 files changed

+3265
-0
lines changed
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(0);
12+
bt.insert(5);
13+
bt.insert(9);
14+
bt.insert(6);
15+
bt.insert(0);
16+
Node node=bt.root;
17+
bt.print(node);
18+
}
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root=new Node<T>();
5+
Node p=new Node();
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.key);
15+
}
16+
17+
public void insert(T key){
18+
Node newNode=new Node();
19+
newNode.key=key;
20+
Node tem=root;
21+
if(root==null){
22+
root=newNode;
23+
root.leftChild=null;
24+
root.rightChild=null;
25+
System.out.println(key);
26+
}else{
27+
System.out.println(key);
28+
while(tem!=null){
29+
30+
p=tem;
31+
if (key.compareTo(key)<0){
32+
tem=tem.leftChild;
33+
}
34+
else{
35+
tem=tem.rightChild;
36+
}
37+
}
38+
tem=newNode;
39+
tem.parent=p;
40+
tem.leftChild=null;
41+
tem.rightChild=null;
42+
}
43+
44+
}
45+
46+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package binarytree;
2+
3+
public class TestClass {
4+
5+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 insert(T key) { // 向二叉树中插入
18+
Node newNode = new Node();
19+
newNode.key = key;
20+
Node tem = root;
21+
Node p = null;
22+
while (tem != null) {
23+
p = tem;
24+
if (newNode.key.compareTo(tem.key) < 0) {
25+
tem = tem.leftChild;
26+
} else {
27+
tem = tem.rightChild;
28+
}
29+
}
30+
newNode.parent = p;
31+
32+
if (p == null) {
33+
root = newNode;
34+
35+
} else if ((newNode.key).compareTo(p.key) < 0) {
36+
37+
p.leftChild = newNode;
38+
} else {
39+
p.rightChild = newNode;
40+
}
41+
}
42+
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root=null;
5+
Node p=new Node();
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.rightChild.key);
15+
}
16+
17+
public void insert(T key){
18+
Node newNode=new Node();
19+
newNode.key=key;
20+
Node tem=root;
21+
if(root==null){
22+
root=newNode;
23+
root.leftChild=null;
24+
root.rightChild=null;
25+
System.out.println(key);
26+
}else{
27+
System.out.println(key);
28+
while(tem!=null){
29+
p=tem;
30+
if (key.compareTo(key)<0){
31+
tem=tem.leftChild;
32+
}
33+
else{
34+
tem=tem.rightChild;
35+
}
36+
}
37+
tem=newNode;
38+
tem.parent=p;
39+
tem.leftChild=null;
40+
tem.rightChild=null;
41+
}
42+
43+
}
44+
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root=null;
5+
Node p=new Node();
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.rightChild.key);
15+
}
16+
17+
public void insert(T key){
18+
Node newNode=new Node();
19+
newNode.key=key;
20+
Node tem=root;
21+
if(root==null){
22+
root=newNode;
23+
root.leftChild=null;
24+
root.rightChild=null;
25+
System.out.println(key);
26+
}else{
27+
while(tem!=null){
28+
p=tem;
29+
if (key.compareTo(key)<0){
30+
tem=tem.leftChild;
31+
}
32+
else{
33+
tem=tem.rightChild;
34+
}
35+
}
36+
System.out.println(tem.parent.key);
37+
System.out.println(p.key);
38+
tem=newNode;
39+
newNode.parent=p;
40+
newNode.leftChild=null;
41+
newNode.rightChild=null;
42+
}
43+
44+
}
45+
46+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 insert(T key){
18+
Node newNode=new Node();
19+
newNode.key=key;
20+
21+
Node tem=root;
22+
Node p= null;
23+
/* if(root==null){
24+
root=newNode;
25+
root.leftChild=null;
26+
root.rightChild=null;
27+
28+
}else{*/
29+
while(tem!=null){
30+
p=tem;
31+
if (key.compareTo(tem.key)<0){
32+
tem=tem.leftChild;
33+
System.out.println(key);
34+
}
35+
else{
36+
tem=tem.rightChild;
37+
}
38+
}
39+
newNode.parent=p;
40+
System.out.println(key);
41+
if(p==null){
42+
root=newNode;
43+
System.out.println(key);
44+
}
45+
else if ((newNode.key).compareTo(p.key)<0){
46+
47+
p.leftChild=newNode;
48+
}else{
49+
p.rightChild=newNode;
50+
}
51+
/* newNode.leftChild=null;
52+
newNode.rightChild=null;*/
53+
}
54+
55+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 insert(T key){
18+
Node newNode=new Node();
19+
newNode.key=key;
20+
21+
Node tem=root;
22+
Node p= null;
23+
/* if(root==null){
24+
root=newNode;
25+
root.leftChild=null;
26+
root.rightChild=null;
27+
28+
}else{*/
29+
while(tem!=null){
30+
p=tem;
31+
if (newNode.key.compareTo(tem.key)<0){
32+
tem=tem.leftChild;
33+
}
34+
else{
35+
tem=tem.rightChild;
36+
}
37+
}
38+
newNode.parent=p;
39+
40+
if(p==null){
41+
root=newNode;
42+
43+
}
44+
else if ((newNode.key).compareTo(p.key)<0){
45+
46+
p.leftChild=newNode;
47+
}else{
48+
p.rightChild=newNode;
49+
}
50+
/* newNode.leftChild=null;
51+
newNode.rightChild=null;*/
52+
}
53+
54+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package binarytree;
2+
3+
public class BinaryTree<T extends Comparable<T>> {
4+
Node<T> root=new Node<T>();
5+
Node p=new Node();
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(5);
15+
System.out.println(node.key);
16+
}
17+
18+
public void insert(T key){
19+
Node newNode=new Node();
20+
newNode.key=key;
21+
Node tem=root;
22+
if(root==null){
23+
root=newNode;
24+
root.leftChild=null;
25+
root.rightChild=null;
26+
}else{
27+
while(tem!=null){
28+
29+
p=tem;
30+
if (key.compareTo(key)<0){
31+
tem=tem.leftChild;
32+
}
33+
else{
34+
tem=tem.rightChild;
35+
}
36+
}
37+
newNode.parent=p;
38+
newNode.leftChild=null;
39+
newNode.rightChild=null;
40+
}
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)