-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsa.js
175 lines (159 loc) · 3.96 KB
/
dsa.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
171
172
173
174
175
(function (global) {
global.$ = function () {
return new constructor();
};
// dsaProptotype = {function(){}}
function constructor() {
this.simpleStack = function () {
return new initStack();
};
this.simpleQueue = function () {
return new initQueue();
};
this.bst = function (root, f) {
return new binaryTree(root, f);
};
}
//this functio initializes an empty stack
var initStack = function () {
this.stack = [];
};
initStack.prototype.push = function (val) {
this.stack.push(val);
return this;
};
initStack.prototype.pop = function () {
if (this.isEmpty()) {
throw "Nothing to pop. Stack size is 0";
}
return this.stack.pop();
};
initStack.prototype.isEmpty = function () {
return this.stack.length === 0;
};
// This function initializes an empty queue
var initQueue = function () {
this.queue = [];
};
initQueue.prototype.enqueue = function (val) {
this.queue.push(val);
};
initQueue.prototype.dequeue = function () {
if (this.isEmpty()) {
throw "Nothing to dequeue. Queue size is 0";
}
return this.queue.shift();
};
initQueue.prototype.isEmpty = function () {
return this.queue.length === 0;
};
initQueue.prototype.isNotEmpty = function () {
return this.queue.length !== 0;
};
var Node = function (value) {
this.value = value;
this.left = null;
this.right = null;
};
//This function initializes a binary tree
var binaryTree = function (fn) {
//fn should take 2 argument and return either 0 or 1
this.root = null;
if (fn) {
this.compareFunc = fn;
} else {
this.compareFunc = function (a, b) {
return a >= b ? 0 : 1;
};
}
};
binaryTree.prototype.insert = function (val) {
var self = this;
function insert(root, value) {
if (!self.root) {
self.root = new Node(value);
return;
}
if (!root) {
root = new Node(value);
return root;
}
if (self.compareFunc(root.value, value) === 0) {
root.left = insert(root.left, value);
} else {
root.right = insert(root.right, value);
}
return root;
}
insert(self.root, val);
return this;
};
binaryTree.prototype.inorder = function () {
var self = this;
var treeInorder = []
return (function inorder(root) {
if (root) {
inorder(root.left);
treeInorder.push(root);
inorder(root.right);
}
return treeInorder;
})(self.root);
};
binaryTree.prototype.preorder = function () {
var self = this;
var treePreorder = []
return (function preorder(root) {
if (root) {
treePreorder.push(root);
preorder(root.left);
preorder(root.right);
}
return treePreorder;
})(self.root);
};
binaryTree.prototype.postorder = function () {
var self = this;
var treePostorder = []
return (function postorder(root) {
if (root) {
postorder(root.left);
postorder(root.right);
treePostorder.push(root);
}
return treePostorder;
})(self.root);
};
binaryTree.prototype.levelOrder = function () {
var self = this;
function lo(root) {
let queue = $().simpleQueue();
let levelOrder = [];
if (root) queue.enqueue(root);
else return;
while (queue.isNotEmpty()) {
const node = queue.dequeue();
levelOrder.push(node.value);
if (node.left) {
queue.enqueue(node.left);
}
if (node.right) {
queue.enqueue(node.right);
}
}
return levelOrder;
}
return lo(self.root);
};
})(window);
var b = $().bst(function (a,b){
return a.val >= b.val ? 0 : 1;
});
b.insert({ val: 10 }).insert({ val: 5 }).insert({ val: 20 }).insert({ val: 2 });
// b.insert(5);
// b.insert(20);
// b.insert(2);
console.log(b.preorder());
console.log(b.inorder());
console.log(b.postorder());
// console.log(b.levelOrder());