Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80acc6a

Browse files
committed
二叉树
1 parent 6ea908a commit 80acc6a

File tree

9 files changed

+145
-1
lines changed

9 files changed

+145
-1
lines changed
1 Byte
Binary file not shown.
1.31 KB
Binary file not shown.
1.48 KB
Binary file not shown.
1.11 KB
Binary file not shown.
865 Bytes
Binary file not shown.

src/main/java/com/zetian/review/SetIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void main(String[] args){
1616
set.add(1);
1717
set.add(2);
1818
set.add(9);
19-
set.add(3);
19+
set.add(89);
2020
Iterator iterator = set.iterator();
2121
while (iterator.hasNext()){
2222
if (iterator.next().equals(2)){
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.zetian.review.binarytrees;
2+
3+
/**
4+
* Description
5+
*
6+
* @author Zetian Wang
7+
* @date 2019/11/18
8+
**/
9+
public class BinaryTree {
10+
11+
private class Node {
12+
private Comparable data; //排序的依据就是Comparable
13+
private Node left; //保存左节点
14+
private Node right; //保存右节点
15+
16+
public Node(Comparable data) {
17+
this.data = data;
18+
}
19+
20+
public void addNode(Node newNode) {
21+
if (this.data.compareTo(newNode.data) > 0) {
22+
if (this.left == null) {
23+
this.left = newNode;
24+
} else {
25+
this.left.addNode(newNode);
26+
}
27+
} else {
28+
if (this.right == null) {
29+
this.right = newNode;
30+
} else {
31+
this.right.addNode(newNode);
32+
}
33+
}
34+
}
35+
36+
public void toArrayNode() {
37+
if (this.left != null) { //表示有左节点
38+
this.left.toArrayNode();
39+
}
40+
BinaryTree.this.retData[BinaryTree.this.foot++] = this.data;
41+
if (this.right != null) {
42+
this.right.toArrayNode(); //右子树输出
43+
}
44+
}
45+
}
46+
/**
47+
* 定义根节点
48+
*/
49+
private Node root;
50+
/**
51+
* 保存元素个数
52+
*/
53+
private int count;
54+
private Object[] retData;
55+
private int foot;
56+
/**
57+
* 进行数据的追加,
58+
* 但是所有的Object都必须变为Comparable
59+
*
60+
* @param obj
61+
*/
62+
public void add(Object obj) {
63+
/**
64+
* 必须强制转换为Comparable才能进行Node的保存
65+
*/
66+
Comparable com = (Comparable) obj;
67+
/**
68+
* 创建新的节点
69+
*/
70+
Node newNode = new Node(com);
71+
/**
72+
* 现在不存在根节点
73+
*/
74+
if (this.root == null) {
75+
/**
76+
* 保存根节点
77+
*/
78+
this.root = newNode;
79+
} else {
80+
this.root.addNode(newNode);
81+
}
82+
this.count++;
83+
}
84+
85+
public Object[] toArray() {
86+
if (this.root == null) {
87+
return null;
88+
}
89+
this.foot = 0;
90+
this.retData = new Object[this.count];
91+
this.root.toArrayNode();
92+
return this.retData;
93+
}
94+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.zetian.review.binarytrees;
2+
3+
4+
import java.util.Arrays;
5+
6+
/**
7+
* Description
8+
*
9+
* @author Zetian Wang
10+
* @date 2019/11/18
11+
**/
12+
public class BinaryTreeDemo {
13+
public static void main(String[] args) {
14+
BinaryTree bt = new BinaryTree();
15+
bt.add(new Book("java开发", 79.8));
16+
bt.add(new Book("JSP开发", 88.8));
17+
bt.add(new Book("Android开发", 99.1));
18+
Object[] obj = bt.toArray();
19+
System.out.println(Arrays.toString(obj));
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.zetian.review.binarytrees;
2+
3+
/**
4+
* Description
5+
*
6+
* @author Zetian Wang
7+
* @date 2019/11/18
8+
**/
9+
public class Book implements Comparable<Book> {
10+
private String title;
11+
private double price;
12+
13+
public Book(String title, double price) {
14+
this.title = title;
15+
this.price = price;
16+
}
17+
18+
@Override
19+
public int compareTo(Book o) {
20+
if (this.price > o.price) {
21+
return 1;
22+
} else if (this.price < o.price) {
23+
return -1;
24+
} else {
25+
return 0;
26+
}
27+
}
28+
}
29+

0 commit comments

Comments
 (0)