Thread-traversal of thought, process, code

Source: Internet
Author: User

1. Preface

The normal binary tree can only find the children information of the nodes, and the direct precursor and direct successor of the node can only be obtained during the traversing process.

If the corresponding precursor and subsequent pre-traversal can be stored, the whole tree can be traversed quickly from the first node.

What is the idea of a binary clue tree?



Middle order traverse this tree = = = "Convert to Linked list access

2 Thought of Clue




Conclusion: The lead process is the process of modifying a null pointer in a traversal process (assuming a sequential traversal):

Change the empty lchild to the direct precursor of the node;

Change the empty rchild to the direct successor of the node.


3 Thought of Clue


Please thread this tree.

1) Right null pointer thread:


2) left null pointer is threaded


3) Summary


The essence of the clue: let the front and back node, establish the relationship;

1) Two auxiliary pointer variables formed after the difference: the left child of the successor node points to the precursor node, the right child of the precursor node points to the successor node.

2) Logical relationship of assignment pointer variables and business operations


Code:

threadtree.cpp//Tree's clue # _crt_secure_no_warnings#include <iostream> #include <cstdio> #include <stack>using namespace std;//link = = 0 means pointing to the left and right child pointer//thread==1 indicating a precursor or successor Clue # define thread 1#define Link 0//two fork clue storage node structure typedef struct BITHRNODE{CHAR data;struct bithrnode *lchild, *rchild;int ltag;int rtag;///Left/Right flag}bithrnode, *bithrtree;c Har Nil = ' # '; Character type with space blank//By the value of the node in the binary clue tree in the preamble, construct a two-fork clue Tree tbithrnode* Createbithrtree () {Bithrnode *tmp = Null;char ch;scanf ("%c", &ch) if (ch = = ' # ') {return NULL;} else {tmp = (Bithrnode *) malloc (sizeof (Bithrnode)); if (TMP = = null) {return null;} memset (tmp, 0, sizeof (bithrnode)), Tmp->data = Ch;tmp->lchild = Createbithrtree (); Recursive construction left subtree Tmp->rchild = Createbithrtree ();} return TMP;} Bithrnode *pre; A global variable that always points to a node that has just been visited//middle-order traversal for sequencing-threaded void inthreading (Bithrnode *p) {if (p) {inthreading (p->lchild);//recursive left subtree-threaded if (!p-& Gt;lchild) {//no left subtree P->ltag = Thread;//precursor thread p->lchild = pre;//left child pointing precursor}if (!pre->rchild) {//precursor No child pre-> Rtag = Thread; Subsequent clues pre->rchild = p; The precursor and the child point to the successor}pre = p; Keep the pre-inthreading (p->rchild) pointing to P; Recursive right subtree}}//sequence traversal binary tree T, and the sequence is threaded, thrt points to the head node bithrnode* inorderthreading (Bithrtree T) {Bithrnode *thrt = NULL; Thrt = (Bithrnode *) malloc (sizeof (Bithrnode)); Thrt) {return NULL;} memset (thrt, 0, sizeof (bithrnode)); Thrt->ltag = Link; Left child for child pointer thrt->rtag = Thread; Right child for the clue pointer thrt->rchild = Thrt; The right pointer refers back to the IF (! T) {//Jou fork tree is empty, the left pointer refers to Thrt->lchild = Thrt;} else {thrt->lchild = T;//Step 1pre = thrt;inthreading (t);//middle order traversal for sequencing pre->rchild = thrt;//Step 4pre->rtag = Threa D The last node leads thrt->rchild = pre; Step 2}return Thrt;} /* Non-recursive algorithm for sequential traversal of binary clue Tree T (head node) */int inordertraverse_thr (bithrnode* t) {bithrnode* p;p = t->lchild; */p points to root node */while (p! = t) {/* At the end of the empty tree or traversal, p==t */while (P->ltag = = Link) p = p->lchild;printf ("%c", p->data);//If the right child = = T of the last node of the middle order traversal is explained to the last node, traverse end: while (P->rtag = = Thread && p->rchild! = T) {p = p->rchild;printf ("%c", p->data);} p = p->rchild;} return 0;} /* Non-recursive algorithm for sequential traversal of binary clue Tree T (head node) */int INORDERTRAVERSE_THR2 (bithrnode* t) {bithrnode* p;p = t->rchild; */p points to root node */while (p! = T {/*/* At the end of the empty tree or traversal, p==t */while (P->rtag = = Link) p = p->rchild;printf ("%c", p->data);//If the right child = = T of the last node of the middle order traversal is explained to the last node, the end of the traversal. while (P->ltag = = Thread && p->lchild! = T) {p = p->lchild;printf ("%c", P->data);} p = p->lchild;} return 0;} void Operatortree () {Bithrtree T, h;printf ("Enter the binary tree in the preamble (such as: ' abdh# #I # # #EJ # # #CF # #G # # ') \ n"); T = Createbithrtree (); Generate two fork Tree H = inorderthreading (T) in order of preamble; The middle sequence traversal, and the middle sequence Clue binary tree printf ("Middle sequence traversal (output) binary clue tree: \ n"); Inordertraverse_thr (H); Middle sequence traversal (output) binary clue tree//H D I b J E A F C gprintf ("\ n Reverse access:"); Inordertraverse_thr2 (H);//G C F A E J B I D hprintf ("\ n"); int main () {operatortree (); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Thread-traversal of thought, process, code

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.