I. Related concepts of trees 1. Basic concept Subtree
A subtree is composed of a node and its descendants.
The degree of the node
The number of subtrees owned by the node.
The degree of the tree
The maximum number of nodes in a tree
The depth of the node
The depth of the node equals the number of ancestor nodes
Height of the tree
The height of the tree is equal to the maximum value of all node depths
Forest
A collection of trees that do not intersect in several classes. Any tree, deleting the root node becomes a forest.
2. Two fork tree two fork tree definition
(1) The degree of each node in the binary tree is not less than 2
(2) The binary tree is orderly, its sub-tree has left and right points, its order can not be arbitrarily reversed
The nature of binary tree
- There are up to 2^ (k-1) nodes on the first level of K
- Two-tree with a depth of k with up to 2^ (k-1) nodes
3. Several special two-fork tree full two fork tree
Two-2^k-1 tree with a depth of k and a node
Complete binary Tree
Two-tree with n nodes, when and only if each node is numbered from top to bottom, left to right, its number corresponds to node number one by one of 1 to N in the full two fork tree
- Sequence number of the last node with a child node in a full binary tree with N nodes: Math.floor (n/2-1)
Binary search Tree
A binary tree, but it only allows the left dial hand node to store a value smaller than the parent node, and the right child node stores a value larger than the parent node. This is the data structure to be researched in this paper
2. Two binary search tree and related methods of JS implementation code
The function Binarysearchtree () {//node class represents a node in a tree that is const-nodes = function (key) {this.key = key; This.left = null; This.right = null; }; The variable root represents the root node let root = null; Insert a node into the tree This.insert = function (key) {Const NEWNODE = new node (key); Const INSERTNODE = function (node, newNode) {if (Newnode.key < Node.key) {if (Node.left = = = NULL) {node.left = NewNode; } else {Insertnode (node.left, NewNode); }} else {if (node.right = = = null) {node.right = NewNode; } else {Insertnode (node.right, NewNode); }}} if (root = = = null) {root = NewNode; } else {Insertnode (root, NewNode); } }; First Order traversal: root this.preordertraverse = function (callback) {Const PREORDERTRAVERSENODE = functIon (node, callback) {if (node!== null) {callback (Node.key); Preordertraversenode (Node.left, callback); Preordertraversenode (Node.right, callback); }} preordertraversenode (Root, callback)}; Middle sequence traversal: Zogen Right this.inordertraverse = function (callback) {Const INORDERTRAVERSENODE = function (node, callback) { if (node!== null) {Inordertraversenode (Node.left, callback); Callback (Node.key); Inordertraversenode (Node.right, callback); }} inordertraversenode (root, callback); }; Post-post traversal: Left and right root This.postordertraverse = function (callback) {Const POSTORDERTRAVERSENODE = function (node, callback) {if (node!== null) {Postordertraversenode (Node.left, callback); Postordertraversenode (Node.right, callback); Callback (Node.key); } }; Postordertraversenode (Root, callback); }; Look for the minimum value in the tree and return this minimum value this.min = function () {Const MINNODE = function (node) {if (node) { while (Node.left!== null) {node = Node.left; } return Node.key} return null; }; return Minnode (root); }; Looking for the maximum value in the tree, the disease returns this maximum This.max = function () {Const MAXNODE = function (node) {if (node) { while (node.right!== null) {node = node.right; } return Node.key; } return null; }; return Maxnode (root); }//Find if there is a specific value in the tree, there is a return of true, there is no return false this.search = function (key) {Const SEARCHNODE = function (node, key) { if (node = = = null) {return false; } if (Key < Node.key) {return Searchnode (Node.left, key); } elsE if (key > Node.key) {return Searchnode (node.right, key); } else {return true; }} return Searchnode (root, key); }; Remove a node from a tree This.remove = function (key) {Const FINDMINNODE = function (node) {////Unlike Minnode in method min, Minnode returns no De.key, which returns node itself while (Node.left!== null) {node = Node.left; } return node; }; Const REMOVENODE = function (node, key) {if (node = = = null) {return null; } if (Key < Node.key) {//This situation requires an update of Node.left and then returns a new node Node.left = RemoveNode that updated node.left (no De.left, key); return node; } else if (Key > Node.key) {//This situation requires an update of node.right, and then returns the new node Node.right = RemoveNode (node) that updated node.right. Right, key); return node; } else {//This situation requires updating Node.key or other update means (including directly changing node to NULL, or updating node.right), and returning the updated nOde//Case 1, removed is the leaf node if (node.left = = = NULL && node.right = = = null) { node = null; return node; }//Case 2, the node that is removed is only one child node if (node.left = = = null) {//Only right child nodes node = NODE.R ight; return node; } else if (node.right = = = null) {//Only left dial hand nodes node = node.left; return node; }//Case 3, removed is a node with two child nodes const AUX = Findminnode (node.right);//Find the smallest node in the subtree, it must be a leaf node Node.key = aux.key;//Update node key//node.left unchanged//node.right to remove the above AUX node n Ode.right = RemoveNode (Node.right, Aux.key);//update node.right, which actually removes the leaf node of a tree with node.right as root; } }; Root = RemoveNode (root, key); };}
Resources
-"Learning JavaScript data structure and algorithms" Chapter8
-"Data structure (c language version)" Chapter6
Data structure-JS implementation of two-fork search tree