Binary Tree)

Source: Internet
Author: User
Binary Tree Definition

Binary Tree definition: tree with a degree of 2.

Recursive definition of a binary tree: a binary tree, an empty tree, or a non-empty tree composed of a root node and two left and right trees that do not overlap with each other, the left and right Subtrees are all Binary Trees.

Binary Tree nature

1. layer I has at most 2 I-1 power nodes.

2. A binary tree with a depth of H has at most 2 H Power minus 1 node.

3. Each layer is full of Binary Trees calledFull Binary TreeThe tree with only a few knots missing on the right of the last layer is calledFull Binary Tree.

(The node numbers usually start from the root of a full binary tree and start from top to bottom from left to right ).

Important nature of a Complete Binary Tree:

1. the node number is I, the left child number is 2I, And the right child number is 2I + 1.

2. Except for the root node, the node number is I, and the label of its parent node is I/2 rounded down.

Binary Tree Storage Structure

Like a linear table, binary trees have two storage structures: sequence and link.

1. Sequential Storage Structure

Store a binary tree in sequence. First, number each node in the tree, and then store the values of each node in a one-dimensional array using the node number as the subscript.

The number of each node is the same as the number of the node corresponding to the full binary tree with the same depth, that is, the root number is 1, and then the numbers are numbered from left to right from top to bottom, the left and right children of the I node are 2I and 2I + 1.

Disadvantage: many storage locations are idle for Binary Trees with many single nodes.

2. Link Storage Structure

The common method used in link storage is to set three fields in each node: Value Field (used to store corresponding data elements), left pointer field and right pointer field.

Another method is to add a parent pointer field in the above structure (to facilitate searching for the parent node ).

The link storage structure without parent-child pointers is calledBinary linked listIt can be composed of independently allocated node links or element nodes in the array.

The types of independent nodes can be defined as follows:

struct BTreeNode{    ElemType data;    BTreeNode* left;    BTreeNode* right;};

The array element node can be defined

struct ABTreeNode{    ElemType data;    int left,right;};

In the second form, the left and right fields store the subscript of the unit where the left and right nodes are located, and are defined as integer.

The advantage of building a binary tree in an array is that after the establishment, you can write the entire array into the file and save it. If necessary, you can read it from the file into the array.

Binary tree traversal

A binary tree is composed of btreenode independent node links generated by dynamic allocation, and Bt is the pointer to the root node.

Forward Traversal
Void preorder (btreenode * BT) {If (BT! = NULL) {cout <Bt-> data <''; // access the root node. The operation depends on the preorder (BT-> left) of the application ); preorder (BT-> right );}}
Sequential Traversal
void InOrder(BTreeNode* BT){    if(BT != NULL)    {        InOrder (BT->left);        cout<<BT->data<<' ';        InOrder (BT->right);    }

Post-order traversal

void PostOrder(BTreeNode* BT){    if(BT != NULL)    {        PostOrder (BT->left);        PostOrder (BT->right);        cout<<BT->data<<' ';    }}

Traverse by Layer

You can traverse the nodes based on the layered structure of the binary tree, that is, you can access each node from top to bottom in the order of left to right.

The layer-by-layer traversal algorithm requires a queue. At the beginning, the root node of the entire tree is queued, and each time a node is deleted from the queue and the value of the node is output, the left and right child nodes of the queue are all put into the queue, so that the algorithm ends when the queue is empty.

// Traverse void levelorder (btreenode * BT) by layer {const int maxsize = 30; // define the length of the array used to store the queue // In this algorithm, the maximum length of the queue does not exceed the maximum number of nodes on the first layer of the binary tree. btreenode * Q [maxsize]; // defines the array space used by the queue. Int front = 0, rear = 0; // define the first pointer and tail pointer of the team. The first pointer is the empty team btreenode * P; If (BT! = NULL) // forward the root pointer to the queue {rear = (Rear + 1) % maxsize; Q [rear] = Bt;} while (front! = Rear) // execute the loop {front = (front + 1) % maxsize when the queue is not empty; // point the first line pointer to the first line element P = Q [Front]; // operate and delete the cout <p-> data <''; If (p-> left! = NULL) // If the left child exists, the node pointer enters the queue {rear = (Rear + 1) % maxsize; Q [rear] = p-> left ;} if (p-> right! = NULL) // if the right child exists, the node pointer enters the queue {rear = (Rear + 1) % maxsize; Q [rear] = p-> right ;}} // while end}

Reprinted from:

Http://www.cnblogs.com/mengdd/archive/2012/10/31/2748760.html

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.