Data structure-clue two fork Tree

Source: Internet
Author: User

Clue Tree
  遍历二叉树是按一定的规则将树中的结点排列成一个线性序列,即是对非线性结构的线性化操作。如何找到遍历过程中动态得到的每个结点的直接前驱和直接后继(第一个和最后一个除外)?如何保存这些信息?问:一棵有n个结点的二叉树,有多少个空闲指针域未用?    若一棵二叉树有n个结点,则有n-1条指针连线 , 而n个结点共有2n个指针域(Lchild和Rchild) ,所以有n+1个空闲指针域未用。可以利用这些空闲的指针域来存放结点的直接前驱和直接后继信息。

The pointer field of the node is defined as follows:
If the node has a left child, then the lchild points to its left child, otherwise, pointing to its direct precursor;
If the node has a right child, then the rchild points to its right child, otherwise, pointing to its direct successor;

Node structure of clue two fork tree

A two-tree storage structure consisting of this node structure is called a clue chain list; A pointer to a node precursor and a successor is called a clue, and in some order, a two-tree with clues is called a Clue two fork tree.

线索二叉树的结点结构与示例typedefstruct BiTreeNode{   ElemType  data;structint  Ltag , Rtag ;}BiThrNode ;
如何在线索树中找结点的直接后继?以图 ,所示的中序线索树为例:

If the right chain of nodes in the tree is a clue (rtag=1), then the right chain directly indicates the immediate successor of the end point, as the direct successor of the node G is the node E.
If the right chain of nodes in the tree is a pointer (rtag=0). According to the rule of the sequence traversal, the direct successor of the RTAG=0 node is the first node that is accessed when traversing its right subtree, that is, the (leaf) node of the leftmost lower position in the right subtree. such as the direct successor of Node C: Locate the root node F of the right subtree along the right hand, then follow the left chain down until the Ltag=1 node is the direct successor node H of C.

Post-Traversal Trail tree

The direct successor of finding nodes in the clue Tree of post-order traversal is more complicated, which can be divided into the following three kinds of situations:

If the node is the root node of a two-fork tree: Its direct successor is empty;
If the node is the left child of its parent node and its parent node has no right subtree: The direct successor is its parent node;
If the node is the left child of its parent node and its parent node has a right subtree: The direct successor is the first node of the right subtree of its parent node to be traversed sequentially.

Threaded binary Tree

The clue of binary tree refers to the process of making a two-fork tree into a clue two-tree according to some sort of traversal order.

线索化的过程就是在遍历过程中修改空指针使其指向直接前驱或直接后继的过程。   下面主要讨论按中序遍历次序线索化二叉树。   仿照线性表的存储结构,在二叉树的线索链表上也添加一个头结点head,头结点的指针域的安排是:

Lchild domain: The root node that points to the binary tree;
Rchild field: points to the last node in the middle sequence traversal;
The first node in the sequential sequence of a binary tree lchild the pointer field and the last node rchild the pointer field points to the head node head.

  添加了头结点的线索二叉树,如同为二叉树建立了一个双向线索链表,对一棵线索二叉树既可从头结点也可从最后一个结点开始按寻找直接后继进行遍历。显然,这种遍历不需要堆栈。
#define  MAX_NODE   50typedef enmu{ Link , Thread} PointerTag ;/*  Link=0表示指针, Thread=1表示线索   */typedefstruct BiThrNode{   ElemType  data;struct BiTreeNode *Lchild , *Rchild ; PointerTag  Ltag , Rtag ;}BiThrNode, *BiThrTree;
Constructing two-fork clue tree by ordinal sequence
Elemtype nil= '# '; /* with # empty * /Status Createbithrtree (Bithrtree *t) {elemtype ch; scanf"%c", &ch);if(H==nil) *t=null;Else{*t= (bithrtree) malloc (sizeof (Bithrnode));if(!*t)returnERROR; (*t)Data=ch;/* Generate root node (pre-order) */Createbithrtree (& (*t)->lchild);/* Recursive construction left subtree */ if((*t)->lchild)(*t)    Ltag=link;/ * have left child * /Createbithrtree (& (*t)->rchild);/* Recursive construction right subtree */ if((*t)->rchild)(*t)-> ;   Rtag=link;/ * have right child * /}returnOK;}
The recursive function of the middle sequence traverse the clue
Bithrnode*Pre//global variable that always points to the node you just visitedvoidInthreading (Bithrnode*T) {if(t) {inorder_threading (t -Lchild)/ * Recursive left sub-tree thread * /    if(!T -Lchild)/ * No left child * /{T -Ltag=Thread;/ * Precursor clue * /T -Lchild=Pre/ * Left child pointer pointing to precursor * /}if(!Pre -Rchild)/ * Precursor no right child * /{Pre -Rtag=Thread;/ * Successor clue * /Pre -Rchild=T/* Precursor right child pointer pointing to successor (current node T) */} Pre=T/ * Keep the pre pointing to T's precursor * /Inorder_threading (T -Rchild);/ * Recursive right sub-tree thread * /}}

The clue of the precursor node: if (! T->lchild) indicates that if the left pointer field of a node is empty, because its predecessor has just been accessed and assigned to the pre, it can be assigned to T->lchild and modified t->ltag=thread (that is, defined as 1) to complete the cue of the precursor node ;

The subsequent node of the clue: because the subsequent node has not been accessed, so it can only be the predecessor of the pre-node right pointer rchild judgment, if (!pre->rchild) indicates if the precursor of the right pointer field is empty, then T is the predecessor of the pre, so pre-> Rchild=t, and set the Pre->rtag=thread, to complete the subsequent node of the thread.

After completing the precursor and subsequent judgment, assign the current node T to the pre for the next use.

/ * Middle sequence traversal of the binary tree T, and the sequence of clues, Thrt point to the head node * /Status inorderthreading (Bithrtree *thrdhead, Bithrtree T) {* Thrdhead = (bithrtree) malloc (sizeof (Bithrnode));if(!* Thrdhead)returnERROR; (* thrdhead)Ltag=link;/ * Build head node * /     (* thrdhead)Rtag=thread; (* thrdhead)Rchild= NULL;//The right pointer is now emptyif(! T)(* thrdhead)   lchild= * THRDHEAD;//If the binary tree is empty, the left pointer returnsElse{ (* thrdhead)lchild=t;//Non-null, pointing to the root node pre= (* thrdhead); Inthreading (T);/* Middle sequence traversal for middle order thread * /pre->rchild=* Thrdhead;//The pre is the last node in the middle sequence traversal pre->rtag=thread;/ * Last node to be threaded * /         (* thrdhead)Rchild=pre; }returnOK;}
Clue Two fork Tree traversal
    线索二叉树的创建虽然比较复杂,但在线索二叉树中,由于有线索存在,在某些情况下可以方便地找到指定结点在某种遍历序列中的直接前驱或直接后继。   此外,在线索二叉树上进行某种遍历比在一般的二叉树上进行这种遍历要容易得多,不需要设置堆栈,且算法十分简洁。
/* Non-recursive algorithm for sequential traversal of binary clue Tree T (head node) */Status inordertraverse_thr (Bithrtree T) {Bithrtree p; P=T -Lchild;/ * p point to root node * /     while(p!=T/* At the end of the empty tree or traversal, p==t */{ while(p -Ltag==Link) p=P -Lchild;//When ltag==0, loop to the first node of the middle sequence sequence.Visit (P -Data); while(p -Rtag==Thread&&P -Rchild!=T) {P=P -Rchild; Visit (P -Data);/ * Access to subsequent nodes * /} p=P -Rchild; }returnOK;}

Data structure-clue two fork Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.