数据结构记录--基于二叉排序树的查找

HomeWeb BoardProblemSetStandingStatusStatistics

Problem B: 基于二叉排序树的查找

Time Limit: 1 Sec  
Memory Limit: 128 MB

Submit: 960  
Solved: 366

[
Submit][
Status][
Web Board]

Description

用顺序和二叉链表作存储结构,要求用结构体BinaryTree封装二叉排序树的所有功能。根据一个输入序列,构建二叉排序树避免增加树的深度(课本7.3.1)
存在下列三种操作:
a)输入数列L,生成一棵二叉排序树T;
b)对二叉排序树T作中序遍历,输出结果;
c)输入元素x,查找二叉排序树T,若存在含x的结点,则删除该结点,并作中序遍历(执行操作b);否则输出信息”x doesn’t exist”;

Input

输入包含多组,每一组第一行两个数n q(1 <= n <= 500 1 <= q <= 500)表示序列长度和操作数;第二行输入n个数,用于构造二叉排序树; 接下来q行每行一个数x查找二叉排序树T,若存在含x的结点,则删除该结点,并作中序遍历,否则输出信息”x doesn’t exist”;输入必须保证序列没有两个相同的数。

Output

对于每组样例先输出一行排序树中序遍历的结果;
对于每个查询x,若存在含x的结点,则删除该结点,并作中序遍历、输出结果;否则输出信息”x doesn’t exist” 。注意,每个查询输出占一行。

Sample Input

5 51 5 2 4 339447

Sample Output

1 2 3 4 51 2 4 59 doesn’t exist1 2 54 doesn’t exist7 doesn’t exist

HINT

Append Code

[
Submit][
Status][
Web Board]

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
static int flag=0;
static  int n,q;
typedef struct Node
{
    int data;
    struct Node *lchild;
    struct Node *rchild;
} NODE,*BSTree;
void insert(BSTree &T,int key)
{
    if(!T)
    {
       BSTree S=new Node;
        S->data=key;
        S->lchild=S->rchild=NULL;
        T=S;
    }
    else if(key<T->data)
    {
        insert(T->lchild,key);
    }
    else if(key>T->data)
    {
        insert(T->rchild,key);
    }
}

BSTree search(BSTree pTree,int key)
{
    if(!pTree || pTree->data == key)
        return pTree;
    else if(key < pTree->data)
        return search(pTree->lchild,key);
    else
        return search(pTree->rchild,key);
}
void delete_Node1(BSTree &p)
{
    BSTree q,s;
    if(!p->lchild)
    {
        q = p;
        p = p->rchild ;
        free(q);
    }
    else if(!p->rchild)
    {
        q = p;
        p = p->lchild;
        free(q);
    }
    else
    {
        s = p->lchild;
        while(s->rchild)
            s = s->rchild;
        s->rchild = p->rchild;
        q = p;
        p = p->lchild;
        free(q);
    }
}
bool delete_BSTree(BSTree &pTree,int key)
{
    if(!pTree)
        return false;
    else
    {
        if(key == pTree->data)
        {
            delete_Node1(pTree);
            return true;
        }
        else if(key < pTree->data)
            return delete_BSTree(pTree->lchild,key);
        else
            return delete_BSTree(pTree->rchild,key);
    }
}

void in_traverse(BSTree pTree)
{

    if(pTree)
    {
        if(pTree->lchild)
        {
            in_traverse(pTree->lchild);
        }
        if(flag==0)
        {
            printf("%d",pTree->data);
            flag=1;
        }
        else
        {
            printf(" %d",pTree->data);
        }
        if(pTree->rchild)
        {
            in_traverse(pTree->rchild);
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        BSTree pTree = NULL;
        //create_BSTree(pTree,n);
       pTree = NULL;
    int key;
    while(n--)
    {
        scanf("%d",&key);
        insert(pTree,key);
    }
        flag=0;
        in_traverse(pTree);
        printf("\n");
       while(q--)
        {
            flag=0;
            int key;
            scanf("%d",&key);
            if(search(pTree,key))
            {
                if(delete_BSTree(pTree,key))
                {
                    in_traverse(pTree);
                    printf("\n");
                }
            }
            else
            {
                printf("%d doesn't exist\n",key);
            }
        }
    }
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/xbw12138/article/details/50451392
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞