Data structure - binary tree traversal

Traversal concept

A tour along a search path in the binary tree nodes , such that each node are visited once, and is accessed only once.

"Access" means can be very broad, such as: information output node and so on.

"Traverse" are any type of operation, for a linear structure, and only a search path (since each node has only one successor both), it does not require additional discussion. andBinary non-linear structure, each node has two successors, i.e., how to traverse the search path according to what there is a problem to traverse

Search Path

For the "binary tree" is concerned, there can be three search paths:

  1. After traversing a hierarchy in the first;
  2. To right (sub-tree) traversal rear left (sub-tree);
  3. First left (subtree) traversal rear right (sub-tree).

Traversal algorithm (non-recursive)

Preorder

If the binary tree is empty, the air operation; otherwise,

  1. Access root node;
  2. Preorder left subtree;
  3. Preorder right subtree.

Preorder

If the binary tree is empty, the air operation; otherwise,

  1. Preorder left subtree;
  2. Access root node;
  3. Preorder right subtree.

Postorder

If the binary tree is empty, the air operation; otherwise,

  1. After traversing the left subtree order;
  2. Postorder right subtree.
  3. Access root node;

Preorder traversal Code Description

void preOrder(BiTree T, void(*visit)(int &e){
    //先序遍历二叉树
    if(T){
        visit(T->data); //访问节点
        preOrder(T->lchild, visit); //遍历左子树
        preOrder(T->richild, visit);    //遍历右子树
    }
}

Traversal algorithm (recursive)

Preorder

Central preorder known process using a stack pointer storage node need to return, the first scan (not accessible) the root node of all nodes left and every one of them into the stack.

Then pop a node, it is clear that there is no left child node left child node or nodes have visited, then access it.

Then scans the junction of the right child node, which is the stack, and then scan all nodes of the left and right child nodes and 11 into the stack, so this way, until the stack is empty.

void inOrder(BtTree root){
    initStack(&S);  //初始化栈
    p = root;
    while(p != NULL || !StackEmprt(S)){
        if(p != NULL){
            //根指针进栈,遍历左子树
            Push(&S, p);
            p = p->lchild;
        } else {
            //根指针出栈,访问根节点,遍历右子树
            Pop(%S, p);
            Visit(p->data); //访问节点
            p = p->rchild;
        }
    }
}

Preorder

From the preorder process, first visit the root, then visit the left subtree, right subtree last visit.

Therefore, the first root node into the stack, the stack is not empty cycle: the stack p, access *pnode, its right child node into the stack, and then left child node into the stack.

void preOrder(BtTree root){
    initStack(&S);
    p = root;
    if(root != NULL){
        Push(&S,p); //根节点进栈
        while(!StackEmpty(S)){  //栈不空时循环
            Pop(&S, p); //出栈并访问该节点
            Visit(p->data);
            if(p->rchild != NULL){
                Push(&S,p)->rchild);    //右孩子进栈
            } else if(p->lchild != NULL){   //左孩子进栈
                Push(&S, lchild);
            }
        }
    }
}

And the motif sequence preorder contribution Binary Tree

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/hjc256/article/details/94292506