[Data structure-trees and forests]

One, the storage structure of the tree

(1) Parental representation

achieve:Define a structure array to store the nodes of the tree, each node contains two fields:
Insert picture description hereData field: store the information of the node itself
Parent field: indicate the position of the parent node of the node in the array

Features: It’s easy to find parents but difficult to find children

Insert picture description here


#define MAX_TREE_SIZE 100
typedef struct PTNode
{
    
    //结点结构
	ElemType data;
	int parent;//保存双亲位置
}PTNode;

typedef struct
{
    
    //定义树结构
	PTNode nodes[MAX_TREE_SIZE];
	int r, n;//根的位置和结点数
}PTree;

(2) Child linked list notation (child linked list with parents-parent)

Insert picture description here

//孩子链表表示法的类型描述
typedef struct CTNode
{
    
    
	int child;
	struct CTNode* nextchild;
}*ChildPtr;
//双亲结点结构
typedef struct
{
    
    
	ElemType data;
	int parent;//保存双亲位置
	ChildPtr firstchild;//孩子链的头指针
}CTBox;
//树结构
typedef struct
{
    
    
	CTBox nodes[MAX_TREE_SIZE];
	int n, r;//结点数和根结点的位置
}CTree;

(3) The storage representation of the binary linked list of the tree (child-brother)

Insert picture description here

Insert picture description here

/*树的二叉链表类型描述*/
typedef struct CSNode
{
    
    
	ElemType data;
	struct CSNode* firstchild, * nextsibling;
}CSNode,*CSTree;

Second, the conversion between forest and binary tree

(1) Convert the tree into a binary tree

Add line: Add a connection between brothers
Wipe the thread: For each node, except for its leftmost child, remove its relationship with the rest of the children
Rotate: With the root node of the tree as the axis, rotate the whole tree 45 degrees clockwise.

Insert picture description here
The binary tree converted from the tree, the right subtree of the root node must be empty

(2) Convert a binary tree to a tree

Add line: If the node p is the left child of the parent node, then the right child of P, the right child of the right child...all the right children found along the branch, are connected to the parents of p with a line
Wipe the thread: Erase the connection between the parent and the right child in the original binary tree
Adjustment: Arrange the nodes hierarchically to form a tree structure
Insert picture description here

(3) Convert the forest into a binary tree

1. Convert each tree into a binary tree respectively
2. Connect the root nodes of each tree with a line
3. Use the root node of the first tree as the root of the binary tree, and then use the root node as the core, clockwise Rotate to form a binary tree structure

Insert picture description here

(4) Convert a binary tree into a forest

Wipe the thread: Erase the connection between the root node of the binary tree and its right child, and all the connections between the right children found along the right branch to make it an isolated binary tree
reduction: Restore the isolated binary tree to a tree
Insert picture description here

Third, the traversal of trees and forests

Traverse: Walk through the vertices of the tree according to a certain rule, and make each vertex be visited only once, that is, find a complete and regular walking method to obtain a linear arrangement of all nodes in the tree

(1) Tree traversal (do not discuss middle order)

Common methods:
1. Root-first (order) traversal: visit the root node of the tree first, and then traverse each subtree
of the root first in turn 2. Root (order) traversal: traverse each subtree in turn, then root, and then visit the root node
3. Traverse by level: first visit the nodes on the first level, and then traverse the second level, ..., and the Nth level nodes in turn.

Insert picture description here

(2) The traversal of the forest (do not discuss the post sequence)

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46518461/article/details/109395052