Data structure (III): Non-linear Logical Structure-binary tree and data structure Binary Tree

Source: Internet
Author: User

Data structure (III): Non-linear Logical Structure-binary tree and data structure Binary Tree

Next, let's start an in-depth review and summary of the binary tree on the content of the non-linear Logical Data Structure Tree. First, let's review several important concepts:

I. Review 1. Full binary and full binary

A full Binary Tree indicates that all nodes except leaf nodes have two subnodes. In this way, we can easily calculate the depth of a full binary tree. We need to master some properties of a full binary tree.

The full binary tree is inherited from the full binary tree, which means that all nodes are arranged from top to bottom and from left to right. The binary tree is called a full binary tree. Therefore, we can imagine that for the depth of the Complete Binary Tree h, the front h-1 layer can constitute the depth of the full binary tree for the H-1, and for the h layer is arranged from left to right consecutively. When the binary tree storage structure described later adopts the sequential structure, a complete binary tree structure is used to record that a binary tree node has a precursor and two successor nodes and facilitate insertion, use unused nodes as virtual nodes, build full binary trees, and store them from top to bottom and from left to right. Therefore, it is very important to understand the performance of full Binary Trees. Of course, the chain structure is more suitable for binary tree storage, which will be introduced separately later.

With this feature, a Complete Binary Tree with 553 nodes can easily calculate its depth and number of nodes at each layer:

553 = (512-1) + 42, so its depth is 10, and the number of nodes on the 10th layer is 42, the first nine layers of the binary tree structure constitute a full binary tree with a depth of 9.

2. Binary Tree Storage

As mentioned above, the ordered storage structure of Binary Trees is structured according to the complete binary tree structure. The chain storage structure is described here.

It is natural to store a binary tree using a chain storage structure. In addition to data fields, the left and right pointer fields are set for the node to store the addresses of the left and right children of the node respectively. The binary tree chain storage structure becomes a binary linked list. Root is the pointer to the root, and root is NULL when the tree is empty.

Binary Tree binary linked list storage structure:

Typedef struct tnode

{

DataType data;

Struct tnode * left, * right;

} TNode;

When the left child node is absent, left = NULL. Similarly, when the right child node is absent, right = NULL. Therefore, from the perspective of insertion, deletion, and search, using a chain storage structure is much better than using a sequential storage structure. Therefore, in the future, the ordered storage structure of Binary Trees will only be of comparative significance. In practice, the chain storage structure will be used.


Ii. binary tree traversal

The nodes in the binary tree are visited along a search path, so that each node is accessed once and only once. A linear arrangement of all nodes in the tree is obtained, normalize the node of the tree (define an order ). "Access" can have a wide range of meanings, such as output node information. Traversal is the foundation of algorithms. In a linear structure, the traversal order is the same as the logic order. Therefore, traversal is not studied separately. Non-linear traversal is complicated and there are more than one method, in addition, the algorithm is directly associated with a certain traversal, so the algorithm design and traversal are studied together.

Hierarchical traversal: access each node from top to bottom and left to right. The nodes are as follows:


First (first) traversal: first visit the root node, and then traverse the Left and Right sub-trees in order;
Middle-order traversal: first, the middle-order traversal of the Left subtree, then access the root node, and finally the Middle-order traversal of the right subtree;
Post-order traversal: traverses the Left and Right sub-trees sequentially, and then accesses the root node.

For the simple binary tree shown in, different traversal methods are used to obtain the linear arrangement:


Hierarchical traversal sequence: A B C D

First-order traversal sequence: A B D C
Sequential traversal sequence: B D A C
Post-order traversal sequence: D B C

Iii. binary tree traversal algorithm 1. Recursive Algorithm

The Recursive Algorithm for first-order traversal is as follows:


<span style="font-family:SimSun;font-size:18px;"><span style="font-size:18px;">void PreOrder(CNode *pNode){if(pNode!=NULL){printf("%lf\t",pNode->data);PreOrder(p->lchild);PreOrder(p->rchild);}}</span></span>
Then the recursive algorithm for sequential traversal is as follows:

<span style="font-family:SimSun;font-size:18px;"><span style="font-size:18px;">void PreOrder(CNode *pNode){if(pNode!=NULL){PreOrder(p->lchild);                        printf("%lf\t",pNode->data);PreOrder(p->rchild);}}</span></span>
Similarly, the recursive algorithms for subsequent traversal are as follows:

<span style="font-family:SimSun;font-size:18px;"><span style="font-size:18px;">void PreOrder(CNode *pNode){if(pNode!=NULL){        PreOrder(p->lchild);PreOrder(p->rchild);                        printf("%lf\t",pNode->data);}}</span></span>
2. recursive algorithm Evaluation

Recursive functions have a clear structure and concise code, which hides complicated details. Many algorithms are defined by recursion, making it easier to implement recursion. However, recursive functions have the following limitations:

(1) low execution efficiency;

(2) A recursive call may generate a layer-by-layer recursion, and the continuous pressure Stack may be out of the range of available stack space;

(3) The parameters of recursive functions are special, usually more than those required by non-recursive functions;

(4) recursive design is not an object-oriented method;

(5) recursive calls are not allowed in some languages.

Recursion can be used when algorithm design is emphasized and runtime requirements for time and space are reasonable.

For how to implement loops using recursion and iteration, refer to another blog article titled iteration is human, recursion is God (Summary of iteration and Recursion: comparison). detailed analysis and comparison of iteration and Recursion are provided.

**************************************** **************************************** **************************************** **************************************** *******

2015-8-2

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.