Article Directory
A. Threaded binary basis
Clue: points to the predecessor and successor pointers
trail list: adding binary cue list
threaded binary: adding binary cues (corresponding to a doubly linked list)
threaded: traverse the binary tree in a certain order so that it becomes threaded binary process
Cueing is the process of modifying a null pointer in the traversal process
How do I know rchild (lchild) refers to the right (left) or subsequent child it?
Set two flags field ltag and rtag , ltag and rtag is stored only 0 or 1 Boolean variables:
- ltag node to point to the left child is 0, 1 is directed to the precursor node
- rtag to point to the right child node is 0, 1 is directed to the successor node
II. Structure realized
1. Ordinary threaded binary
Threaded binary tree structure:
//Link = 0,表示左右孩子指针
//Thead = 1,表示前驱或后继
typedef enum { Link, Thread } Pointertag;
typedef struct BiThrTree
{
char data;
struct BiThrTree* lchild, * rchild;
Pointertag ltag;
Pointertag rtag;
}BiThrNode,*BiThrTree;
BiThrTree pre;//指向刚刚访问过的结点
Binary tree sequence of clues clues:
void Inthread(BiThrTree p)
{
if (p)
{
Inthread(p->lchild);
//若某结点的左指针域为空(前驱结点刚刚访问过,赋值给了pre)
if (!p->lchild)
{
p->ltag = Thread;
p->lchild = pre;
}
//P的后继还没访问到,因此对P的前驱pre的右指针进行判断
if (!pre->rchild)
{
pre->rtag = Thread;
pre->rchild = p;
}
pre = p; //将当前结点赋值给pre
Inthread(p->rchild);
}
}
- This code will only inorder traversal of the binary tree recursive codebook print function node into a threaded
Threaded binary tree traversal sequence:
the sequence in a manner corresponding threaded binary
void Inorder(BiThrTree T)
{
//让p指向根结点
BiThrTree p = T;
while (p)
{
//遍历到中序遍历第一个开始的结点(左子树)
while (p->ltag == Link)
{
p = p->lchild;
}
printf("%c", p->data);
//如果该结点右孩子是线索(后继),且右指针存在,就一直遍历
while (p->rtag == Thread && p->rchild)
{
p = p->rchild;
printf("%c", p->data);
}
/*
* 当右孩子不是线索时(没有后继),
* 说明该结点右孩子存在,
* 所以接着右孩子的遍历
*/
p = p->rchild;//p进至其右子树根
}
}
2. The node having the first threaded binary
Add a head in front of a binary tree root node, its left child point to the root node , right child points to the last node in a preorder , the equivalent of a doubly linked list
threaded binary tree of clues in order:
void Inthread2(BiThrTree* head, BiThrTree T)
{
(*head) = (BiThrTree)malloc(sizeof(BiThrNode));
//左孩子是根结点 0
(*head)->ltag = Link;
//右孩子是线索 1
(*head)->rtag = Thread;
//右孩子先指向自身,防止原二叉树为空
(*head)->rchild = (*head);
//二叉树为空
if (T == NULL)
{
//左孩子也指向自身
(*head)->lchild = *head;
return;
}
(*head)->lchild = T;
pre = *head; // 前指针指向头结点
/*
* 普通的中序线索化二叉树,
* 此时二叉树中序序列的第一个结点的左孩子指向头结点
* 结束后,此时的pre指向最后一个元素
*/
Inthread(T);
pre->rchild = *head;
pre->rtag = Link;
(*head)->rchild = pre;
}
Traverse either from scratch, you can also traverse from the tail
Threaded binary tree preorder:
void Inorder2(BiThrTree head)
{
//让p指向根结点
BiThrTree p = head->lchild;
//空树或遍历结束 p==head
while (p != head)
{
while (p->ltag == Link)
{
p = p->lchild;
}
printf("%c", p->data);
//右孩子不能为 head
while (p->rtag == Thread && p->rchild!=head)
{
p = p->rchild;
printf("%c", p->data);
}
p = p->rchild;
}
}
When often find the need to traverse junctions or points, may be used, it is far less than the time complexity of recursively