-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
164 lines (160 loc) · 3.72 KB
/
tree.js
File metadata and controls
164 lines (160 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const RED = true
const BLACK = false
class Node {
constructor(key, value) {
this.key = key
this.value = value
this.left = null
this.right = null
this.color = RED
}
}
class RBT {
constructor() {
this.root = null
this.size = 0
}
isRed(node) {
if (!node) return BLACK
return node.color
}
// 左旋 右红左黑
leftRotate(node) {
let tmp = node.right
node.right = tmp.left
tmp.left = node
tmp.color = node.color
node.color = RED
return tmp
}
// 右旋转 左红左子红
rightRoate(node) {
let tmp = node.left
node.left = tmp.right
tmp.right = node
tmp.color = node.color
node.color = RED
return tmp
}
// 颜色翻转
flipColors(node) {
node.color = RED
node.left.color = BLACK
node.right.color = BLACK
}
add(key, value) {
this.root = this.addRoot(this.root, key, value)
this.root.color = BLACK // 根节点始终是黑色
}
addRoot(node, key, value) {
if (!node) {
this.size++
return new Node(key, value)
}
if (key < node.key) {
node.left = this.addRoot(node.left, key, value)
} else if (key > node.key) {
node.right = this.addRoot(node.right, key, value)
} else {
node.value = value
}
if (this.isRed(node.right) && !this.isRed((node.left))) {
node = this.leftRotate(node)
}
if (this.isRed(node.left) && this.isRed((node.left.left))) {
node = this.rightRoate(node)
}
if (this.isRed(node.left) && this.isRed(node.right)) {
this.flipColors(node)
}
return node
}
isEmpty() {
return this.size === 0
}
getSize() {
return this.size
}
contains(key) {
let ans = ''
!(function getNode(node, key) {
if (!node || key === node.key) {
ans = node
return node
} else if (key > node.key) {
return getNode(node.right, key)
} else {
return getNode(node.right, key)
}
})(this.root, key)
return !!ans
}
// bst前序遍历(递归版本)
preOrder(node = this.root) {
if (node == null) return
console.log(node.key)
this.preOrder(node.left)
this.preOrder(node.right)
}
preOrderNR() {
if (this.root == null) return
let stack = []
stack.push(this.root)
while (stack.length > 0) {
let curNode = stack.pop()
console.log(curNode.key)
if (curNode.right != null) stack.push(curNode.right)
if (curNode.left != null) curNode.push(curNode.left)
}
}
// bst中序遍历
inOrder(node = this.root) {
if (node == null) return
this.inOrder(node.left)
console.log(node.key)
this.inOrder(node.right)
}
// bst后续遍历
postOrder(node = this.root) {
if (node == null) return
this.postOrder(node.left)
this.postOrder(node.right)
console.log(node.key)
}
// bsf + 队列的方式实现层次遍历
generateDepthString1() {
let queue = []
queue.unshift(this.root)
while (queue.length > 0) {
let tmpqueue = []; let ans = []
queue.forEach(item => {
ans.push(item.key)
item.left ? tmpqueue.push(item.left) : ''
item.right ? tmpqueue.push(item.right) : ''
})
console.log(...ans)
queue = tmpqueue
}
}
minmun(node = this.root) {
if (node.left == null) return node
return this.minmun(node.left)
}
maximum(node = this.root) {
if (node.right == null) return node
return this.maximum(node.right)
}
}
let btins = new RBT()
let ary = [5, 3, 6, 8, 4, 2]
ary.forEach(value => btins.add(value))
btins.generateDepthString1()
// ///////////////
// 5 //
// / \ //
// 3 8 //
// / \ / //
// 2 4 6 //
// ///////////////
console.log(btins.minmun()) // 2
console.log(btins.maximum()) // 8