-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathIterative_preorder.js
139 lines (124 loc) · 3.14 KB
/
Iterative_preorder.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
/*
The traversal operation is a frequently used operation on a binary tree. This operation is used to visit each node in the tree exactly once
A full traversal on a binary tree gives a linear ordering of data in the tree . This is the iterative preorder tree traversal algorithms
*/
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element)
}
pop() {
if (this.items.length == 0) {
return "Underflow";
}
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1]
}
isEmpty() {
return this.items.length == 0;
}
}
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
//insert function inserts a new node into the binary search tree
insert(value) {
var New = new Node(value);
if (this.root === null) {
this.root = New;
return this;
}
let curr = this.root;
let prev = null;
while (curr) {
if (value < curr.data) {
prev = curr;
curr = curr.left;
}
else if (value > curr.data) {
prev = curr;
curr = curr.right;
}
}
if (prev.data > value) {
prev.left = New;
return this;
}
else {
prev.right = New;
return this;
}
}
iterative_preorder(node) {
let ptr = node;
let stack = new Stack();
let s = ""
stack.push(ptr);
while (!stack.isEmpty()) {
ptr = stack.pop();
if (ptr != null) {
s += ptr.data + " "
stack.push(ptr.right)
stack.push(ptr.left)
}
}
console.log(s)
}
}
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const getLine = (function () {
const getLineGen = (async function* () {
for await (const line of rl) {
yield line;
}
})();
return async () => ((await getLineGen.next()).value);
})();
const main = async () => {
console.log("Enter the number of nodes to insert");
let n = Number(await getLine());
let b = new BinarySearchTree();
console.log("Enter the values of nodes to insert\n")
for (let i = 0; i < n; i++) {
console.log("Enter the value for node " + (i + 1));
b.insert(Number(await getLine()));
}
let root = b.root;
console.log("The preorder traversal for the binary search tree ")
b.iterative_preorder(root);
process.exit(0);
}
main();
/*
Sample I/O:
Enter the number of nodes to insert
5
Enter the values of nodes to insert
Enter the value for node 1
3
Enter the value for node 2
4
Enter the value for node 3
2
Enter the value for node 4
5
Enter the value for node 5
1
The preorder traversal for the binary search tree
3 2 1 4 5
Time Complexity = O( n )
Space Complexity = O( n )
*/