-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-tree.js
170 lines (148 loc) · 4.53 KB
/
binary-search-tree.js
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
/**
A binary search tree (BST) is a binary tree data structure in which each node has at most two child nodes, referred to as the left child and the right child.
In a BST, the values in the left subtree of a node are less than the node's value, and the values in the right subtree are greater than the node's value.
This property enables efficient searching, insertion, and deletion of elements.
*/
/**
1. Creating a Binary Search Tree:
- To create a binary search tree, you can define a TreeNode class to represent each node, and a BinarySearchTree class to manage the tree structure.
The TreeNode class will have a value, a left child, and a right child.
*/
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
/**
2. Inserting Elements
- You can insert elements into a BST using the insert operation.
It involves comparing the value to be inserted with each node and traversing left or right based on the comparison
until finding an appropriate position to insert a new node.
*/
insert(value) {
const newNode = new TreeNode(value);
if (!this.root) {
this.root = newNode;
} else {
let current = this.root;
while (true) {
if (value < current.value) {
if (current.left === null) {
current.left = newNode;
break;
}
current = current.left;
} else {
if (current.right === null) {
current.right = newNode;
break;
}
current = current.right;
}
}
}
}
/**
3. Searching for an Element
- You can search for an element in a BST using the search operation.
It involves comparing the target value with each node and traversing left or right based on the comparison
until finding a match or reaching a leaf node.
*/
search(value) {
let current = this.root;
while (current !== null) {
if (value === current.value) {
return true;
} else if (value < current.value) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}
/**
4. Finding the minimum value
- You can find the minimum value in a BST using the findMin operation.
The minimum value is located at the leftmost leaf node.
*/
findMin() {
let current = this.root;
while (current.left !== null) {
current = current.left;
}
return current.value;
}
/**
5. Finding the maximum value
- You can find the maximum value in a BST using the findMax operation.
The maximum value is located at the rightmost leaf node.
*/
findMax() {
let current = this.root;
while (current.right !== null) {
current = current.right;
}
return current.value;
}
/**
6. Traversing the tree
- You can traverse a BST in different orders: inorder, preorder, and postorder.
- Inorder traversal visits the left subtree, current node, and then the right subtree.
- Preorder traversal visits the current node, left subtree, and then the right subtree.
- Postorder traversal visits the left subtree, right subtree, and then the current node.
*/
inorderTraversal(node) {
if (node !== null) {
this.inorderTraversal(node.left);
console.log(node.value);
this.inorderTraversal(node.right);
}
}
preorderTraversal(node) {
if (node !== null) {
console.log(node.value);
this.preorderTraversal(node.left);
this.preorderTraversal(node.right);
}
}
postorderTraversal(node) {
if (node !== null) {
this.postorderTraversal(node.left);
this.postorderTraversal(node.right);
console.log(node.value);
}
}
}
// Example Usage - Creating a Binary Search Tree
const bst = new BinarySearchTree();
// Inserting Elements
bst.insert(50);
bst.insert(30);
bst.insert(70);
bst.insert(20);
bst.insert(40);
bst.insert(60);
bst.insert(80);
// Searching for an Element
console.log(bst.search(40)); // Output: true
console.log(bst.search(90)); // Output: false
// Finding the Minimum and Maximum Values
console.log(bst.findMin()); // Output: 20
console.log(bst.findMax()); // Output: 80
// Traversing the Tree
console.log("Inorder Traversal:");
bst.inorderTraversal(bst.root);
// Output: 20, 30, 40, 50, 60, 70, 80
console.log("Preorder Traversal:");
bst.preorderTraversal(bst.root);
// Output: 50, 30, 20, 40, 70, 60, 80
console.log("Postorder Traversal:");
bst.postorderTraversal(bst.root);
// Output: 20, 40, 30, 60, 80, 70, 50