Clue Thread two fork tree

Source: Internet
Author: User

For a normal two-fork tree

We can obviously see, in a binary tree, there will be a lot of empty nodes, and these empty nodes will inevitably cause the waste of space, in order to solve this problem, we can introduce the clue two fork tree, the use of these empty nodes, using the ' ^ ' to record the predecessor of a given node, then the problem is, how to build it?

Before we say four kinds of methods, which method should I use to build a clue two fork tree?

Through the analysis, we find that using the method of middle order traversal can effectively establish the clue two fork tree

Let's take a look at the results of the sequence traversal in the above two-fork tree

H D I B E A F C G

The red node is a null node.

Storage of precursors and successors in empty nodes

What happens when you face such a binary tree?

The middle order traversal is

F D G B A C E

We can see that there is only one empty junction in the B node and the node C.

How does the machine decide whether to put a clue or a pointer?

To solve this problem, we can expand each node of the tree.

Ltag: 0 o'clock, indicating that lchild points to the left child to change the node, for 1 o'clock, indicating that lchild points to the precursor of the node

Rtag: 0 o'clock, which indicates that rchild points to the right child of the node, to 1 o'clock, indicating that the rchild points to the successor of the node

The code implementation of the Clue two fork tree:

#include <iostream>using namespace std;typedef char elemtype;//thread store flag bit//link is 0 o'clock, indicating that the pointer to the left and right child//thread to 1 o'clock, Indicates a clue to the successor of the precursor typedef enum{link, thread} pointertag;typedef struct Bitthrnode{char data;struct bitthrnode *lchild, *rchild ; Pointertag Ltag; Pointertag Rtag;} Bitthrnode, *bitthrtree;//global variable, points to the node that has just been accessed Bitthrtree pre;//to create two fork tree void Createbitthrtree (Bitthrtree *t)//root node address { Char c;cin >> c;if (c = = '-') {*t=null;}    Else{*t=new Bitthrnode; (*t)->data=c; (*t)->ltag=link; (*t)->rtag=link;createbitthrtree (& (*t)->lchild); CreateBitThrTree ( & (*t)->rchild);}} The middle sequence traversal of the threaded void inthreading (Bitthrtree t) {if (t) {inthreading (t->lchild);//recursive Left child threaded//Node processing if (! T->lchild) {t->ltag=thread; T->lchild=pre;} if (!pre->rchild) {pre->rtag=thread;pre->rchild=t;}    Pre=t;inthreading (t->rchild);//recursive Right child threaded}}void inorderthreading (bitthrtree *p, Bitthrtree T) {*p=new BitThrNode (); (*p)->ltag=link; (*p)->rtag=thread; (*p)->rchild=*p;if (! T) {(*p)->lchild=*p;} else{(*p)-&GT;lchild=t;pre=*p;inthreading (T); pre->rchild=*p;pre->rtag=thread; (*p)->rchild=pre;}} void visit (char c) {Cout<<c<<endl;} Middle sequence Traversal binary tree non-recursive void Inordertravel (Bitthrtree T) {bitthrtree p;p=t->lchild;while (p!=t) {while (P->ltag==link) {p=p- >lchild;}    Visit (P->data); while (p->rtag==thread&&p->rchild!=t) {p=p->rchild;visit (p->data);} P=p->rchild;}} int main () {Bitthrtree t=null;        Bitthrtree P;createbitthrtree (&t);        Inorderthreading (&p,t);cout<< "Middle sequence traversal output is" <<endl;inordertravel (p); return 0;}

  

Clue Thread 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.