Tree: a non-linear data structure that stores data in a hierarchical manner. Used to store hierarchical data.
Tree traversal: access all nodes in the tree in a certain order.
Depth of tree: number of layers
Binary tree: a special tree with no more than two child nodes.
Binary search tree: a special binary tree in which relatively small values are saved in the left node and larger values are saved in the right node. This feature makes the search more efficient.
Traversing binary search tree
- First access the root node, then access the left and right subtrees in the same way
- Medium order accesses all nodes on the binary tree in ascending order according to the key value on the node
- Next, access the leaf node first, from the left subtree to the right subtree, and then to the root node
Create node
function Node(data,left,right){ this.data = data; this.left = left; this.right = right; this.show = function(){ return this.data; }; }
Create a binary search tree
function BST(){ this.root = null; // Sequential traversal this.inOrder = function(node){ if(!(node == null)){ inOrder(node.left); console.log(node.show() + ' '); inOrder(node.right); } }; // Preorder traversal this.preOrder = function(node){ if(!(node == null)){ console.log(node.show() + ' '); preOrder(node.left); preOrder(node.right); } } // Post order traversal this.postOrder = function(node){ if(!(node == null)){ postOrder(node.left); postOrder(node.right); console.log(node.show() + ' '); } } this.insert = function(data){ var n = new Node(data,null,null); if(this.root == null){ this.root = n; } else { var current = this.root; var parent; while(true){ parent = current; if(data < current.data){ current = current.left; if(current == null){ parent.left = n; break; } } else { current = current.right; if(current == null){ parent.right = n; break; } } } } }; }
Search on a binary search tree
There are generally three types of lookups:
- Find given value
- Find minimum
- Find Max
// Find minimum this.getMin = function(){ var current = this.root; while(!(current.left == null)){ current = current.left; } return current.data; }; // Find Max this.getMax = function(){ var current = this.root; while(!(current.right == null)){ current = current.right; } return current.data; } // Find given value this.getData = function(data){ var current = this.root; while(current != null){ if(data < current.data){ current = current.left; } else if(data > current.data) { current = current.right; } else { return current; } } return null; }
Delete node
this.remove = function(data){ root = removeNode(this.root,data); }; function removeNode(node,data){ if(node == null){ return null; } if(data = node.data){ if(node.left == null && node.right == null){ return null; } if(node.left == null){ return node.right; } if(node.right == null){ return node.left; } var tempNode = getSmallest(node.right); // Minimum value on the right subtree find the maximum value on the left subtree or the minimum value on the right subtree of the node to be deleted node.data = tempNode.data; node.right = removeNode(node.right,tempNode.data); return node; } else if(data < node.data){ node.left = removeNode(node.left,data); return node; } else { node.right = removeNode(node.left,data); return node; } };