数据结构-二叉排序树(二叉查找树or二叉线索数)

#include
// #include
#include
#include
typedef int DataType;
typedef struct BST_node{
       
    DataType data;
    struct BST_node *lchild,*rchild;

}BST_T;

BST_T* Search_BST(BST_T** root,DataType key)
{
     BST_T *p =*root;

     while(p)
     {
        if(p->data==key)  return p;
        p=(key < p->data)?p->lchild:p->rchild;
     }
   return NULL;

}
void Insert_BST(BST_T** T,DataType data)
{

    BST_T *p_bst =(BST_T*)malloc(sizeof(BST_T));

    if(!p_bst) return;

    p_bst->data =data;
    p_bst->lchild=p_bst->rchild=NULL;
    //empty tree
    if((*T)==NULL)
    {
        *T = p_bst;
         return;
    }
    //是否存在,已存在则返回,不插入
    if (Search_BST(T, data) != NULL) return; 
    //insert 
    BST_T* tnode=NULL; BST_T *troot=(*T);

    while(troot)
    {
        tnode = troot;
        troot =(datadata)? troot->lchild:troot->rchild;

    }
    if(data < tnode->data)
       tnode->lchild =p_bst;

    else
       tnode->rchild =p_bst;


}
void CreateBST(BST_T** T,int a[],int n)
{

  int i;
  for(i=0;ilchild);
        //cout << T->data << " ";
        printf("%d\t",T->data);
        MidOrderTraverse(T->rchild);
    }
}
void PostOrderTraverse(BST_T* T)
{
    if (T)
    {
        PostOrderTraverse(T->lchild);
        PostOrderTraverse(T->rchild);
        printf("%d\t",T->data);
    }
}
void PreOrderTraverse(BST_T* T)
{
    if (T)
    {
        printf("%d\t",T->data);
        PreOrderTraverse(T->lchild);
        PreOrderTraverse(T->rchild);
    }
}
void DeleteBSTNode(BST_T** root,DataType data)
{
    
  BST_T *p = *root;
  BST_T *parent =NULL;
  BST_T *s =NULL;
  
  if (!(*root)) return;

  if(p->data == data)
  {
     //删除的是叶子节点
     if(p->lchild==NULL && p->rchild==NULL)

        *root =NULL;
     //只有一个子节点
     else if(!p->rchild&&p->lchild)
        *root =p->lchild;
 
     else if (!p->lchild&&p->rchild)  
        *root =p->rchild;  //为了退出递归

    //左子树右子树都有
     else{
           
        s = p->rchild;
        //删除倒数第二层的节点
        if(!s->lchild)
            s->lchild =p->lchild;
        //删除倒数第二层往上的节点
        else{

            while(s->lchild)
            {
                parent = s;
                s= s->lchild;

            }

            parent->lchild =s->rchild;
            s->lchild =p->lchild;
            s->rchild = p->rchild;
            
        }
        (*root) = s;

     }
       free(p);

  }

  else if(data > p->data)
      DeleteBSTNode(&(p->rchild),data);
  else if(data < p->data)
      DeleteBSTNode(&(p->lchild),data);
     
}
int main()
{
    int arr[] = { 17,12,19,10,15,18,25,8,11,13,16,22};  
    BST_T *root =NULL;
   
    CreateBST(&root,arr,12);
    //printf("\nCreate BST: ");

    // printf("\nMid order traverse: ");
    // MidOrderTraverse(root);

    printf("\npre order traverse: ");
    PreOrderTraverse(root);

    // printf("\npost order traverse: ");
    // PostOrderTraverse(root);

    //查找某节点
    BST_T *result = Search_BST(&root, 12);

    printf("查找结果:%d\n",result->data);
    //删除二叉排序树中的节点12
    DeleteBSTNode(&root, 8);

    printf("\npre order traverse: ");
    PreOrderTraverse(root);
    // printf("\npost order traverse: ");
    // PostOrderTraverse(root);
    // printf("\n");


}

你可能感兴趣的:(数据结构,数据结构)