Tree : Storing data in a hierarchical manner; node : root node, child node, parent node, leaf node (node without any child nodes); layer : Root node starts at layer 0;
two fork Tree : No more than two nodes per node; find faster (than linked list), add, delete fast (than array);
BST: Two fork Tree lookup:
- Sets the root node as the current node;
- If the node to be inserted is less than the current node, set its left node as the new current node, or greater than the right node;
- If the selected node is null, the node to be inserted is placed in this position, exiting;
Basic code to implement:
function Node (data,left,right) { this.data = data; this.show = sh} function show () { console.log (this.data);} function BST () { this.root = root; This.insert = insert; } function Insert (data) { var n = new Node (data,null,null); if (this.root = = = null) { this.root = n; } else { var currnode = this.root,parent; while (true) { parent = Currnode; if (Data < Currnode.data) { currnode = currnode.left; if (Currnode = = = null) { parent.left = n; break; } } else { currnode = currnode.right; if (Currnode = = = null) { parent.right = n; Break;}}}} }
JS: Data structure note 9--two fork tree