Binary Tree Summary

Source: Internet
Author: User

The concept of binary tree

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

Characteristics of Binary Tree

Each node has a maximum of two subtrees trees, so there are no nodes in the binary tree that are more than 2. Each node in the binary tree is an object, and each data node has three pointers, each pointing to the parent, the left child, and the right child. Each node is connected to each other by pointers. The relationship between the linked pointers is a parent-child relationship.

Definition of binary tree node

The binary tree node is defined as follows:

struct BinaryTreeNode{    int m_nValue;    BinaryTreeNode* m_pLeft;    BinaryTreeNode* m_pRight;};
Five basic forms of binary tree
空二叉树只有一个根结点根结点只有左子树根结点只有右子树根结点既有左子树又有右子树

There are only two cases of a normal tree with three nodes: two or three layers. But because the binary tree to distinguish between the left and right, so it will become the following five kinds of forms:

Special binary Tree Oblique tree

As shown in the 2nd and 3 thumbnails of the top-down first figure.

Full two fork Tree

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

Complete binary Tree

A complete binary tree means that the last layer on the left is full, the right side may be full or dissatisfied, and the remaining layers are full. A two-tree with a depth of k and a node number of 2^k-1 is a full two-tree (full binary tree). It is a tree with a depth of k and no vacancy.

The features of the complete binary tree are:

叶子结点只能出现在最下两层。最下层的叶子一定集中在左部连续位置。倒数第二层,若有叶子结点,一定都在右部连续位置。如果结点度为1,则该结点只有左孩子。同样结点树的二叉树,完全二叉树的深度最小。

Note: A full two fork tree must be a fully binary tree, but a complete binary tree is not necessarily full of two forks.

The algorithm is as follows:

bool is_complete(tree *root)  {      queue q;      tree *ptr;      // 进行广度优先遍历(层次遍历),并把NULL节点也放入队列      q.push(root);      while ((ptr = q.pop()) != NULL)      {          q.push(ptr->left);          q.push(ptr->right);      }      // 判断是否还有未被访问到的节点      while (!q.is_empty()) { ptr = q.pop(); // 有未访问到的的非NULL节点,则树存在空洞,为非完全二叉树 if (NULL != ptr) { return false; } } return true; } 
The nature of binary tree

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

The nature of Binary tree two: two-fork tree with a depth of k 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 a 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 applicability of sequential storage is not strong, then we need to consider the chain storage structure. The storage of binary tree is generally used as a chain-store structure in accordance with international practice.

The binary tree has a maximum of two children per node, so designing a data field and two pointer fields for it is a natural idea, and we call this a linked list called a two-cross list.

Traversal of a binary tree

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

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

Pre-order Traversal:

If the binary tree is empty, then an empty operation is returned, otherwise the root node is accessed first, then the left subtree is traversed before the sequence, and then the right subtree is traversed.


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

//先序遍历function preOrder(node){ if(!node == null){ putstr(node.show()+ " "); preOrder(node.left); preOrder(node.right); }}
Middle Sequence Traversal:

If the tree is empty, then an empty operation is returned, 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, and then accesses the root node, and the final middle sequence traverses the right subtree.


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

//使用递归方式实现中序遍历function inOrder(node){ if(!(node == null)){ inOrder(node.left);//先访问左子树 putstr(node.show()+ " ");//再访问根节点 inOrder(node.right);//最后访问右子树 }}
Post-post traversal:

If the tree is empty, then an empty operation is returned, otherwise the left-to-right leaves the back node of the way to traverse the tree, and finally access the root node.

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

//后序遍历function postOrder(node){ if(!node == null){ postOrder(node.left); postOrder(node.right); putStr(node.show()+ " "); }}
Implement a two-fork find tree

Binary lookup tree (BST) is made up of nodes, so we define a Node node object as follows:

function Node(data,left,right){    this.data = data; this.left = left;//保存left节点链接 this.right = right; this.show = show;}function show(){ return this.data;//显示保存在节点中的数据}
Find maximum and minimum values

Finding the minimum and maximum values on BST is simple, because the smaller values are always on the left child node, and the minimum value is found on BST, just traverse the left subtree until you find the last node

Find Minimum value
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 the leftmost node of BST, which is defined as:

null;

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

Find Maximum Value

Finding the maximum value on BST only needs to traverse the right subtree until the last node is found, and the value saved on that node is the maximum value.

function getMax(){    var current = this.root; while(!(current.right == null)){ current = current.right; } return current.data;}

Binary Tree Summary

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.