This article mainly introduces the binary tree of JavaScript Data Structures and Algorithms. This article describes the concept of binary tree, the characteristics of binary tree, the definition of binary tree nodes, and the maximum and minimum values for searching, for more information, see
Binary Tree concept
A Binary Tree is a finite set of n (n> = 0) nodes. it is either an empty set or an empty one ), or a binary tree consisting of a root node and two left and right trees that do not conflict with each other.
Binary Tree features
Each node has a maximum of two subtrees, so the binary tree does not have nodes with a degree greater than 2. Each node in a binary tree is an object, and each data node has three pointers pointing to the parent, left child, and right child respectively. Each node is connected by pointers. The link pointer is a parent-child relationship.
Binary tree node definition
Binary Tree nodes are defined as follows:
The code is as follows:
Struct BinaryTreeNode
{
Int m_nValue;
BinaryTreeNode * m_pLeft;
BinaryTreeNode * m_pRight;
};
Five basic binary tree forms
Empty binary tree
Only one root node
The root node only has the left subtree.
The root node only has the right subtree.
The root node has both the left subtree and the right subtree.
There are only two common trees with three nodes: two or three. However, because the binary tree needs to be differentiated between the left and right, it will become the following five forms:
Special binary tree
Oblique tree
As shown in the 2nd and 3 small pictures in the first and last pictures above.
Full binary tree
In a binary tree, if all the branches have left and right trees, and all the leaves are on the same layer, such a binary tree is called a full binary tree. As shown in:
Full binary tree
A full binary tree indicates that the left side of the last layer is full, the right side may be full or not, and the rest of the layers are full. A binary tree with a depth of k and a number of nodes of 2 ^ k-1 is a full binary tree (full binary tree ). It is a tree with a depth of k and no space available.
Full binary tree features:
Leaf nodes can only appear at the bottom two layers.
The bottom leaves must be concentrated in the left continuous position.
The second-to-last layer. if there is a leaf node, it must be in a continuous position on the right.
If the node degree is 1, the node has only the left child.
For binary trees of the same node tree, the depth of the full binary tree is the minimum.
Note: Full binary trees must be full binary trees, but full binary trees may not be full binary trees.
The algorithm is as follows:
The code is as follows:
Bool is_complete (tree * root)
{
Queue q;
Tree * ptr;
// Perform breadth-first traversal (hierarchical traversal) and put NULL nodes into the queue
Q. push (root );
While (ptr = q. pop ())! = NULL)
{
Q. push (ptr-> left );
Q. push (ptr-> right );
}
// Determine whether there are still unaccessed nodes
While (! Q. is_empty ())
{
Ptr = q. pop ();
// If there are unaccessed non-NULL nodes, the tree is empty and the non-completely binary tree
If (NULL! = Ptr)
{
Return false;
}
}
Return true;
}
Binary Tree nature
Binary Tree nature 1: at most 2 ^ (i-1) nodes on the I layer of the binary tree (I> = 1)
The nature of Binary Tree 2: depth of k binary tree at most 2 ^ K-1 nodes (k> = 1)
Ordered storage structure of binary trees
The ordered storage structure of a binary tree stores all nodes in a one-dimensional array, and the storage location of the nodes reflects the logical relationship between nodes.
Binary linked list
Since the applicability of sequential storage is not strong, we need to consider the chain storage structure. Binary Tree storage adopts a chain storage structure according to international practices.
Each node of a binary tree has two children at most. Therefore, it is natural to design a Data Domain and two pointer domains for it. We call such a linked list a binary linked list.
Binary Tree traversal
The traversal of a binary tree is to access all the nodes in the binary tree in sequence starting from the root node, so that each node is accessed once and only once.
There are three methods to traverse a binary tree:
(1) first Traverse (DLR), first visit the root node, then traverse the left subtree, and finally traverse the right subtree. Note root-left-right.
(2) in the middle-order traversal (LDR), first traverse the left subtree, then access the root node, and finally traverse the right subtree. Note: Left-root-right.
(3) Post-sequential traversal (LRD): First traverse the left subtree, then traverse the right subtree, and finally access the root node. Note: left-right-root.
Forward traversal:
If the binary tree is empty, the null operation is returned. Otherwise, the root node is accessed, and the left subtree is traversed in the first order, and then the right subtree is traversed in the first order.
The traversal order is as follows: A B D H I E J C F K G
The code is as follows:
// First-order traversal
Function preOrder (node ){
If (! Node = null ){
Putstr (node. show () + "");
PreOrder (node. left );
PreOrder (node. right );
}
}
In-order traversal:
If the tree is empty, an empty operation is returned. Otherwise, the system starts from the root node (note that the root node is not accessed first), traverses the left subtree of the root node in a middle order, and then accesses the root node, finally, the right subtree is traversed in the middle order.
The traversal order is: H D I B E J A F K C G
The code is as follows:
// Use recursion to implement sequential traversal
Function inOrder (node ){
If (! (Node = null )){
InOrder (node. left); // first access the left subtree
Putstr (node. show () + ""); // access the root node
InOrder (node. right); // finally access the right subtree
}
}
Post-order traversal:
If the tree is empty, the null operation is returned. Otherwise, the left and right subtree is traversed from left to right, and then the root node is accessed.
The traversal order is: H I D J E B K F G C
The code is as follows:
// Post-order traversal
Function postOrder (node ){
If (! Node = null ){
PostOrder (node. left );
PostOrder (node. right );
PutStr (node. show () + "");
}
}
Implement binary search tree
The binary search tree (BST) is composed of nodes. Therefore, we define a Node object as follows:
The code is as follows:
Function Node (data, left, right ){
This. data = data;
This. left = left; // save the left node link.
This. right = right;
This. show = show;
}
Function show (){
Return this. data; // display the data stored in the node
}
Find the maximum and minimum values
It is very easy to find the minimum and maximum values on the BST, because smaller values are always on the left subnode and the minimum value on the BST, you only need to traverse the left subtree until the last node is found.
Find minimum
The code is as follows:
Function getMin (){
Var current = this. root;
While (! (Current. left = null )){
Current = current. left;
}
Return current. data;
}
This method traverses the left subtree of BST one by one until it traverses to the leftmost node of BST, which is defined:
The code is as follows:
Current. left = null;
The value saved on the current node is the minimum value.
Find the maximum value
To find the maximum value on BST, you only need to traverse the right subtree until the last node is found. The value saved on this node is the maximum value.
The code is as follows:
Function getMax (){
Var current = this. root;
While (! (Current. right = null )){
Current = current. right;
}
Return current. data;
}