-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.ts
179 lines (156 loc) · 4.15 KB
/
Tree.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
export class TreeNode<T> {
public data: T;
public left;
public right;
constructor(d: T) {
this.data = d;
this.left = this.right = null;
}
}
export class BST<T> {
public root: TreeNode<T>;
constructor() {
this.root = null;
}
public initWithArray(arr:T[]) {
for(let a of arr) {
this.insert(a);
}
}
public insert(d: T) {
let node = new TreeNode(d);
if (this.root == null) {
this.root = node;
return;
}
let cur = this.root;
let parent;
while (true) {
parent = cur;
if (d < cur.data) {
cur = cur.left;
if (cur == null) {
parent.left = node;
break;
}
} else {
cur = cur.right;
if (cur == null) {
parent.right = node;
break;
}
}
}
}
public remove(d:T) {
this.root = this.removeNode(this.root, d);
}
public removeNode(node:TreeNode<T>, d:T): TreeNode<T> {
if(node == null) {
return null;
}
if(d == node.data) {
if(node.left == null && node.right == null) {
return null;
}
if(node.left == null) {
return node.right;
}
if(node.right == null) {
return node.left;
}
let tempNode = this.getSmallestNode(node.right);
node.data = tempNode.data;
node.right = this.removeNode(node.right, tempNode.data);
return node;
} else if( d < node.data) {
node.left = this.removeNode(node.left, d);
return node;
} else {
node.right = this.removeNode(node.right, d);
return node;
}
}
public getSmallestNode(node:TreeNode<T>): TreeNode<T> {
let cur = node;
while(!(cur.left == null)) {
cur = cur.left;
}
return cur;
}
// count node number
public count():number {
return this.countNode(this.root);
}
private countNode(node: TreeNode<T>):number {
let cur = node;
if(cur != null) {
return 1 + this.countNode(cur.left) + this.countNode(cur.right);
}
return 0;
}
// count edges
public edges():number {
return this.countNode(this.root) - 1;
}
// get minum data
public min(): T {
let cur = this.root;
while (!(cur.left == null)) {
cur = cur.left;
}
return cur.data;
}
// get max data
public max(): T {
let cur = this.root;
while (!(cur.right == null)) {
cur = cur.right;
}
return cur.data;
}
// find the node
public find(d: T): TreeNode<T> {
let cur = this.root;
while (cur != null) {
if (cur.data == d) {
return cur;
} else if (d < cur.data) {
cur = cur.left;
} else {
cur = cur.right;
}
}
return null;
}
// in order traversal, the result is ascending sort
public inOrder(node: TreeNode<T>): T[] {
let res = [];
if (!(node == null)) {
res = res.concat(this.inOrder(node.left));
res.push(node.data);
res = res.concat(this.inOrder(node.right));
}
return res;
}
// pre order travsersal
public preOrder(node: TreeNode<T>): T[] {
let res = [];
if (!(node == null)) {
res.push(node.data);
res = res.concat(this.preOrder(node.left));
res = res.concat(this.preOrder(node.right));
}
return res;
}
// post order travsersal
public postOrder(node: TreeNode<T>): T[] {
let res = [];
if (!(node == null)) {
res = res.concat(this.postOrder(node.left));
res = res.concat(this.postOrder(node.right));
res.push(node.data);
}
return res;
}
}