-
Notifications
You must be signed in to change notification settings - Fork 314
/
BinaryTree.ts
150 lines (122 loc) · 3.01 KB
/
BinaryTree.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
import BinaryTreeNode from './BinaryTreeNode';
class BinaryTree<T> {
public root: BinaryTreeNode<T> | null;
/**
* Initialize the Binary Tree
* @param {*} value The value
* @return {undefined}
*/
constructor(value?: T) {
this.root = value == null ? null : new BinaryTreeNode(value);
}
/**
* Get the number of nodes in the tree.
* @return {number} The number of nodes in the tree.
*/
size(): number {
if (!this.root) {
return 0;
}
return this.root.size();
}
/**
* Get the height of the tree.
* @return {number} The height of the tree.
*/
height(): number {
if (!this.root) {
return 0;
}
return this.root.height();
}
/**
* Traverse the tree in an in-order fashion.
* @return {*[]} An array of values.
*/
inOrder(): Array<T> {
const arr: Array<T> = [];
function inOrderImpl(node: BinaryTreeNode<T> | null) {
if (node == null) {
return;
}
inOrderImpl(node.left);
arr.push(node.value);
inOrderImpl(node.right);
}
inOrderImpl(this.root);
return arr;
}
/**
* Traverse the tree in a pre-order fashion.
* @return {*[]} An array of values.
*/
preOrder(): Array<T> {
const arr: Array<T> = [];
function preOrderImpl(node: BinaryTreeNode<T> | null) {
if (!node) {
return;
}
arr.push(node.value);
preOrderImpl(node.left);
preOrderImpl(node.right);
}
preOrderImpl(this.root);
return arr;
}
/**
* Traverse the tree in a post-order fashion.
* @return {*[]} An array of values.
*/
postOrder(): Array<T> {
const arr: Array<T> = [];
function postOrderImpl(node: BinaryTreeNode<T> | null) {
if (!node) {
return;
}
postOrderImpl(node.left);
postOrderImpl(node.right);
arr.push(node.value);
}
postOrderImpl(this.root);
return arr;
}
/**
* Checks if the binary tree is balanced, i.e. depth of the two subtrees of
* every node never differ by more than 1
* @return {boolean}
*/
isBalanced(): boolean {
function isBalancedImpl(node: BinaryTreeNode<T> | null): boolean {
if (!node) {
return true;
}
const leftHeight = node.left ? node.left.height() : -1;
const rightHeight = node.right ? node.right.height() : -1;
if (Math.abs(leftHeight - rightHeight) > 1) {
return false;
}
return isBalancedImpl(node.left) && isBalancedImpl(node.right);
}
return isBalancedImpl(this.root);
}
isComplete(): boolean {
function isCompleteImpl(
node: BinaryTreeNode<T> | null,
index: number,
numNodes: number,
): boolean {
if (!node) {
return true;
}
if (index >= numNodes) {
return false;
}
return (
isCompleteImpl(node.left, 2 * index + 1, numNodes) &&
isCompleteImpl(node.right, 2 * index + 2, numNodes)
);
}
return isCompleteImpl(this.root, 0, this.size());
}
}
export default BinaryTree;