Data structure of the two-fork tree __ Data structure

Source: Internet
Author: User
Two fork Tree:
Use the widest range, and the most regular to follow, easy to maintain and handle

Using a two-fork list to represent the storage structure of a binary tree

typedef struct BITNODE
{
    elemtype data;                  Store node Data
    struct Bitnode *lchild,*rchild;//Pointer to left child and right child node
}bitnode,*bitree;


Traversal of binary tree:
Starting from the root node, access all nodes in the binary tree in some order, so that each node is accessed once and accessed only once
Traversal method:
a). Pre-sequence traversal (first-order traversal)
b). In-sequence traversal
c). Subsequent traversal
d). Sequence traversal

The code is as follows, including the establishment of the two-fork tree and its traversal

#include <stdio.h> #include <stdlib.h> typedef char elemtype;
    typedef struct BITNODE {elemtype data;
struct Bitnode *lchild,*rchild;
}bitnode,*bitree;
    Create a binary tree, contract the user to enter data in the order of the first traversal of the void Createbitree (Bitree *t) {char C;
    scanf ("%c", &c);
    if (' ==c ') {*t=null;
        }else {*t= (Bitnode *) malloc (sizeof (Bitnode));
        (*t)->data=c;
        Createbitree (& (*t)->lchild);
    Createbitree (& (*t)->rchild); }///access the concrete operation of the binary tree void visit (char c,int level) {printf ("%c is at layer%d \ n", c,level);}//Pre-ordered traversal binary tree void Preordertraverse (Bi
        Tree T,int level) {if (T) {visit (t->data,level);
        Preordertraverse (t->lchild,level+1);
        Preordertraverse (t->rchild,level+1);
        /* Middle sequence Traversal preordertraverse (t->lchild,level+1);
        Visit (T->data,level);

        Preordertraverse (t->rchild,level+1);
        After the subsequent traversal preordertraverse (t->lchild,level+1); Or:Ordertraverse (t->rchild,level+1);
        Visit (T->data,level);
    */} int main () {int level = 1;

    Bitree T=null;
    Createbitree (&t);

    Preordertraverse (T,level);
return 0; }

The binary tree will waste a lot of space when it is implemented, because the leaf node of the two child pointer will be empty, and the general two-tree of the non-leaf node may also be empty, and if a node is known to access its successor node is simple, but if you want to access its predecessor nodes need to traverse the entire tree structure, the time complexity of high
So we figured out if there was a way to save the space that "^" wastes.
The answer is yes, based on the sequence traversal, "^" is used to record the precursor and subsequent-----of the node. That's the clue. Two fork Tree
"Dilatancy" the previously defined structure, recording the precursor and subsequent nodes

PO Map simple and clear ~


The following write code implementation:

#include <stdio.h> #include <stdlib.h> typedef char elemtype;

Thread storage flag bit//link (0): Indicates a pointer to the left and right child//thread (1): Indicates a thread pointing to the predecessor, the typedef enum {link,thread} Pointertag;
    typedef struct BITHRNODE {elemtype data;
    struct Bithrnode *lchild,*rchild;
    Pointertag Ltag;
Pointertag Rtag;


}bithrnode,*bithrtree;

Global variable, always point to the node that has just been visited Bithrnode *pre;
    Create a binary tree, require the user to enter data in order of first sequence void Creatbithrtree (Bithrtree *t) {char C;
    scanf ("%c", &c);
    if (' ==c ') {*t=null;
        }else {*t= (Bithrnode *) malloc (sizeof (Bithrnode));
        (*t)->data=c;
        (*t)->ltag=link;  (*t)->rtag=link;
        First assume that all nodes have children node Creatbithrtree (& (*t)->lchild);
    Creatbithrtree (& (*t)->rchild); In//in sequence traversal implements the threaded void inthreading (Bithrtree t) {if (t) {inthreading (t->lchild);//recursive left subtree thread//processing The current node if (!  T->lchild)///If there is no left child {t->ltag=thread;
       Modifies the ltag of the current node to indicate a pointer to a predecessor or subsequent index     t->lchild=pre;
             The left child pointer points to the node that has just been accessed} if (!pre->rchild) {pre->rtag=thread;
         pre->rchild=t;  } pre=t;
The pre always points to the node inthreading (t->rchild) that has just been accessed;//recursive Right subtree cue}} void Inorderthreading (Bithrtree *p,bithrtree T)  {*p= (Bithrnode *) malloc (sizeof (Bithrnode));
    Create a "header node" (*p)->ltag=link;
    (*p)->rtag=thread;    (*p)->rchild=*p; Initializes the node, the left pointer points to the root node of the tree, the right pointer to the predecessor, and ultimately to the last node that points to the tree traversal if (!).
    T//If it is an empty tree, the left pointer points to itself {(*p)->lchild=*p;
        else//non-empty tree, point to root node {(*p)->lchild=t;           Pre=*p;  A pointer to the node that has just been accessed----initialize the Pointing head node inthreading (T);
        After the sequence traversal index completes, the pre points to the last traversal node of the tree pre->rchild=*p;
        pre->rtag=thread;  (*p)->rchild=pre; 
    Implements the last node of the tree and ends with the head pointer} void visit (char c) {printf ("%c", c);}//In-sequence traversal binary tree, non-recursive void Inordertraverse (Bithrtree P) {  Bithrtree p;  P is equivalent to a head pointer, and the left child node points to the root node if the tree is not empty p=p->lchild; //P points to the root node while (p!=p)//When the tree is not empty or the tree is not traversed end {while (P->ltag==link)//If there is always a left child {p=p->
        Lchild;   } visit (P->data); 
        First access node is found//thereafter while (P->rtag==thread && p->rchild!=p)///If the node has a successor node and the entire tree is not traversed    {p=p->rchild;
        The right pointer points to the successor visit (P-&GT;DATA);   } p=p->rchild;

    To access the right subtree}//In fact, the sequence traversal index binary tree, the root node traversal is based on the successor pointer to the node to access the main () {Bithrtree p,t=null;

    Creatbithrtree (&t);
    Inorderthreading (&p,t);
    printf ("The middle sequence traversal output result is:");
	Inordertraverse (P);
    printf ("\ n");
return 0;
 }

The conversion of tree, forest and binary tree

For trees and forests, which do not have a regular data structure, the processing of which is obviously much more complex, so the structure is limited to form a regular two-fork tree
Then in the face of the general tree structure or forest, only need to follow certain rules to convert to two-tree, and then data processing or traversal

There are two ways to traverse a tree:
Root Traversal First
After root traversal

Forests also have two ways of traversing: in fact, according to the tree's first root traversal and after the root traversal in turn to visit the forest every tree
First-order traversal
Subsequent traversal


And we found that: tree, forest before the root (sequence) traversal and two-fork tree sequence traversal is the same, tree, forest after the root (subsequent) traversal and two-tree sequence traversal results are the same ... The rationality of our conversion to two-tree processing is more determined.

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.