JavaScript data structure and algorithm binary tree Detailed _ basics

Source: Internet
Author: User

The concept of two-fork tree

A binary tree (Binary) is a finite set of n (n>=0) nodes, the collection is either an empty set (a null binary tree) or a two-forked tree of a root node and two disjoint Saozi, called root nodes, respectively.

The characteristics of binary tree

Each node has a maximum of two Shang trees, so there are no nodes with degrees greater than 2 in the binary tree. Each node in the binary tree is an object, and each data node has three pointers, which point to the parents, the left child and the right child. Each node is connected to each other through pointers. The relationship between the attached pointers is a parent-child relationship.

The definition of binary tree node

The binary tree node is defined as follows:

Copy Code code as follows:

struct Binarytreenode
{
int m_nvalue;
binarytreenode* M_pleft;
binarytreenode* M_pright;
};

Five basic forms of two-fork tree

Empty two-pronged tree
There's only one root knot.
The root knot is only Zuozi.
The root node is only the right subtree.
Root nodes are both Zuozi and right subtrees.

There are only two common trees with three nodes: two or three layers. But because the binary tree to differentiate between the left and right, so it will evolve into the following five kinds of forms:

Special two-fork tree

Oblique tree

As shown in the 2nd and 3 small graphs of the first and last diagrams above.

Full two fork Tree

In a binary tree, if all branch nodes exist Saozi right subtree, and all leaves are on the same layer, such two-fork tree is called full two-fork tree. As shown in the following illustration:

Complete two-fork tree

A complete binary tree is the last layer on the left is full, the right may be full or dissatisfied, and then the rest of the layer is full. A two-fork tree with a depth of K and a 2^k-1 number of nodes is a full two-fork tree (complete binary tree). is a tree with a depth of k and no vacancy.

The characteristics of the complete binary tree are:

Leaf nodes can only be present at the bottom two levels.

The lowest leaves must be concentrated in the left continuous position.

The penultimate layer, if there is a leaf node, must be in the right hand continuous position.

If the node degree is 1, then the node is only the left child.

The two-forked tree of the same node tree has the smallest depth of the complete binary tree.

Note: Full two fork tree must be a complete binary tree, but the complete binary tree is not necessarily full two fork tree.

The algorithm is as follows:

Copy Code code as follows:

BOOL Is_complete (tree *root)
{
Queue q;
Tree *ptr;
Perform breadth-first traversal (hierarchy traversal) and put the null node into the queue
Q.push (root);
while (ptr = Q.pop ())!= NULL)
{
Q.push (Ptr->left);
Q.push (Ptr->right);
}

Determine if there are any nodes that have not been accessed
while (!q.is_empty ())
{
ptr = Q.pop ();

A non-null node that has not been accessed, the tree has a void, a non-complete binary tree
if (NULL!= ptr)
{
return false;
}
}

return true;
}

The properties of binary tree

The nature of a binary tree: There are at most 2^ (i-1) nodes (I>=1) on the first layer of the binary tree.

The nature of the binary tree two: The two-forked tree with a depth of K has at most 2^k-1 nodes (k>=1).

Sequential storage structure of binary tree

The sequential storage structure of binary tree is to store each node in the binary tree with one-dimensional array, and the storage location of nodes can reflect the logical relationship between nodes.

Two-fork linked list

Since the application of sequential storage mode is not strong, then we have to consider the chain-type storage structure. Binary tree storage in accordance with international practice is generally the use of chain-type storage structure.

Binary tree Each node has up to two children, so it is more natural to design a data field and two pointer fields, which we call a list of two forks.

Traversal of binary Tree

The traversal of binary trees (traversing binary tree) means that all nodes in a binary tree are accessed sequentially from the root node, in some order, so that each node is accessed once and accessed only once.

There are three ways to traverse a binary tree, as follows:

(1) Pre-sequence traversal (DLR), first accessing the root node, then traversing the left subtree, and finally traversing the right subtree. Jenkgen-left-right.

(2) Sequence traversal (LDR), first traversing the left subtree, then accessing the root node, and finally traversing the right subtree. denoted left-root-right.

(3) subsequent traversal (LRD), first traversing the left subtree, and then traversing the right subtree, the last access to the root node. Denoted left-right-root.

Pre-sequence Traversal:

If the binary tree is empty, the empty operation returns, otherwise the first access to the root node, and then the first sequence of the left subtree, and then the previous sequence to traverse the right subtree.

The order of traversal is: A B D H I E J C F K G

Copy Code code as follows:

First-order traversal
function preorder (node) {
if (!node = = null) {
Putstr (Node.show () + "");
Preorder (Node.left);
Preorder (node.right);
}
}

In-sequence traversal:

If the tree is empty, the empty operation returns, otherwise starting from the root node (note that the root node is not first accessed), the middle sequence traverses the left subtree of the root node, then accesses the root node, and the last sequence traverses the right subtree.

The order of traversal is: H D I B E J A F K C G

Copy Code code as follows:

Using recursion to implement middle-order traversal
function inorder (node) {
if (!) ( node = = null)) {
Inorder (Node.left)//First visit Zuozi
Putstr (Node.show () + ""), and then access the root node
Inorder (node.right)//Last access to right subtree
}
}

Subsequent traversal:

If the tree is empty, then the empty operation returns, otherwise from left to right before the leaves of the node in the way to traverse access to the left subtree, the last access to the root node.

The order of traversal is: H I D J E B K F G C A

Copy Code code as follows:

Subsequent traversal
function postorder (node) {
if (!node = = null) {
Postorder (Node.left);
Postorder (Node.right);
Putstr (Node.show () + "");
}
}

Implementing a two-fork lookup tree

The binary lookup tree (BST) is made up of nodes, so we define the node node object as follows:

Copy Code code as follows:

function Node (data,left,right) {
This.data = data;
This.left = left;//Save left node link
This.right = right;
This.show = Show;
}


Function Show () {
Return this.data;//display data saved in a node
}

Find maximum and minimum values

It is very simple to find the minimum and maximum values on the BST, because the smaller values are always on the left child node, and the minimum value is found on the BST, just traversing the left subtree until the last node is located

Find Minimum value

Copy Code code as follows:

function Getmin () {
var current = This.root;
while (!) ( Current.left = = null)) {
current = Current.left;
}
return current.data;
}

The method iterates along the left subtree of BST until it traverses to the leftmost node of BST, which is defined as:
Copy Code code as follows:

Current.left = null;

At this point, the value saved on the current node is the minimum value

Find Maximum Value

Finding the maximum value on the BST only requires traversing the right subtree until the last node is found, and the value saved on that node is the maximum.

Copy Code code as follows:

function Getmax () {
var current = This.root;
while (!) ( Current.right = = null)) {
current = Current.right;
}
return current.data;
}

Related Article

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.