Data Structures-tree and forest representations and traversal

Source: Internet
Author: User
Tags dashed line

Parent representation (sequential storage structure)
      用一组连续的存储空间来存储树的结点,同时在每个结点中附加一个指示器(整数域) ,用以指示双亲结点的位置(下标值) 。数组元素及数组的类型定义如下:
#define MAX_SIZE  100typedef  struct PTNode{  ElemType  data ;int  parent ;}PTNode ;
typedef  struct{  PTNode  Nodes[MAX_SIZE] ;int  root;    /*  根结点位置  */int  num ;   /*  结点数   */ }Ptree ;

Shown is the storage structure represented by a tree and its parents. This storage structure takes advantage of the unique nature of the parent node of any node. The parent node of either node can be easily found directly, but the entire array needs to be scanned when the node's child nodes are evaluated.

Child linked list notation

2 Child linked list notation
Each node in the tree has multiple pointer fields, and each pointer points to its root node of a subtrees tree. There are two kinds of node structures.
⑴ fixed-length node structure
The number of pointer fields is the degree of the tree.
Its characteristic is: the structure of the list is simple, but when the degree of each node in the tree differs greatly, the waste of the pointer domain is obvious. But if the degree of each node of the tree is very small, the open space is fully utilized, and the disadvantage of this storage structure becomes the advantage.
Node structure

⑵ Indefinite long node structure (on Demand)
Each node in the tree has a different number of pointer fields, which is the degree of the node. There is no extra pointer field, but the operation is inconvenient.
Advantage: Increased space utilization
Disadvantage: The link list structure of each node is not the same, it is difficult to realize;
Secondly, to maintain the value of the node's degree, the computation will lead to the loss of time.

⑶ Composite Linked list structure
(Sequential storage + chained storage)
This is done by placing all the nodes in an array stored sequentially, and then arranging the children nodes of each node, using a single-linked list as the storage structure (because the number of children per node is uncertain).

    n个结点的树有n个(孩子)单链表(叶子结点的孩子链表为空);   n个头结点又组成一个线性表,采用顺序存储结构,存放进一个一维数组中。

Design two types of junction structures:
Header node of table header array: (FirstChild the head pointer of the child linked list that stores the node);
The table node of the child's list: (Childno is used to store a node's subscript in the table header array, next refers to the next child).

Data structure type definition

The data structure type is defined as follows:

#define max_node 100  typedef  struct  listnode{int     Childno; /* child node number */ struct  listno *next;}    Ctnode; /* table node structure */  typedef  struct  {Elemtype data; Ctnode *firstchild;}    Hnode; /* head node structure */  
typedef  struct{  HNode   nodes[MAX_NODE] ;int  root;    /*  根结点位置  */int  num ;   /*  结点数   */}CLinkList;    /*  头结点结构  */复合链表结构点评:优点:便于查找结点的孩子或结点的兄弟缺点:查找某结点的双亲需遍历整棵树        
Child Brother Representation (binary tree notation)

3 Children Brother Representation (binary tree representation)

typedef  struct   CSnode{  ElemType   data ;struct   CSnode *firstchild, *nextsib ;}CSNode;  

Child Brother notation Reviews:
A child who is easy to find a node.
By FirstChild find the eldest son of the knot, and then find the second son through the nextsib of the eldest son. Until you find the child you are looking for.
Hard to find parents
Workaround: Add a parent parent pointer field

Forest and two-fork tree conversion

From the physical structure, the tree and the two-fork tree of the two-linked list is the same, but the logical interpretation of the pointer is different.
From the definition of the tree's two-linked list, it is known that the right subtree of any two-tree corresponding to a tree must be empty.

The tree is converted into a binary tree
For the general tree, it can be conveniently converted to a unique two-fork tree corresponding to it. The detailed steps are:
⑴ with dashed lines. Each layer of the tree is connected by a dashed line between the sibling nodes in the order from left to right.
⑵ to connect. In addition to the leftmost first child node, the parent node is removed from all other child nodes.
⑶ rotation. Rotate the tree clockwise by 450, and the original solid line is left oblique.
⑷ integral type. Changes all dashed lines in the tree after rotation to a solid line and oblique to the right

3 forest converted into binary tree
When the normal tree is converted into a binary tree, the right sub-tree of the binary tree must be empty. If the root node of the second tree (converted to a binary tree) in the forest is the sibling node of the root node of the first tree (the binary tree), the conversion steps for converting the forest into a binary tree are as follows:
1. Convert each tree to a two-fork tree
2, according to the order of the trees given in the forest, the first tree does not move, starting from the second tree, in turn, the root node of the latter tree as the root node of the former binary tree of the right child, with the line up, when all the two-fork tree connected, the forest has been converted from the two fork tree.

The traversal of trees and forests

1 Traversal of the tree
As defined by the tree structure, there are two ways to traverse a tree.
⑴ Sequence Traversal: Accesses the root node first, then sequentially iterates through each subtrees tree. Tree, the sequence of first order traversal is:
⑵ post-order traversal: sequential traversal of each subtrees tree, and then access the root node. Tree, the sequence of sequential traversal is:
The first-order traversal of a tree is essentially the same as the first-order traversal of a two-fork tree after the tree is converted to a binary tree.
The post-order traversal of a tree is essentially the same as converting a tree into a binary tree after a two-fork tree.
2 Traversal of the forest
Set F={T1, T2,?, Tn} is a forest and there are two ways to traverse F.
⑴ Sequential traversal: Iterates through each tree in the first sequence, sequentially traversing the tree.
⑵ sequence traversal: Iterates through each tree in F in the same way as the sequential traversal tree.

Data Structures-tree and forest representations and traversal

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