Data Structure (iii): Nonlinear logic structure-two fork tree

Source: Internet
Author: User

The last time, the content of the nonlinear logical data structure tree is opened, and the deep review and summary of the two-fork tree is started. First, let's review a few important concepts:

First, review1. Full two fork tree with complete binary tree

A two-prong tree means that all nodes except the leaf node have two child nodes. This makes it easy to calculate the depth of a two-fork tree, and to master some of the properties of a two-fork tree.

The complete binary tree is inherited from the full two-tree, referring to all the nodes from top to bottom, from left to right in order of the rank of the two-tree is called the complete binary tree. So it can be imagined that for a complete binary tree with a depth of H, the front h-1 layer can form a full two-fork tree with a depth of h-1, whereas for the H-layer it is continuously arranged from left to right. The storage structure of the two-fork tree is described later when using sequential structure, in order to be able to record the binary tree node has a precursor and two successive nodes, as well as convenient insertion, such as the use of a complete binary tree structure, will not have nodes as virtual nodes, build full two fork tree, and then from the top to the bottom, from left to right Therefore, it is very important to understand the performance of a two-fork tree. Of course, for binary tree storage, the use of chain-type structure will be more appropriate, will be introduced separately.

With this nature, for a complete binary tree with a node number of 553, it is easy to calculate its depth with the number of nodes per layer:

553 = (512-1) + 42, so it has a depth of 10, while the number of nodes in the 10th layer is 42, and the 9 fork tree structure of the first two layers constitutes a full 9 fork tree with a depth of two.

2. Storage of two fork trees

The sequential storage structure of the two-fork tree is described earlier, and is arranged according to the structure of the complete binary tree. This explains the chain-store structure.

It is natural to store the binary tree with a chained storage structure, in addition to the data domain, the left and right two pointer fields are set up, respectively, to hold the address of the child of that node. The chain storage structure of the binary tree becomes a binary linked list. Where Root is a pointer to the root, and the empty tree is the root null.

Binary tree two-fork linked list storage structure:

typedef struct TNODE

{

DataType data;

struct Tnode * left, * right;

}tnode;

When the left child node is not in the same order, then Ieft = null, in the same vein, right = NULL when the child node is not there. Therefore, the use of chained storage structures is much better than using sequential storage structures from various angles, such as insert, delete, and find. Therefore, in the future, the sequential storage structure of binary tree is only of comparative significance, and the chain storage structure is used in practice.


two or two traversal of a fork tree

Follow a search path to the nodes in the binary tree so that each node is accessed once, and only once, to get a linear arrangement of all nodes in the tree, to linearization the nodes of the tree (define a sequence). The meaning of "access" can be very wide, such as: output node information and so on. Traversal is the basis of the algorithm, in the linear structure, the order of traversal is the same as the logical order, so the traversal is not studied alone, and the nonlinear traversal is more complex, more than one method, and the algorithm is directly associated with some kind of traversal, so the algorithm design and traversal linked together.

Hierarchical traversal: Access nodes from top to bottom, left to right, as follows:


First Order (pre-order) traversal: first accesses the root node, then sequentially traverses the Zuozi and right subtree respectively;
Middle Sequence traversal: first the middle sequence traverses the left subtree, then accesses the root node, and finally the middle sequence traverses the right subtree;
Post-order traversal: The sequence traverses the left and right subtrees, then accesses the root node.

For the simple two-fork tree shown, a different traversal method is used to get the linear arrangement, respectively:


Hierarchy traversal sequence: A B C D

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

traversal algorithm of three or two-fork tree1. Recursive algorithm

Then the recursive algorithm for the 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>
so the recursive algorithm for the middle sequence 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 algorithm for subsequent 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); Preorder (p->rchild);                        printf ("%lf\t", Pnode->data);}} </span></span>
2. Evaluation of recursive algorithms

Recursive function structure is clear, the code is concise, concealed the complex details, and many algorithms because it is recursive definition, so the recursive implementation is more direct and convenient, but the recursive function has the following disadvantages:

(1) Low implementation efficiency;

(2) A recursive call may result in one layer of recursion, and a continuous stack may exceed the range of available stack space;

(3) The parameter table of the recursive function is special, usually more than the parameters required by the non-recursive function;

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

(5) Some languages are not allowed to use recursive calls.

Recursion is used when emphasis is placed on the design of the algorithm and the time and space requirements of the runtime are reasonable.

For the two ways of implementing loops using recursion and iteration, you can refer to my other post, "Iteration is human, recursion is God (summary of iteration and recursion: comparison)", with detailed analysis and comparison of iterations and recursion.

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

2015-8-2

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data Structure (iii): Nonlinear logic structure-two fork tree

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.