The expression of the Binary Search Tree is javascript.
A tree is a non-linear data structure that stores data in layers. The tree is used to store hierarchical data, such as files in the file system. The tree is also used to store ordered lists. Here we will study a special tree: Binary Tree. Select the tree instead of the basic data structure because the search on the binary tree is very fast (but not on the linked list ), adding or deleting elements to or from a binary tree is also very fast (this is not the case for adding or deleting an array ).
A tree is a finite set of n nodes. The top is the root, and the bottom is the root subtree. The node of the tree contains a Data Element and several branches pointing to its subtree. The subtree owned by a node is called the degree of the node. A node with 0 degrees is called a leaf or terminal node. A node with a degree of less than 0 is called a non-terminal node or a branch node. The tree degree is the maximum degree of each node in the tree. The hierarchy of a node is defined from the root, and the root layer is 0th. The maximum hierarchy of nodes in the tree is called the depth or height of the tree.
A binary tree is a special tree with no more than two subnodes. Binary Trees have some special computing properties, making some operations on them extremely efficient. By limiting the number of subnodes to 2, you can write efficient programs to insert, query, and delete data in the tree.
Before using JavaScript to build a binary tree, we need to add two new terms to the tree dictionary. The two subnodes of a parent node are called the left node and the right node respectively. In the implementation of some binary trees, the left node contains a group of specific values, and the right node contains another group of specific values. A binary tree is a special binary tree. A relatively small value is stored in the left node, and a large value is stored in the right node. This feature makes searching highly efficient. This is true for both numeric and non-numeric data, such as words and strings.
The binary search tree consists of nodes. Therefore, we need to define a Node object. The Code is as follows:
Function Node (data, left, right) {// Node class this. data = data; this. left = left; this. right = right; this. show = show;} function show () {// return this. data ;}
Left and right are respectively used to point to left and right subnodes.
Next, you need to create a binary search tree class. The Code is as follows:
Function BST () {// tree class this. root = null; this. insert = insert; this. inOrder = inOrder; this. preOrder = preOrder; this. postOrder = postOrder ;}
Next, insert the code of the node. Traverse the left side of a small plug, and the right side of a large plug. The Code is as follows:
Function insert (data) {// insert var n = new Node (data, null, null); if (this. root = null) {// The first element this. root = n;} else {var current = this. root; // always point to the root node var parent; while (true) {// always run until the Left or Right node is found, parent = current; if (data