Parents notation, notation children, child Brothers notation (binary notation), the binary tree and forest conversion

First, parents notation

Implementation: definitionsArray of structuresStorage node of the tree, each node contains two fields:
Data Domain: Data storage node itself.
Parents domain: This indicates the position of the node of the parent node point in the array.

Node structure represented as:

data parent

data is the data field, parent pointer field is
for example:
Here Insert Picture Description
can be expressed as:
Here Insert Picture Description
r=0,n=10.(Root node number and position)

Such a storage structure, according to the parent node pointer is easy to find its parent node, with the time complexity is O (1), until the parent is -1, find the root of the tree, but we need to know the node What child is, I'm sorry, please traverse the entire structure of the job.

So parents representation is characterized by:Easy to find parents, children find difficult.

Node Structure:

typedef struct PTNode{
	int data;
	int parent;//双亲位置域
}PTNode;

Tree structure definition:

#define MAX_TREE_SIZE 100
typedef struct{
	PTNode nodes[MAX_TREE_SIZE];
	int r,n;//根结点的位置和结点个数
}PTree;

We can expand the nodes to solve the problem to find the child node, but the design of storage structures is a very flexible process. A storage structure design is reasonable, depending on the storage structure based on operational suitability, convenience, time complexity is good and so on.

Second, children notation (children list)

The concrete method is: the child nodes of each node are arranged together, as is a linear table in memory as a single linked list structure, the n-th node has child list n (the leaf node of the child list is empty) and n and head pointer table consisting of a linear, sequential storage structure, deposited into a one-dimensional array , shown in FIG.
Here Insert Picture Description

To this end, two kinds of design node structure, a child is a child node of the list:

child next

child data field is used to store a node index in the header array, next pointer field, a pointer to the next child node of a storage node point.

Another header is the header node of the array, i.e. the parent node:

data first child

Wherein, data is a data field, the head pointer is firstChild field, the head pointer stored in the child node list.

Child shows the structure of custom code law:

Child node structure


typedef struct CTNode{//孩子结点
	int child;
	struct CTNode *next;	
}*ChildPtr;

Header Structure (parent structure)

typedef struct		//表头结构
{		
	int data;
	ChildPtr firstchild;
}CTBox;//孩子链表

Tree:

#define MAX_TREE_SIZE 100
typedef syruct		//树结构
{
	CTBox nodes[MAX_TREE_SIZE];//结点数组
	int n,r;//结点数和根结点的位置
}CTree;

Features:Easy to find the child, parents find difficult

这样的结构对于我们要查找某个结点的某个孩子,或者找某个结点的兄弟,只需要查找这个结点的孩子单链表即可。对于遍历整棵树也是很方便的,对头结点的数组循环即可。


但是,要找双亲结点却很麻烦,我们可以把双亲表示法和孩子表示法综合一下,称为双亲孩子表示法,算是孩子表示法的改进。

我们在表头结构中新增加一个成员即可,存储双亲下标。为了多存储双亲,操作方便,我们牺牲了空间。
Here Insert Picture Description

三、孩子兄弟表示法(二叉树表示法、二叉链表表示法)

任意一棵树,它的结点的第一个孩子如果存在就是唯一的,它的右兄弟如果存在也是唯一的。因此,我们用二叉链表作树的存储结构,链表中每个结点的两个指针域分别指向第一个孩子结点此结点的下一个兄弟结点
,根结点没有兄弟,置为空。
结点结构如表:


画的有些丑,将就着看哈~(最右边是nextsibling,写错了)

//树的孩子兄弟表示法结构定义
typedef struct CSNode 
{
	int data;
	struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;//结点类型和指向结点类型的指针,指针表示孩子兄弟表示的二叉链表结构

举例:
Here Insert Picture Description

这种表示法,给查找某个结点的某个孩子带来方便,当然想找到结点的双亲,这个表示法也是有缺陷的,所以真的有必要的话,完全可以再增加一个parent指针域来解决快速查找双亲的问题,大家试试吧

此表示法的最大好处就是把一棵复杂度的树变成了一颗二叉树。


四、森林(树)和二叉树的转换

1)将树转化为二叉树进行处理,利用二叉树的算法来实现对树的操作
2)由于树和二叉树都可以用二叉链表作存储结构,则以二叉链表作媒介可以导出树与二叉树之间的一个对应关系。

  1. 将森林转换成二叉树
    1)加线:在兄弟之间加一连线
    2)抹线:对每个结点,除了其左孩子外,去除其与其余孩子之间的关系
    3)旋转:以树的根结点为轴心,将整数顺时针转45度。
    记忆口诀:兄弟相连留长子
    过程如下:加线->抹线->旋转
    Here Insert Picture Description

  2. Convert binary tree forest
    steps:
    1) add the line: If p is the left child node parent node, then p is the right child, right child of the right child ... ... find all the children along the right branch, both with p even parents line up with
    2) wipe wire: erase the line between the original binary tree and the right child of the parent
    3) adjustment: nodes hierarchically arranged in a tree structure is formed

Memory formulas: left right right even two-child, child the right to remove the original line
As follows: Add Line -> wiping Line -> adjustment (rotation)
Here Insert Picture Description


Content Reference:
"data structure" Yan Wei Min

Published 34 original articles · won praise 85 · views 4608

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/104067660