Data structure ------ tree non-recursive traversal

Here, the binary tree is taken as an example to perform tree pre-order, in-order, post-order, layer order, and delete operations of the binary tree.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct BiTNode{
    
    
     int  ch;
     struct BiTNode *Lchild,*Rchild;
}BiTNode ,*BiTree;
BiTNode *NewNode(int ch){
    
    
    BiTNode *p=(BiTNode *)malloc(sizeof(BiTNode));
    p->ch=ch;
    p->Lchild=p->Rchild=NULL;
    return p;
}
void Insert( BiTree &root,int  x){
    
    
     if(root==NULL){
    
    
        root=NewNode(x);
        return ;
     }
     if(root->ch==x){
    
    
        return;
     }
     else if(root->ch>x){
    
    
        Insert(root->Lchild,x);
     }
     else{
    
    
        Insert(root->Rchild,x);
     }
}
BiTree Create(int data[],int n){
    
    
       BiTNode *root=NULL;
       for(int i=0;i<n;i++){
    
    
         Insert(root,data[i]);
    }
    return root;
}
void PreOrder(BiTree L){
    
    
    cout<<"先序遍历非递归:";
    BiTree Stack[100];//栈的初始化过程
    int top=-1;
    BiTNode *p=L;
    while(p!=NULL||top>-1){
    
    
        if(p!=NULL){
    
    
             cout<<p->ch<<' ';
             Stack[++top]=p;
             p=p->Lchild;
        }
        else{
    
    
            p=Stack[top--];
            p=p->Rchild;
        }
    }
    cout<<endl;
}
void InOrder(BiTree root){
    
    
    cout<<"非递归中序遍历: ";
    BiTree Stack[100];
    int top=-1;
    BiTNode *p=root;
    while(p!=NULL||top>-1){
    
    
        if(p!=NULL){
    
    
            Stack[++top]=p;
            p=p->Lchild;
        }
        else{
    
    
            p=Stack[top--];
            cout<<p->ch<<' ';
            p=p->Rchild;
        }
    }
  cout<<endl;
}
void PostOrder(BiTree L){
    
    
    cout<<"后序非递归算法: ";
    BiTree p=L,r=NULL;//p是工作指针,r是用来记录上一个被访问的节点信息;
    BiTree Stack[100];
    int top=-1;
    while(p!=NULL||top>-1){
    
    
        if(p!=NULL){
    
    //如果当前孩子左孩子不为空则一直往左边走;
            Stack[++top]=p;
            p=p->Lchild;
        }
        else{
    
    
            p=Stack[top];
            if(p->Rchild!=NULL&&p->Rchild!=r){
    
    //当前节点的右孩子不为空并且其没有被访问过
                p=p->Rchild;
                Stack[++top]=p;
                p=p->Lchild;
            }
            else{
    
    
                p=Stack[top--];
                cout<<p->ch<<' ';
                r=p;//r指针存储刚刚访问过的p指针的信息
                p=NULL;
            }

        }
    }
    cout<<endl;
}
void BFS_BiTree(BiTree root){
    
    
    cout<<"数的层序遍历:  ";
    BiTree Queue[100];
    int pre=-1,rear=-1;//初始化一个队列;
    BiTree p;//p为工作指针;
    int level=0,last=1;//last为总是指向每一层的最右的节点,level为树的层次
    Queue[++rear]=root;
    while(pre<rear){
    
    
        p=Queue[++pre];
        cout<<p->ch<<' ';
        if(p->Lchild){
    
    
            Queue[++rear]=p->Lchild;
        }
        if(p->Rchild){
    
    
            Queue[++rear]=p->Rchild;
        }
        if(pre==last){
    
    
            level++;
            last=rear;
        }
    }
    cout<<endl;
    cout<<"数的层数  :"<<level<<endl;
}
int high(BiTree root){
    
    
      if(root==NULL){
    
    
        return 0;
    }
   int rhigh=high(root->Rchild);
   int lhigh=high(root->Lchild);
   return rhigh>lhigh ? rhigh+1:lhigh+1;

}
bool Search(BiTree root,int x){
    
    
    if(root==NULL){
    
    
        return false;
    }
    if(root->ch==x){
    
    
        return true;
    }
    else if(root->ch>x){
    
    
        Search(root->Lchild,x);
    }
    else{
    
    
        Search(root->Rchild,x);
    }
}
BiTNode *FindMaxNode(BiTree root){
    
    //找到以root为根的最大节点,因为二叉树的性质一直往右边就好了
    BiTNode *p=root;
    while(p->Rchild){
    
    
        p=p->Rchild;
    }
    return p;
}
BiTNode *FindMinNode(BiTree root){
    
    //找到以root为根的最小节点,因为二叉树的性质一直往左边就好了
    BiTNode *p=root;
    while(p->Lchild!=NULL){
    
    
        p=p->Lchild;
    }
    return p;
}
void DeleteNode(BiTree &L,int x){
    
    
   if(L==NULL){
    
    //如果没有找到该数据则直接返回
        return ;
   }
   if(L->ch==x){
    
    //找到该数据则进行判断
        if(L->Lchild==NULL&&L->Rchild==NULL){
    
    //如果左右子树都未空则直接节点为空
              L=NULL;
        }
        else if(L->Lchild!=NULL){
    
    //左子树不为空时就找到以左孩子为根节点的最大节点,然后来和当前需要删除的节点替换即可,就可以转化为了删除当前节点左孩子上面的节点pre
             BiTNode *pre=FindMaxNode(L->Lchild);
             L->ch=pre->ch;
             DeleteNode(L->Lchild,pre->ch);
        }
        else if(L->Rchild!=NULL){
    
    
               BiTNode *next=FindMinNode(L->Rchild);
               L->ch=next->ch;
               DeleteNode(L->Rchild,next->ch);
        }

   }
   else if(L->ch>x){
    
    
        DeleteNode(L->Lchild,x);
   }
   else{
    
    
       DeleteNode(L->Rchild,x);
   }
}
int main()
{
    
    
    int data[]={
    
    5,3,7,4,2,8,6};
    BiTree root=Create(data,7);
    PreOrder(root);
    InOrder(root);
    PostOrder(root);
    BFS_BiTree(root);
    cout<<"树的高度: "<<high(root)<<endl;
    //cout<<Search(root,8)<<' '<<Search(root,9)<<endl;
    /*测试二叉排序树的删除算法*/
    DeleteNode(root,5);
    PreOrder(root);
    InOrder(root);
    PostOrder(root);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44741914/article/details/110351523