Data Structure-Binary Tree | Understanding and Realization of Binary Tree of Clues

For a binary tree with n nodes:
it has 2*n pointer fields

Corresponding to it has n-1 pointer branches

There are n+1 pointer fields that are empty (on the leaves, or not the nodes of the complete binary tree)

The clue binary tree uses n+1 empty chain domains to store the information of the predecessor and successor of the node.

The solution is to add two flag fields: left flag ltag and right flag rtag.
At this time, the node structure of the clue binary tree is as follows:
lchild ltag data rtag rchild

Where
ltag is 0 refers to the left child of the node, when it is 1, it refers to the predecessor of the node

When rtag is 0, it points to the right child of the node, and when it is 1, it points to the successor of the node.

The essence of clueing is to change the null pointer of the binary tree to a clue to the predecessor or successor. This process is obtained when traversing the binary tree

——————————————————————————————————————
//pre is always the node in the process The previous one of the tree (the order is according to the traversal)
//Since the pre is in the front and the tree is in the back,
then first make the left child of the tree point to the pre node
//and then make the right child of pre point to the tree node
// traversing The tree keeps changing during the process, and the pre is also changing

//I didn't understand how to generate a doubly linked list at the beginning

可以这样思考   我们输入时是以二叉树的顺序输入
然后遍历时有着不一样的顺序 
这个时候已经可以根据不一样的遍历方式根据A点找到其他点(而非拘泥于输入时候的顺序)
那么这些点又重新建立了关系

但这些关系是单向的


加上线索的原因就是使这些新的关系变为双向的
从而变为了双向链表!!!

Insert picture description here

比如这里输入的顺序是A B C D E F G
这也是前序遍历的顺序

Insert picture description here

但中序遍历可以得到B D C E A F G这样的顺序

Insert picture description here

同理 后序遍历也是如此

The predecessor and successor of a node can be different with different traversal rules;
the binary tree of clues corresponding to pre-order, middle-order, and post-order is called pre-cue binary tree, middle-order cue binary tree, and subsequent cue binary tree.

	//pre始终在过程中的结点tree的前一个(顺序是按照遍历的来) 
	//既然pre在前  tree在后
	//那么先使tree的左孩子指向pre结点
	//再使得pre的右孩子指向tree结点

	//在遍历的过程中tree一直变化,  pre也在变化
	//刚开始没有理解双向链表如何生成 看图可以推一下
#include<iostream>
using namespace std;
typedef int Status;
typedef char elementype;

typedef enum {
    
    Link,thread}Pointertag;//枚举类型  第一个不声明的话就为1,后面依次增加

typedef struct TBree
{
    
    
	elementype data;
	struct TBree *lchild, *rchild;
	Pointertag ltag, rtag;
}*TB_NODE,BNODE;

//中序线索二叉树
Status InitThread_mid(TB_NODE &tree,TB_NODE &pre)
{
    
    
	if (tree == NULL)
	{
    
    
		return 0;
	}
	else
	{
    
    
		InitThread_mid(tree->lchild,pre);

		if (!tree->lchild)//如果某元素的左孩子不存在的话,则使pre为其它的前驱   使得遍历顺序直接能用指针找到
		{
    
    
			tree->ltag = thread;
			tree->lchild = pre;
		}
		if (pre->rchild == NULL)//如果前驱元素的右孩子不存在的话,使其右孩子指向下一个  即建立后继
		{
    
    
			pre->rtag = thread;
			pre->rchild = tree;
		}
		pre = tree;//保持pre为   前面的元素
		//这里原为中序遍历的输出

		InitThread_mid(tree->rchild,pre);
	}
	return 0;
}
//前序线索二叉树
Status InitThread_before(TB_NODE &tree, TB_NODE &pre)
{
    
    
	if (tree == NULL)
	{
    
    
		return 0;
	}
	else
	{
    
    
		if (tree->lchild == NULL)//建立该结点的前驱
		{
    
    
			tree->ltag = thread;
			tree->lchild = pre;
		}
		if (pre->rchild == NULL)//建立前驱的后继
		{
    
    
			pre->rtag = thread;
			pre->rchild = tree;
		}
		pre = tree;
		InitThread_before(tree->lchild, pre);
		InitThread_before(tree->rchild,pre);
	}
	return 0;
}
//后续线索二叉树
Status Initthread_after(TB_NODE &tree, TB_NODE &pre)
{
    
    
	if(tree == NULL)
	{
    
    
		return 0;
	}
	else
	{
    
    
		Initthread_after(tree->lchild, pre);
		Initthread_after(tree->rchild,pre);

		if (tree->lchild == NULL)
		{
    
    
			tree->ltag = thread;
			tree->lchild = pre;
		}
		if (pre->rchild == NULL)
		{
    
    
			pre->rtag = thread;
			pre->rchild = tree;
		}
		pre = tree;
	}
}
//当有了线索之后,二叉树可以被认为是一个双向链表

Chain representation of binary tree|pre-order output|follow-up output|in-order output|destroy and other operations

Parental representation of children of N-ary tree-sequential storage structure + linked list | data structure-tree | C++ implementation

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/112149329