用JavaScript实现树的前序、中序、后续查询以及高度计算

用JavaScript实现树的前序、中序、后续查询以及高度计算

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
class TreeNode {
constructor (value) {
this.value = value;
this.left = this.right = null;
}
}

var maxDepth = function(troot) {
if (!troot) {
return 0;
}

const left = maxDepth(troot.left);
const right = maxDepth(troot.right);
return Math.max(left, right) + 1;
}

var preOrder = function(root) {
if (!root) {
return;
}
console.log(root.value);
preOrder(root.left);
preOrder(root.right);
}

var preOrderWithoutRescursion = function(root) {
let stack = [];
let pointNode = root;

while(pointNode || stack.length > 0) {
while(pointNode) {
console.log(pointNode.value);
stack.push(pointNode);
pointNode = pointNode.left;
}

if (stack.length > 0) {
pointNode = stack.pop();
pointNode = pointNode.right;
}
}
}

var midOrder = function(root) {
if (!root) {
return;
}
midOrder(root.left);
console.log(root.value);
midOrder(root.right);
}

var midOrderWithoutRescursion = function(root) {
let stack = [];
let pointNode = root;

while(pointNode || stack.length > 0) {
while(pointNode) {
stack.push(pointNode);
pointNode = pointNode.left;
}

if (stack.length > 0) {
pointNode = stack.pop();
console.log(pointNode.value);
pointNode = pointNode.right;
}
}
}

var postOrder = function(root) {
if (!root) {
return;
}
postOrder(root.left);
postOrder(root.right);
console.log(root.value);
}

var postOrderWithoutRescursion = function(root) {
let stack = [];
let pointNode = root;
let right = root;

while(pointNode || stack.length > 0) {
if (pointNode) {
stack.push(pointNode);
pointNode = pointNode.left;
} else {
pointNode = stack.length > 0 ? stack[stack.length - 1] : null;
if (pointNode.right && pointNode.right != right) {
pointNode = pointNode.right;
} else{
pointNode = stack.pop();
console.log(pointNode.value);
right = pointNode;
pointNode = null;
}
}
}
}

var levelOrder = function(root) {
if (!root) {
return;
}

let queue = [];
queue.unshift(root);

while(queue.length > 0) {
let pointNode = queue.pop();
console.log(pointNode.value);

if (pointNode.left) {
queue.unshift(pointNode.left)
}

if (pointNode.right) {
queue.unshift(pointNode.right);
}
}
}

var tree = new TreeNode(3);
tree.left = new TreeNode(9);
tree.right = new TreeNode(20);
tree.right.left = new TreeNode(15);
tree.right.right = new TreeNode(7);

let depth = maxDepth(tree);
console.log("depth:"+depth);

console.log("pre order:");
preOrder(tree);

console.log("mid order:");
midOrder(tree)

console.log("post order:");
postOrder(tree)

console.log("pre order without recursion");
preOrderWithoutRescursion(tree);

console.log("mid order without recursion");
midOrderWithoutRescursion(tree);

console.log("post order without recursion");
postOrderWithoutRescursion(tree);

console.log("level order");
levelOrder(tree);

运行结果:

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
depth:3
pre order:
3
9
20
15
7
mid order:
9
3
15
20
7
post order:
9
15
7
20
3
pre order without recursion
3
9
20
15
7
mid order without recursion
9
3
15
20
7
post order without recursion
9
15
7
20
3
level order
3
9
20
15
7
Author: y500
Link: https://www.y500.me/2020/06/29/JavaScript-Depth-of-Tree/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.