-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
90 lines (85 loc) · 1.78 KB
/
Copy pathtree.js
File metadata and controls
90 lines (85 loc) · 1.78 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
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
isEmpty() {
return this.root === null;
}
insert(value) {
const newNode = new Node(value);
if (this.isEmpty()) {
this.root = newNode;
} else {
this.insertNode(this.root, newNode);
}
}
insertNode(root, newNode) {
if (newNode.value < root.value) {
if (root.left === null) {
root.left = newNode;
} else {
return this.insertNode(root.left, newNode);
}
} else {
if (root.right === null) {
root.right = newNode;
} else {
return this.insertNode(root.right, newNode);
}
}
}
search(root, value) {
if (!root) {
return false;
} else {
if (root.value === value) {
return true;
} else if (value < root.value) {
return this.search(root.left, value);
} else {
return this.search(root.right, value);
}
}
}
preOrder(root) {
if (root) {
console.log(root.value);
this.preOrder(root.left);
this.preOrder(root.right);
}
}
inOrder(root) {
if (root) {
this.inOrder(root.left);
console.log(root.value);
this.inOrder(root.right);
}
}
postOrder(root) {
if (root) {
this.postOrder(root.left);
this.postOrder(root.right);
console.log(root.value);
}
}
}
// const bst = new BinarySearchTree();
// bst.insert(10);
// bst.insert(5);
// bst.insert(15);
// bst.insert(3);
// bst.insert(7);
// console.log(bst.search(bst.root, 7));
// console.log(bst.search(bst.root, 8));
// console.log(bst.search(bst.root, 1));
// bst.postOrder(bst.root);
class BreadthSaerchTree {
constructor() {}
}