Trail of clues and traverse binary tree

Summary

  • Definitions and data structures
  • Threaded
  • Traversal

Definitions and data structures

  1. Defined
    binary tree structure containing nodes referred threaded binary cues.
    If the node has a left subtree, it lchild field indicating its left child, or so lchild field indicates its precursor; if the node has a right subtree, it rchild field indicating its right child, or so rchild field indicates its successor.
    To avoid confusion, it is necessary to increase two flags and fields LTag RTag, the flag field is connected to the node pointer indicates 0, then the flag bit is represented by a node after a clue.

  2. data structure

    typedef struct BiThrNode
    {
        ElemType data;
        struct BiThrNode *lchild, *rchild;
        PointerTag LTag, RTag;
    } BiThrNode, *BiThrTree;
    

    Wherein, as defined PointerTag

    typedef enum PointerTag
    {
        Link,
        Thread
    } PointerTag;
    

    Link == 0 represents a pointer, Thread == 1 represents leads.

Threaded

  The essence of the trail is the list of binary null pointer to point to the predecessor or successor clues and successor or predecessor of information can only be obtained when traversing, the clues of the process is the modification of a null pointer during traversal process. So in order to record the relationship traversal has access node, attached to a pre pointer always points to the node just visited, that is, if the pointer p points to the current access node, the pre pointing to it just visited nodes.

void InThreading(BiThrTree p, BiThrTree *pre)
{
    if (p)
    {
        // 左子树线索化.
        InThreading(p->lchild, pre);

        // 前驱线索.
        // 若当前结点无左子,则修改标志域并使得 lchild 指向 p 的前驱.
        if (!(p->lchild))
        {
            p->LTag = Thread;
            p->lchild = (*pre);
        }

        // 后继线索.
        if (!((*pre)->rchild))
        {
            (*pre)->RTag = Thread;

            // 将前驱结点的后继结点设置为当前结点.
            (*pre)->rchild = p;
        }

        // 更新 pre, 使其始终指向刚访问过的结点.
        (*pre) = p;

        // 右子树线索化.
        InThreading(p->rchild ,pre);
    }

    return;
} // InThreading

  Here I encountered a problem when learning: just visited nodes pre successor clues setup fails. The book did not say pre whether pseudo-code as a global variable, and there is no data structure stored pre, so my first pre-defined as a local variable and the value passed to the InThreading, no problem at first glance, but after careful debug found when you call this form of recursive algorithm, the system maintains a function call stack, each entering a new layer of recursion, the system will save the return address of the current call, and pressed into a fresh call return address. After performing sub-function, subroutine return address is popped off the stack, this time, all modifications done before they all disappeared, pre and will not be updated.

  Improved methods are two:
    1. The pre set a global variable;
    2. pointer is passed to the value transfer.
  In this paper, the second approach.

Traversal

  After threading, can be easily obtained precursor and successor nodes in the traversal of the resulting linear sequence, it can be easily obtained linear sequence.

Status InOrderTraverse_Thr(BiThrTree T, Status (*Visit)(ElemType e))
{
    // p 指向根结点.
    BiThrNode *p = T->lchild;

    // 空树或遍历结束时.
    while (p != T)
    {
        while (p->LTag != Thread)
        {
            p = p->lchild;
        }

        if (!Visit(p->data))
        {
            return ERROR;
        }

        // 访问后继结点.
        // 找到最左叶结点, 就可以轻松地访问其后继结点.
        while ((p->RTag == Thread) && (p->rchild != T))
        {
            p = p->rchild;
            Visit(p->data);
        }

        p = p->rchild;
    }

    return OK;
} // InOrderTraverse_Thr

references

  1. Data Structure: C language version / Yan Wei Min, Wu Weimin edited - Beijing: Tsinghua University Press, 2007
  2. Threaded binary Detailed https://blog.csdn.net/s_999999/article/details/86157532
  3. Threaded binary principle and the preamble, in order threaded (Java version) https://blog.csdn.net/UncleMing5371/article/details/54176252
  4. Personal understanding of the function call stack (call stack) of https://blog.csdn.net/VarusK/article/details/83031643

Guess you like

Origin www.cnblogs.com/tianshihao/p/12663611.html