Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BST :D
- //METHOD: INSERT
- function BST(value){
- this.value = value;
- this.left =null;
- this.right = null;
- }
- BST.prototype.insert = function (value) {
- if(value <= this.value) {
- if(!this.left) this.left = new BST(value);
- else this.left.insert(value);
- }
- else if (value > this.value) {
- if(!this.right) this.right = new BST(value);
- else this.right.insert(value);
- }
- };
- //METHOD: CONTAINS
- BST.prototype.contains = function(value){
- if(value === this.value) {
- return true;
- }
- else if(value < this.value){
- if(!this.left) return false;
- else return this.left.contains(value);
- }
- else if(value > this.value){
- if(!this.right) return false;
- else return this.right.contains(value);
- }
- };
- //DEPTH FIRST TRAVERSAL: travels through all of the nodes from top to bottom. post, pre and order.
- BST.prototype.depthFirstTraversal = function(iteratorFunc, order) {
- if(order === "pre-order") iteratorFunc(this.value);
- if(this.left) this.left.depthFirstTraversal(iteratorFunc, order);
- if(order === "in-order") iteratorFunc(this.value);
- if(this.right) this.right.depthFirstTraversal(iteratorFunc, order);
- if(order === "post-order") iteratorFunc(this.value);
- };
- //BREADTH FIRST TRAVERSAL: it travels level by level
- BST.prototype.breadthFirstTraversal = function(iteratorFunc) {
- var queue =[this]; //queue: first in first out
- while (queue.length) {
- var treeNode = queue.shift();
- iteratorFunc(treeNode);
- if(treeNode.left) queue.push(treeNode.left);
- if(treeNode.right) queue.push(treeNode.right);
- }
- };
- //MIN-MAX
- BST.prototype.getMinVal = function() {
- if(this.left) return this.left.getMinVal();
- else return this.value;
- };
- BST.prototype.getMaxVal = function() {
- if(this.right) return this.right.getMaxVal();
- else return this.value;
- };
- var bst = new BST(50);
- bst.insert(30);
- bst.insert(70);
- bst.insert(100);
- bst.insert(60);
- bst.insert(59);
- bst.insert(20);
- bst.insert(45);
- bst.insert(35);
- bst.insert(85);
- bst.insert(105);
- bst.insert(10);
- //console.log(bst.contains(50));
- //bst.depthFirstTraversal(log, "post-order");
- function log(/*value*/node) {
- console.log(/*value*/node.value);
- }
- bst.breadthFirstTraversal(log);
- console.log("MAX:" + bst.getMaxVal());
- console.log("MIN:" + bst.getMinVal());
- //NOTES:
- //BINARY SEARCH TREES: Collection of nodes all connected together in a certain way. Each parent has two children nodesL: right node and left node. Each node contains value or data. Left nodes <= parent; Right nodes >= parent. Child + parent = binary tree.
- //Traveling through the tree-touching nodes; two patterns: Depth Firts Traversal, Breadth First Traversal (horizontal).
- //RECURSION: when a function calls itself.
- /*function f() {
- if(base case) {
- return whatever;
- }
- else { //recursive case
- f()
- }
- } */
- //factorials (!)
- function factorial(num) {
- if(num === 1) {
- return num;
- }
- else {
- return num * factorial(num -1);
- }
- }
- //Call Stack: what happens at each step? the recursive keeps until it satisfies the base case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement