Binary Tree - Learning And Think

1.About -Traversal of Binary Tree

1. prepared work
typedef struct BiTree {
    char data;
    struct BiTree *lchild, *rchild;
    int ltag,rtag;
}BiTree,*BiTreeNode;  

typedef struct Queue {
    BiTree *qdata;
    struct Queue *next;
}Queue,*SqQueue;

typedef struct LinkQueue { 
    struct Queue *front, *rear;
}LinkQueue;

typedef struct Stack {
     BiTreeNode *base, *top;
    ElemType stacksize;
}Stack,*StStack;
int CreatBiTree(BiTreeNode &root) {
    char ch ;
    cin >> ch;
    if (ch == '#') {
        root = NULL;
    }
    else {
        if (!(root)) {
            return false;
        }
        root = new BiTree;
        root->data = ch;
        CreatBiTree(root->lchild);
        CreatBiTree(root->rchild);
    }
    return true;
}
2.PreOrder

yeah! There are two ways in this part to implement the traversal method.
The first is Recursion way

int PreOrderBiTree(BiTreeNode T) {
    if (T) {
        cout << T->data << " ";
        PreOrderBiTree(T->lchild);
        PreOrderBiTree(T->rchild);
    }
    return 0;
}

The second is NoRecursion way(Stack)

void PreNoRecursionOrder(BiTreeNode T) {
    Stack S;
    InintStack(S);
    BiTreeNode p = T;
    while (p || !IsStackEmpty(S)) {
        if (p) {
            cout << p->data << " ";
            StackPush(S, p);
            p = p->lchild;
        }
        else {
            StackPop(S, p);         
            p = p->rchild;
        }
    }
}
3.MidOrder

Also,two ways to implement traversal
The first is Recursion way

int midOrderBiTree(BiTreeNode T) {
    if (T) {
        PreOrderBiTree(T->lchild);
        cout << T->data << " ";
        PreOrderBiTree(T->rchild);
    }
    return 0;
}

The second is NoRecursion way(Stack)

void midNoRecursionOrder(BiTreeNode T) {
    Stack S;
    InintStack(S);
    BiTreeNode p = T;
    while (p || !IsStackEmpty(S)) {
        if (p) {
            StackPush(S, p);
            p = p->lchild;
        }
        else {
            StackPop(S, p);
            cout << p->data << " ";
            p = p->rchild;
        }
        
    }
}

we may clearly see the different between the pre and mid is the position of the output code,things get a little bit harder in NoRecursionPostTraversal

4.PostOreder

The first is Recursion way

int postOrderBiTree(BiTreeNode T) {
    if (T) {
        
        PreOrderBiTree(T->lchild);
        PreOrderBiTree(T->rchild);
        cout << T->data << " ";
    }
    return 0;
}

The second is NoRecursion way(Stack)

void postNoRecursionOrder(BiTreeNode T) {
    Stack S;
    InintStack(S);
    BiTreeNode p = T,r=NULL;
    while (p || !IsStackEmpty(S)) {
        if (p) {
            StackPush(S, p);
            p = p->lchild;
        }
        else
        {
            GetStackTop(S, p);
            if (p->rchild && p->rchild != r) {
                p = p->rchild;
                StackPush(S, p);
                p = p->lchild;
            }
            else
            {
                StackPop(S, p);
                cout << p->data << " ";
                r = p;
                p = NULL;
            }
        }
    }
}

The key of this way is to mark the node we have been traversed,i use the variable r to mark the node ,so the next traversae will filter that branch, or it will trpaed in the dead loop.

5.levelOrder

In this part ,i use the queue to help me traversal.

void levelOrder(BiTreeNode T) {
    LinkQueue Q; BiTreeNode p=new BiTree;
    InintQueue(Q);
    EnQueue(Q,T);
    while (!IsEmpty(Q)) {
        DeQueue(Q, p);
        cout << p->data << " ";
        if (p->lchild)
            EnQueue(Q, p->lchild);
        if (p->rchild)
            EnQueue(Q, p->rchild);
    }
}

2.About - Huffman Tree


你可能感兴趣的:(Binary Tree - Learning And Think)