Clue binary tree (C++ implementation)

  • The clue binary tree is to add two tag attributes to the original binary tree to record the clues of the current node
  • If ltag=0 lchild points to the left child of the node
  • If ltag=1 lchild points to the predecessor node of the node
  • If rtag=0 rchild points to the right child of the node
  • If rtag=1 rchild points to the successor of the node

Because the predecessor and successor nodes of the first, middle, and subsequent order will be different, so the binary tree cueing needs to be divided into three types of cueing binary trees, here the latter order is an example

typedef int datatype;

typedef struct BiThrNode{
    
    
    datatype data;
    BiThrNode *lchild,*rchild;
    unsigned ltag,rtag;
}BiThrNode,*BiThrTree;

BiThrTree pre=nullptr;

BiThrTree PostOrderThr(BiThrNode *p,BiThrNode *&pre){
    
       //后序线索二叉树
    if(p){
    
    
        PostOrderThr(p->lchild,pre);
        PostOrderThr(p->rchild,pre);
        if(p->lchild==nullptr){
    
    
            p->lchild=pre;
            p->ltag=1;
        }
        if(pre!=nullptr&&pre->rchild==nullptr){
    
    
            pre->rchild=p;
            pre->rtag=1;
        }
        pre=p;
    }
}

Guess you like

Origin blog.csdn.net/qq_43477024/article/details/109754627