Skip to content

Commit 68df125

Browse files
committed
添加二叉树实现
1 parent 7524d10 commit 68df125

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<module>sort</module>
1212
<module>sort</module>
1313
<module>lru</module>
14+
<module>tree</module>
1415
</modules>
1516
<name>algorithm-study</name>
1617
<packaging>pom</packaging>

tree/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>algorithm-study</artifactId>
7+
<groupId>com.mistray</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>tree</artifactId>
13+
14+
15+
</project>
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.mistray;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray
7+
* @create 2019年10月17日 10:45
8+
* @Desc 二叉查找树
9+
*/
10+
public class BST<Key extends Comparable<Key>, Value> {
11+
//二叉树查找根节点
12+
private Node root;
13+
14+
private class Node {
15+
// 键
16+
private Key key;
17+
// 值
18+
private Value value;
19+
// 执行子树的连接
20+
private Node left, right;
21+
// 以该节点为根的子树中的总结点数
22+
private int N;
23+
24+
public Node(Key key, Value value, int n) {
25+
this.key = key;
26+
this.value = value;
27+
N = n;
28+
}
29+
}
30+
31+
/**
32+
* 获取根节点的子树中总结点数
33+
*
34+
* @return 根节点的子树找那个总结点数
35+
*/
36+
public int size() {
37+
return size(root);
38+
}
39+
40+
/**
41+
* 获取x节点的子树中总结点数
42+
*
43+
* @param x 节点
44+
* @return 子树中总结点数
45+
*/
46+
public int size(Node x) {
47+
if (x == null) {
48+
return 0;
49+
} else {
50+
return x.N;
51+
}
52+
}
53+
54+
public Value get(Key key) {
55+
return get(root, key);
56+
}
57+
58+
public Value get(Node x, Key key) {
59+
// 在以x为根节点的子树中查找并返回key所对应的的值
60+
// 找不到就返回null
61+
if (x == null) {
62+
return null;
63+
}
64+
int compare = key.compareTo(x.key);
65+
// 如果查询元素比key小,则找左子树
66+
if (compare < 0) {
67+
// 递归找左子树
68+
return get(x.left, key);
69+
}
70+
// 如果查询元素比key大,则找右子树
71+
else if (compare > 0) {
72+
return get(x.right, key);
73+
}
74+
// 如果查找元素和key相同,则直接返回
75+
else {
76+
return x.value;
77+
}
78+
}
79+
80+
public void put(Key key, Value value) {
81+
root = put(root, key, value);
82+
}
83+
84+
public Node put(Node x, Key key, Value value) {
85+
// 如果key存在于以x为根节点的子树中则更新它的值
86+
// 否则将以key和val为键值对的新节点插入到该子树中
87+
if (x == null) {
88+
return new Node(key, value, 1);
89+
}
90+
// 将key与x节点对比.
91+
int compare = key.compareTo(x.key);
92+
if (compare < 0) {
93+
x.left = put(x.left, key, value);
94+
} else if (compare > 0) {
95+
x.right = put(x.right, key, value);
96+
97+
x.value = value;
98+
}
99+
// 修改当前节点的size为左边子树节点数+右侧子树节点数+1;
100+
x.N = size(x.left) + size(x.right) + 1;
101+
return x;
102+
}
103+
104+
public Key min() {
105+
return min(root).key;
106+
}
107+
108+
public Node min(Node x) {
109+
if (x.left == null) {
110+
return x;
111+
} else {
112+
return min(x);
113+
}
114+
}
115+
116+
// 向下取整
117+
private Node floor(Node x, Key key) {
118+
// 如果根节点为null,则直接返回null.
119+
if (x == null) {
120+
return null;
121+
}
122+
// key与根节点比较大小
123+
int compare = key.compareTo(x.key);
124+
// 如果key == 根节点的key,直接返回
125+
if (compare == 0) {
126+
return x;
127+
}
128+
// 如果key小于根节点的key,则往左边找.
129+
else if (compare < 0) {
130+
return floor(x.left, key);
131+
}
132+
// 左边已经没有符合要求的同时,key也不等于root.
133+
// 把当前节点右侧的节点按上面的逻辑的递归一下
134+
Node t = floor(x.right, key);
135+
if (t != null) {
136+
return t;
137+
} else {
138+
return x;
139+
}
140+
}
141+
142+
143+
}

0 commit comments

Comments
 (0)