Javascript code for binary tree implementation, javascript Binary Tree
Preface:
The characteristics of a binary tree (the example graph is only a binary tree, do not try to use the example graph to reason the following conclusions)
- Except for the bottom layer, each node is a parent node, and each node has a maximum of two subnodes;
- In addition to the layer above the mouth, each node is a subnode, and each node has a parent node;
- The node at the top layer (node 50 in the example Figure) is the root node;
The nodes at the bottom layer are called Leaf nodes, and they do not have child nodes;
Value of the Left subnode <value of the parent node <= value of the right Node
1-node javascript implementation
// Node object function Node (data, left, right) {this. data = data; // node value this. left = left; // The left subnode of the current node this. right = right; // The right subnode of the current node this. show = show; // auxiliary function} function show () {return this. data ;}
Feel the code of the above implementation node. Is it a bit similar to the linked list? It stores the current value and references of the next node (left and right subnodes, below is a sketch of pseudo code:
2 binary tree implementation
To implement a binary tree, you must insert a node to form a binary tree. First, let's look at the js Code that implements the binary tree.
function BST() { this.root = null; this.insert = insert;}function insert(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; } } } }}
Then let's take a look at the pseudocode:
Function BST () {this. root = null; // root node this. insert = insert;} function insert (data) {// initialize a node. Why should I initialize the reference of the left and right subnodes as null, because it may be a leaf node, when a subnode is added, the following code adds var n = new Node (data, null, null); if (whether the binary tree is empty, if it is null, the root Node is empty, therefore, you can use the root node to determine whether the binary tree is empty.) {// Save the current node as the root node. this. root = n;} else {// It indicates that the binary tree is not empty. Here, the key is the following code: // 0. while (true); // 1. parent = current; // 2. current = current. left;/current = current. right; // 3. break; Var current = this. root; var parent; while (true) {parent = current; // obtain the parent node. In the first loop, the parent node is the root node if (data <current. data) {// if the current node value is smaller than the value of the parent node, it is stored on the left. Remember the features of the binary tree. If the value is smaller than that of the parent node, it indicates that the node belongs, left subtree of the parent node. Current = current. left; if (current = null) {parent. left = n; break;} // In fact, the above is not easy to understand, it can be equivalent to the following code: // start if (current. left = null) {// If the left node is empty, the empty node is the current location to be inserted. left = n; break;} else {// if it is not empty, continue to the next layer to find an empty node (Insert Location) current = current. left;} // end} else {// The logic code of the right node has the same current = current as that of the left node. right; if (current = null) {parent. right = n; break ;}}}}}
Below is a better understanding of the insert Function
function insert(data) { var n = new Node(data, null, null); if (this.root == null) { this.root = n; } else { var current = this.root; // start change while (true) { if (data < current.data) { if (current.left == null) { current.left = n; break; }else{ current = current.left; } }else { if (current.right == null) { current.right = n; break; }else{ current = current.right; } } } }}
Summary:
Three Components of binary tree implementation
Node object
function Node(data, left, right) { ... }
BST object
function BST() { ... }
Insert node functions
function insert(data) { ... }
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.