Hierarchical nonlinear structure - tree 03

Storage structure and to achieve binary tree

1. The binary sequence structure

A set of contiguous memory unit, storing sequentially in numerical order complete binary tree nodes.

1) Analysis of the relationship between the nodes: it is known that a number of the node, then the number can be extrapolated related, therefore, only the storage nodes can be numbered;
2) from junctions Analysis: Because the number of consecutive nodes , and therefore can do with it index vector is stored. Since the C language specified array index must start from zero, without this unit, and let the root node is stored at index position 1, so that the number of trees with the subscript position up on correspondence.

For a general binary tree, the binary tree form a completely filled if those nodes are missing, they may be numbered according to the numbering complete binary tree, the logical order of the node reflecting the relationship between the node.
If not a binary tree complete binary tree form, in order to maintain the relationship between nodes and had vacated many elements, resulting in a waste of space. To be allocated based on the actual number of nodes in the storage space, the list structure may be employed.

Here Insert Picture Description

Linear representation of the tree

1. The bracket notation tree
parentheses expressed as A (B (D, E) , C (F)).
Here Insert Picture Description
1) The algorithm is described
from left to right scan string tree parentheses represent:
① encountered left parenthesis, before a node onto the stack;
② right parenthesis encountered, the top element from the stack. DESCRIPTION top element in the subtree rooted structure is completed, the end of this time, if the stack is empty, the algorithm;
③ encountered node, then it must be the top element for the child, the child which is linked to the respective position of the top element on;
④ encounters a comma, skipped.

2. The tree representation of the layer number of
a hierarchical tree representation order: 1A, 2B, 3D, 3E , 2C, 3F.

1) algorithmic descriptions
set i, j is a neighbor node layer number, node i to node j, i and j before
① if i> j, i j is located on the lower level, and just the first j children;
② if i = j, two nodes in the same layer, they have a common parent;
③ if i <j, then i, j an ancestor node of each other brothers, i find the roots of the direction of parents j brothers, to find their common parent.

2. Binary Tree chain structure

Binary list - each node of the binary tree contains two pointers pointing to the domain corresponding branch.
Here Insert Picture Description
Data structure description:

typedef struct node{
     datatype data;
     struct node *lchild,*rchild;
 }bitree;

Trigeminal chain store

In addition to children around outside, plus the parents address, constitutes a binary tree of the trigeminal chain store.
Here Insert Picture Description

Triple list

Record left children, parents, brothers and right.
Here Insert Picture Description

Static binary list

An array storage node contact list.
Here Insert Picture Description

3. Establish a dynamic binary list

Creating a level binary input method list:

Dynamically created all nodes of the tree, the node address of the node numbers sequentially filled by Q array sequentially fill the left and right child pointer field of each node. For example:
Here Insert Picture Description
Here Insert Picture Description
Note: The algorithm of FIG stop condition, i <+1 reciprocal number of nodes all over the second layer.

Here Insert Picture Description
Create a hierarchy of binary input method two lists:

Input node information, if the input node is not a virtual node, a new node is established. If the new node is a first node, then make it as root, otherwise the new node as a child is linked to its parent node. Until the end of the input symbol "#" so far.
Here Insert Picture Description
Here Insert Picture Description
Program:

/*===============================================
函数功能:层次输入法创建二叉链表
函数输入:无
函数输出:二叉链表根结点
键盘输入:按完全二叉树结点编号规则顺序输入结点值,空结点为@
====================================================*/
BinTreeNode *Q[16]; //队列Q放树结点地址
BinTreeNode *CreatBTree()
{ 
    char ch;
    int front=1,rear=0;
    BinTreeNode *root = NULL, *s;
    ch=getchar();
    while(ch!='#')   //结束标志
    {   
        s=NULL;
        if (ch!='@')  //空结点
        {    
              s=(BinTreeNode *)malloc(sizeof(BinTreeNode)); //生成新结点
              s->data=ch; 
              s->lchild=NULL;  
              s->rchild=NULL;
        }
          Q[++rear]=s;  //结点入队
          if (rear==1) root=s; //记录根结点
          else
          {  
              if (s && Q[front])
              {  
                  if (rear%2==0) Q[front]->lchild=s; //左孩子入队
                  else Q[front]->rchild=s;  //右孩子入队
              }
              if (rear%2==1)  front++; //新结点是双亲的右孩子,则双亲结点出队            
          }
      ch=getchar();
    }
    return root;
}

represents a front position of each cycle root; REAR decided if the first position in the array, the second loop found if the left child or right subarray, the third value if End Analyzing Ruoyi assigned to the right child array; let root location plus 1.

Published 26 original articles · won praise 3 · Views 1470

Guess you like

Origin blog.csdn.net/herui7322/article/details/104174159