Data structure and algorithms-tree-two tree and Shing

Source: Internet
Author: User

Traversal of a binary tree

The traversal of a binary tree refers to all nodes in a binary tree that are accessed from the root node in a sequential order, so that each node is accessed once and accessed only once.

Binary Tree Traversal method:

Pre-order Traversal: The rule is that if the binary tree is empty, then an empty operation is returned, otherwise the root node is accessed first, then the left subtree is traversed before the sequence, and then the right subtree is traversed.

1 voidPreordertraverse (bitree T)2 {3     if(T = =NULL)4         return;5printf"%c", T->data);/*Show node data, can be changed to other node operations*/6Preordertraverse (T->lchild);/*The first sequence traverses the left subtree*/7Preordertraverse (T->rchild);/*Finally, the first sequence traverses the right sub-tree*/
a pre-sequence traversal algorithm for two-fork tree

Middle Sequence Traversal: The rule is that if the tree is empty, then an empty operation is returned, otherwise it starts at the root node (note that the root node is not accessed first), the middle sequence traverses the left subtree of the root node, and then accesses the root node, and the final middle sequence traverses the right subtree.

1 voidInordertraverse (bitree T)2 {3     if(T = =NULL)4         return;5Inordertraverse (T->lchild);/*Middle sequence traversal left subtree*/6printf"%c", T->data);/*Show node data, can be changed to other node operations*/7Inordertraverse (T->rchild);/*Finally, the middle sequence traverses the right sub-tree*/8}
a mid-sequence traversal algorithm for two-fork tree

Post-order traversal: The rule is that if the tree is empty, then an empty operation is returned, otherwise the left-to-right leaves the back node of the way through the access to the tree, and finally access the root node.

1 voidPostordertraverse (bitree T)2 {3     if(T = =NULL)4         return;5Postordertraverse (T->lchild);/*sequential traversal of the left sub-tree*/6Postordertraverse (T->rchild);/*and then go through the right sub-tree .*/7printf"%c", T->data);/*Show node data, can be changed to other node operations*/8}
Sequential traversal algorithm of two-fork tree

Sequence traversal: The rule is that if the tree is empty, then an empty operation is returned, otherwise it is accessed from the first layer of the tree, which is the root node, and is traversed from top to bottom, and in the same layer, the nodes are accessed from left to right sequentially.

The establishment of binary tree: The null pointer of each node in the two-fork tree leads to a virtual node, the value of which is a specific value, we call this processing of the two-fork tree as the original binary tree extension binary tree.

1 /*Enter the value of the node in the binary tree by the preamble*/2 /*#表示空树, constructs a two-fork list representing a binary tree T. */3 voidCreatebitree (Bitree *T)4 {5 Telemtype ch;6scanf"%c",&ch);7     if(ch = ='#')8*t =NULL;9     ElseTen     { One*t = (bitree) malloc (sizeof(Bitnode)); A         if( !*T) - exit (OVERFLOW); -(*t)->data = ch;/*Generating root nodes*/ theCreatebitree (& (*t)->lchild);/*constructing the left sub-tree*/ -Createbitree (& (*t)->rchild);/*Constructing the right sub-tree*/ -     } -}
Two establishment of a fork tree

Clue Two fork Tree:

Clue lists: pointers to precursors and successors are called clues, plus the two-linked list of clues is called the clue chain list. The corresponding two-fork tree is called the Clue two fork tree.

Thread: The process of traversing a two-fork tree in some order to turn it into a clue two fork tree.

1typedefenum{Link,thread} Pointertag;/*link ==0 Indicates a pointer to the left and right child, thread==1 indicates a clue to the precursor or successor*/2typedefstructBithrnode/*two fork clue storage node Structure*/3 {4Telemtype data;/*node Data*/5     structBitthrnode *lchild,*rchild;/*child hands left and right*/6 Pointertag Ltag;7Pointertag Rtag;/*left and right signs*/8}bithrnode,*bithrtree;
two fork tree two-fork clue storage structure

The essence of the clue is to change the null pointer in the two-linked list to a precursor or a subsequent clue.

The process of the clue is the process of modifying the null pointer during the traversal.

1Bithrtree Pre;/*global variable that always points to the node you just visited*/2 voidinthreading (Bithrtree p)3 {4     if(P)5     {6Inthreading (P->lchild);/*recursive left subtree clues*/7         if(!p->lchild)/*no left child .*/8         {9P->ltag = Thread;/*Precursor Clues*/TenP->lchild = pre;/*left child pointer pointing to precursor*/ One         } A         if(!pre->rchild)/*The precursor has no right child*/ -         { -Pre->rtag = Thread;/*subsequent clues*/ thePre->rchild = p;/*precursor right child pointer pointing to successor*/ -         } -Pre = P;/*keep the pre-pointing precursor to P*/ -Inthreading (P->rchild);/*Recursive right sub-tree thread*/ +     } -}
Mid-sequence traversal for medium sequence cues
1 Status inordertraverse_thr (bithrtree T)2 {3 Bithrtree p;4p = t->lchild;/*P points to the root node*/5      while(P! = T)/*at the end of an empty tree or traversal, p== T*/6     {7          while(P->ltag = = Link);/*Loop to the first node of the middle sequence when ltag==0.*/8p = p->Lchild;9printf"%c", P->data);/*Show node data, can be changed to other node operations*/Ten          while(P->rtag = = Thread && P->rchild! =T) One         { Ap = p->Rchild; -printf"%c",p->data); -         } thep = p->rchild;/*P into its right sub-root*/ -     } -     returnOK; -}
The two-fork tree T represented by the middle sequence traverse binary clue list

Data structure and algorithms-tree-two tree and Shing

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.