The nature of binary tree two fork tree storage structure Traversal binary tree C realizes two fork tree creation and traverse clue two fork Tree

Source: Internet
Author: User

definition

Binary tree is a finite set of n (n>=0) nodes, which is an empty set called an empty binary tree, or has a root node and two separate left child trees and right child trees, called root nodes, which are disjoint.

two characteristics of the fork tree
  • Each node has a maximum of two subtrees trees, so the total number of binary trees is no more than 2 nodes
  • Saozi right subtree is ordered, the number of times can not be arbitrarily reversed
  • Even if a node in the tree has only one subtrees tree, it is Zuozi or right subtree to distinguish.
Special two-fork tree1. Diagonal Tree

All the nodes are only Zuozi two-fork tree called Left oblique tree;
All nodes are only the right subtree of the two-fork tree called the Right oblique tree;
The two are referred to as oblique trees.

2. 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.

Characteristics

  • Leaves can only appear in the next layer, it is impossible to strike a balance in other layers.
  • The degree of non-leaf junction must be 2.
  • In the same depth of the two-fork tree, the number of nodes full of two forks is the largest and the number of leaves is the largest.
3. Complete binary Tree

For a two-prong tree with n nodes numbered in the sequence, if the nodes numbered I (1 <= i <= N) and the nodes numbered I in the full two-tree with the same depth are positioned exactly the same in the binary tree, the binary tree is called a complete binary tree.

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

Characteristics

  • Leaf nodes can only come out on the bottom two levels.
  • The lowest-level leaves must be concentrated in the left-hand continuous position.
  • The bottom two layer, if there is a leaf node, must be in the right continuous position
  • If the node degree is 1, then the node has only the left child, that is, there is no right subtree.
  • Two forks of the same number of nodes, with the smallest depth of the full binary tree
Two The nature of the fork tree

Property 1

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

Property 2

Two-fork tree with a depth of k up to 2^ (k)-1 nodes (k>=1)

Property 3

for any binary tree T, if its terminal node is N0, the number of nodes with a degree of 2 is n2 n0 = n2 + 1
The total number of tree T nodes is n=n0+n1+n2.
Branch Node count = n-1 = n1 + 2*n2

Property 4

Full binary tree with n nodes has a depth of └log2^n┘+ 1 (└x┘ represents the largest integer not greater than X)

Property 5

If a complete binary tree with n nodes (with a depth of └log2^n┘+ 1) nodes are numbered by sequence (from 1th to └log2^n┘+ 1, each layer from left to right) to either node I (1 <= i <=n).

  1. If I=1, then the node i is the root of the two fork tree, no parents; if i>1, the parent node is: └i/2┘
  2. If 2i>n, then node I no left child (node i is the leaf node); otherwise the left child is node 2i.
  3. If 2i+1>n, then node I no right child, otherwise its right child is node 2i+1
Two the storage structure of the fork tree1. Sequential storage structure

Because of the strict definition of binary tree can use one-dimensional array to store the nodes in the binary tree, and the storage location of the node, that is, the subscript of the array to be able to withdraw the logical relationship between the nodes, such as the relationship between parents and children, left and right sibling relations.

For a typical two-fork tree, although the sequence number does not reflect a logical relationship, the nonexistent node can be set to ^.

Sequential storage structures are generally used only for complete binary trees

2. Two-fork linked list

Binary tree Each node has more than two children, so he designed a data field and two pointer fields is a more natural idea, we call this list as a two-linked list.

left child pointer field data fields Right child pointer field
Lchild Data Rchild
traversing a binary tree

The traversal of a binary tree refers to (traversing binary trees): Starting from the root node, accessing all nodes in a binary tree in some order, making each node accessible once and only once.

1. Pre-sequence traversal

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

2. Middle Sequence traversal

If the binary tree is empty, then an empty operation is returned, otherwise starting from the root node ( not first accessing the root node ), the middle sequence traverses the left subtree of the root node, then accesses the root node, and the final middle sequence traverses the right subtree.

3. Post-Traversal

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

4. Sequence traversal

If the binary tree is empty, then the empty operation returns, otherwise, first from the first layer of the tree, that is, from the root node to access, from top to bottom layer by page access, in the same layer, from left to right in order to access the node one by one.

The four kinds of traversal methods are the linear sequence of the nodes in the tree, which brings the benefit to the realization of the program.

code Implementation Two cross-tree creation and traversal
/ * Two binary tree-chained storage structure to achieve two fork tree creation and traversal traversal and creation are divided into three pre-order, middle order, sequential input data reference <ab D C > Space is to be entered * /#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR 0;typedef intStatus;typedef CharTelemtype;//Node definitiontypedef structBitnode {telemtype data;//Data fields    structBitnode *lchild, *rchild;//Left child hands}bitnode, *bitree;//Pre-order traversal (previous order traverse)voidPreordertraverse (Bitree T) {if(T = = NULL) {return; }printf("%c", T->data);//The first step shows the node data.Preordertraverse (T->lchild);//Second step forward sequence traversal left sub-treePreordertraverse (T->rchild);///third-step sequence traversal right subtree}//middle order Traversal (intermediate order Traverse)voidInordertraverse (Bitree T) {if(T = = NULL) {return; } inordertraverse (T->lchild);//The first step in the sequence to traverse the left subtree    printf("%c", T->data);//The second step shows the node data.Inordertraverse (T->rchild);//Step through the right sub-tree in the third sequence}//post-order traversal (Traverse)voidPostordertraverse (Bitree T) {if(T = = NULL) {return;    } postordertraverse (T->lchild); Postordertraverse (T->rchild);printf("%c", t->data);}//Two The establishment of a fork tree the value of the node in the binary tree by the preamble (one character)//' Indicates an empty tree let each node determine if there are children left and right, construct a two-fork list indicating a binary tree//If the input sequence is in order or sequential, the Code 66 67 68 line order to change, you can refer to the traversal of the code here is not implemented.voidCreatebitree (Bitree *t) {Telemtype e;scanf("%c", &e);if(E = ="') {*t = NULL; }Else{*t = (bitree)malloc(sizeof(Bitnode));//Generate Nodes        if(! (*t)) {//Create failed            printf("Node request failed \ n");return; } (*t)->data = e;//Assign a value to a node.Createbitree (& (*t)->lchild);//Construct left sub-treeCreatebitree (& (*t)->rchild);//Construct right sub-tree} }intMainvoid) {Bitree T; Telemtype e;intoption =1;printf("\ n 1. Pre-order Create two fork tree \ n 2. Pre-order traversal binary tree \ n 3. Middle sequence traverse binary tree \ n 4. Post-traverse binary tree \ n 0. exit \ n"); while(option) {scanf("%d", &option); Fflush (stdin);//Flush buffer This is a must, or it will make you wonder when you create the data.        Switch(option) { Case 1: Createbitree (&t);printf("binary tree creation succeeded \ n"); Break; Case 2: Preordertraverse (T); Break; Case 3: Inordertraverse (T); Break; Case 4: Postordertraverse (T); Break; Case 0:returnOK; }    }returnOK;}
注意: 建立的时候 每个结点确定是否有左右孩子,如果没有则用空格代替

Disadvantages

浪费空间,有很多的空指针域 最有有n+1个空指针

two the establishment and traversal of a fork tree is the principle of recursion.
The establishment is in the original printing node place, changed to the generation node, to the node assignment operation.

n nodes have 2n pointer fields, and N nodes have a total of n-1 branch lines in the two forks, so the null pointer field = 2n-(n-1) = N+1

Clue two fork Tree

A pointer to a precursor and a successor is called a Clue, and a two-linked list of clues is called a clue list, and the corresponding two-tree is called the Clue two fork tree

In order to solve the problem of waste in the two-fork-tree hollow pointer field, the null pointer is used to point to the precursor and the successor of the idea called the Clue two fork tree.

Advantages

easy to find nodes.
high efficiency of traversing binary tree
It is convenient for the precursors and successors of the super-finding nodes.

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

Two fork Tree two tree nature storage structure Traversal binary tree C implements two fork tree creation and traverse thread two fork tree

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.