Binary Tree explanation of JavaScript data structures and algorithms, and javascript Binary Tree

Source: Internet
Author: User

Binary Tree explanation of JavaScript data structures and algorithms, and javascript Binary Tree

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:
Copy codeThe 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:

Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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:
Copy codeThe 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
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe Code is as follows:
Function getMax (){
Var current = this. root;
While (! (Current. right = null )){
Current = current. right;
}
Return current. data;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.