Threaded binary tree basic concepts, for example binary appreciated clues, clue binary tree traversal sequence

Why clues binary tree?

When used as a binary list stored binary tree structure, you can easily find a left child node; In general, however,The node can not be found directly in certain traversal sequence predecessor and successor nodes.

Binary number prior learned hollow chain pointer fields:Binary chain having n nodes, a total of n + 1 pointers field is empty


Threaded binary basic concepts

Using a binary null linked list pointer field:

If the left child of a node is empty, empty field instead of left child pointerPointing to its predecessor; If a right child node is empty, an empty field to the right child pointersPoints to its successor

This kindChange pointer toCalledclue.
Plus trail called binary treeThreaded binary.
Of the process according to the binary tree traversal order that it becomes some kind of threaded binary calledThreaded

To distinguish lchild and rchild pointer in the end is directed around the child pointer or a pointer points to the predecessor or successor, the list of binaryEach node two additional flags and fields ltag rtagAnd convention:
Here Insert Picture Description
the node in the form of:
Here Insert Picture Description


Threaded binary representation storage

typedef struct BiThrNode{
	int data;
	int ltag,rtag;//左右标志
	struct BiThrNode *lchild,rchild;//左右孩子指针
}BiThrNode,*BiThrTree;	

For example three kinds of clues to understand binary tree

Red dotted line points to the predecessor and successor, the solid blue line to the left and right children

Here Insert Picture Description
1)Preorder binary tree trail:
First order sequence is: ABCDE
illustrated as:
Here Insert Picture Description
2)In threaded binary sequence:
In order sequence: BCAED
diagram is:
Here Insert Picture Description
3)Follow clues to binary tree:
Subsequent sequence: C B EDA
illustrated as:
Here Insert Picture Description


Why additional head node?

The figure below showsIn threaded binary sequence
In order sequenceH D I B E A F C G
Here Insert Picture Description
It is seen from the FIG., But also the remaining two null pointer. For ease of operation, thus avoiding floating state, we adding a header node:

Operating head node:
. 1) Data field is left blank, ltag = 0, lchild root point
2) rtag = 1, rchild point to the last traversal sequence node
3) traversing a first node in the sequence (no precursor) and the last field of lchild node (no successor) of the field point rchild head node.

The final results shown in Figure:
Here Insert Picture Description

Like as a circular linked list, you can access any node, the operation more convenient.


Binary tree traversal sequence clues
to declare store, said:

typedef struct BiThrNode{
	 int data;
	 int ltag,rtag;//左右标志
	 struct BiThrNode *lchild,rchild;//左右孩子指针
}BiThrNode,*BiThrTree; 

【Algorithm Description】

void InOrderTraverse_Thr(BiThrTree T)
{//T指向头结点,头结点的左链lchild指向根结点
//中序遍历二叉线索树T的非递归算法,对每个数据元素直接输出
	BiThrNode *p=T->lchild;	//p指向根结点
	while(p!=T)		//空树或遍历结束时,p==T
	{	
		while(p->ltag==0)
			p=p->lchild;	//沿着左孩子向下
			cout<<p->data;	//访问其左子树为空的结点
		while(p->rtag==1||p->rchild!=T)	
		{
			p=p->rchild;	//沿右线索访问后继结点
			cout<<p->data;
		}
		p=p->rchild;		//转向p的右子树
	}	
}

The time complexity is O (n), the spatial complexity is O (1), since the threaded binary traversal recursion need not be implemented stack


Content Reference: "data structure" Yan Wei Min

Published 34 original articles · won praise 85 · views 4609

Guess you like

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