A tree is a non-linear data structure that stores data in a hierarchical manner. Trees are used to store hierarchical relational data, such as files in a file system, and trees are used to store sequential tables. A special tree is to be studied here: two-fork tree. Selecting a tree rather than the basic data structure is because finding on a binary tree is very fast (and not on a linked list), adding or removing elements to a two-tree is also very fast (not the addition or deletion of an array).
A tree is a finite set of n nodes. The topmost is the root , and the following is the subtree of the root. The nodes of a tree contain a data element and several branches that point to its subtree. A node-owned subtree is called the degree of the node . A node with a degree of 0 is called a leaf or terminal node. Nodes that are not 0 degrees are called non-terminal nodes or branch nodes . The degree of the tree is the maximum of the degree of each node within the tree. The hierarchy of nodes is defined from the root, and the root is the No. 0 layer. The maximum level of nodes in a tree is called the depth or height of the tree.
Binary tree is a special kind of tree, its number of child nodes is no more than two. Binary trees have some special computational properties, making some operations above them exceptionally efficient. By limiting the number of child nodes to 2, you can write efficient programs to insert, find, and delete data in the tree.
Before using JavaScript to build a binary tree, we need to add two more words to our dictionary of trees. The two child nodes of a parent node are called the left and right nodes, respectively. In the implementation of some binary trees, the left node contains a specific set of values, and the right node contains another set of specific values . Binary search Tree is a special two-fork tree, with relatively small values stored in the left node, and large values stored in the right node. This feature makes finding efficient, both for numeric and non-numeric data, such as words and strings.
Binary lookup trees are made up of nodes, so we define a node object with the following code:
function Node (data,left,right) {//Node class this.data=data;this.left=left;this.right=right;this.show=show;} Function Show () {//Display data in Node return this.data;}
Where left and right are respectively used to point to the right and right sub-nodes.
Next you need to create a two-fork lookup tree class with the following code:
function BST () {//Tree class This.root=null;this.insert=insert;this.inorder=inorder;this.preorder=preorder;this.postorder =postorder;}
Next is the code to insert the node. Traverse the small plug to the left, big plug to the right. The code is as follows:
function Insert (data) {//insert operation var n=new Node (Data,null,null); if (this.root==null) {//first element this.root=n;} Else{var current=this.root;//always points to the root node var parent;while (True) {//runs until the left node or right node is found Parent=current;if (data< Current.data) {current=current.left;if (current==null) {//If there is no left node parent.left=n;break;}} Else{current=current.right;if (current==null) {//If there is no right node parent.right=n;break;} If there is a right node, jump to the while and re-execute the node as the parent again to start the judgment}}}
Binary Lookup tree representation javascript