C++ implements a simple binary tree? This is enough!

Some concepts

  • Full binary tree: All branch nodes have left and right subtrees, and all leaves are on the bottom layer.
  • Complete binary tree: Leaf nodes can only appear in the lowest and second lowest levels, and the lowest leaf nodes are concentrated in the left part of the tree.

Create & initialize binary tree

typedef int datatype;

typedef struct bitnode{
    
    
    datatype data;
    struct bitnode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Inatiate(){
    
              //初始化一个带头结点的二叉树
    BiTNode *bt;
    bt=(BiTNode*)malloc(sizeof(BiTNode));
    bt->lchild=NULL;
    bt->rchild=NULL;
    return bt;
}

BiTree Create_BiTree(datatype x,BiTree lbt,BiTree rbt){
    
      //创建一个以x为根节点,lbt,rbt分别为左右子节点的二叉树
    BiTree p;
    p=(BiTNode*)malloc(sizeof(BiTNode));
    p->data=x;
    p->lchild=lbt;
    p->rchild=rbt;
    return p;
}

BiTree Insert_Bitree(BiTree bt,datatype x,BiTree parent){
    
     //在二叉树bt中的parent所指节点和其左子树之间插入数据元素为x的节点
    BiTree p;
    p=(BiTNode*)malloc(sizeof(BiTNode));
    p->data=x;
    p->lchild=NULL;
    p->rchild=NULL;
    if(parent->lchild=NULL)
        parent->lchild=p;
    else{
    
    
        p->lchild=parent->lchild;
        p->rchild=parent->rchild;
    }
    return bt;
}

BiTree Delete_Bitree(BiTree bt,BiTree parent){
    
       //在二叉树bt中删除parent的左子树
    BiTree p;
    p=parent->lchild;
    parent->lchild=NULL;
    free(p);    //仅删除所删子树根节点的空间
    return bt;
}

Three kinds of traversal of binary tree

Recursive version

//遍历递归算法

void Visit(BiTree bt){
    
         //访问结点
    std::cout<<"当前结点的值为"<<bt->data<<std::endl;
}

void PreOrder(BiTree bt){
    
        //先序遍历
    if(bt==NULL)
        return;
    Visit(bt);
    PreOrder(bt->lchild);
    PreOrder(bt->rchild);
}

void InOrder(BiTree bt){
    
          //中序遍历
    if(bt==NULL)
        return;
    InOrder(bt->lchild);
    Visit(bt);
    InOrder(bt->rchild);
}

void PostOrder(BiTree bt){
    
          //后序遍历
    if(bt==NULL)
        return;
    PostOrder(bt->lchild);
    PostOrder(bt->rchild);
    Visit(bt);
}

Non-recursive version

//非递归实现遍历

void NRPreOrder(BiTree bt){
    
         //先根 再左孩子 再右孩子
    stack<BiTNode *> mystack;
    mystack.push(bt);
    while(!mystack.empty()){
    
    
        BiTNode *top=mystack.top();
        mystack.pop();
        if(top!=NULL){
    
    
            Visit(top);
            mystack.push(top->rchild);
            mystack.push(top->lchild);
        }
    }

}

void NRInOrder(BiTree bt){
    
          //先访问左孩子 再根 再右孩子
    stack<BiTNode *> mystack;
    BiTNode *p=bt;
    while(p||!mystack.empty()){
    
    
        while(p){
    
    
            mystack.push(p);
            p=p->lchild;       
        }
        p=mystack.top();
        mystack.pop();
        Visit(p);
        p=p->rchild;
    }
}

void NRPostOrder(BiTree bt){
    
        //先访问左孩子 再右孩子 再根
    stack<BiTNode *> mystack;
    BiTNode *p=bt;
    BiTNode *last=NULL;     //标记上一次访问结点
    while(p!=NULL||!mystack.empty()){
    
    
        if(p!=nullptr){
    
    
            mystack.push(p);
            p=p->lchild;
        }
        else{
    
    
            p=mystack.top();
            if(p->rchild==nullptr||p->rchild==last){
    
    
                Visit(p);
                mystack.pop();
                last=p;
                p=nullptr;
            }
            else{
    
    
                p=p->rchild;
            }
        }
    }
}

Sequence traversal of queue implementation

void LevelOrder(BiTree bt){
    
             //层序遍历
    queue<BiTNode *> myqueue;
    if(bt==nullptr)
        return ;
    myqueue.push(bt);
    while(!myqueue.empty()){
    
    
        BiTNode *p=myqueue.front();
        Visit(p);
        myqueue.pop();
        if(p->lchild!=nullptr){
    
    
            myqueue.push(p->lchild);
        }
        if(p->rchild!=nullptr){
    
    
            myqueue.push(p->rchild);
        }
    }
}

Guess you like

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