c++实现二叉搜索树(BST) 递归实现 采用结构体数组/指针两种方式 (有插入,查找,删除,前序遍历中序遍历

模板题为:Aizu Online Judge-Binary Search Tree III 

递归实现好麻烦为什么不用循环啊喂!!

1.结构体数组实现:

#include<cstdio>
#include<cstring>
using namespace std;

const int N = 500005;

struct BST_Node
{
    int left, right, val;
}tree[N];

void insert(int t, int &r)
{
    if(r == 0)
    {
        //递归到相应位置将节点加入
        r = t;
        return;
    }
    if(tree[t].val>tree[r].val)
    {
        insert(t, tree[r].right);
    }
    if(tree[t].val < tree[r].val)
    {
        insert(t, tree[r].left);
    }
}

void add(int x, int &root, int &tot)
{
    //创建节点
    tot++;
    tree[tot].left = 0;
    tree[tot].right = 0;
    tree[tot].val = x;
    insert(tot, root);
}

void search(int v, int r)
{
    if(r == 0)
    {
    	printf("no\n");
        return ;
    }
    if(v == tree[r].val)
    {
    	printf("yes\n");
        return ;
    }
    if(v < tree[r].val)
    {
        search(v, tree[r].left);
    }
    if(v > tree[r].val)
    {
        search(v, tree[r].right);
    }
}

int find_r(int &r)
{
    if(tree[r].right == 0)
    {
        int rr = r;
        r = 0;
        return rr;
    }
    else
    {
        return find_r(tree[r].right);
    }
}

int find_l(int &r)
{
	if(tree[r].left == 0)
	{
		int rr = r;
		r = 0;
		return rr;
	}
	else
	{
		return find_l(tree[r].left);
	}
} 

void del(int x, int &r)
{
    if(x < tree[r].val)
    {
        del(x, tree[r].left);
    }
    else if(x > tree[r].val)
    {
        del(x, tree[r].right);
    }
    else
    {
        if(tree[r].left == 0 && tree[r].right == 0)
        {
            r = 0;
        }
        else if(tree[r].left == 0)
        {
            r = tree[r].right;
        }
        else if(tree[r].right == 0)
        {
            r = tree[r].left;
        }
        else
        {
//            int temp = find_r(tree[r].left);
			int temp = find_l(tree[r].right);
//            printf("temp:%d tree[r].left:%d", temp, tree[r].left);
            int left = tree[r].left;
            int right = tree[r].right;
            r = temp;
            tree[r].left = left;
            tree[r].right = right;
        }
    }
}

void inorder(int r)
{
    if(r != 0)
    {
        inorder(tree[r].left);
        printf(" %d", tree[r].val);
        inorder(tree[r].right);
    }
}

void preorder(int r)
{
	if(r != 0)
	{
		printf(" %d", tree[r].val);
		preorder(tree[r].left);
        preorder(tree[r].right);
	}
}

int main()
{
    int n;
    scanf("%d", &n);
    int root = 0;//根节点
    int tot = 0;//tot保证节点不重复
    while(n--)
    {
        char ch[10];
        scanf("%s", ch);
        if(strcmp(ch,"print") == 0)
        {
			inorder(root);
			printf("\n");
			preorder(root);
			printf("\n");
        }
        else if(strcmp(ch, "find") == 0)
        {
        	int x;
        	scanf("%d", &x);
        	search(x, root);
		}
		else if(strcmp(ch, "delete") == 0)
		{
			int x;
			scanf("%d", &x);
			del(x, root);
		}
        else
        {
            int x;
            scanf("%d", &x);
            add(x, root, tot);
        }
    }
//    printf("%d", search(1, root));

    return 0;
}

2.结构体指针实现:

#include<cstdio>
#include<cstring>
using namespace std;

struct BSTNode{
    BSTNode* left;
    BSTNode* right;
    int val;

    BSTNode(int vall = 0)
    {
        left = NULL;
        right = NULL;
        val = vall;
    }

};

BSTNode* root = NULL;

void insert(int x, BSTNode* &r)
{
    if(r == NULL)
    {
        r = new BSTNode(x);
        return ;
    }

    if(r->val < x)
    {
        insert(x, r->right);
    }
    else if(r->val > x)
    {
        insert(x, r->left);
    }
}

void find(int x, BSTNode* r)
{
    if(r == NULL)
    {
        printf("no\n");
        return;
    }
    if(r->val == x)
    {
        printf("yes\n");
        return;
    }
    if(x < r->val)
    {
        find(x, r->left);
    }
    else if(x > r->val)
    {
        find(x, r->right);
    }
}

void preorder(BSTNode *r)
{
	if(r != NULL)
	{
		printf(" %d", r->val);
		preorder(r->left);
		preorder(r->right);
	}
}

void inorder(BSTNode *r)
{
	if(r != NULL)
	{
		inorder(r->left);
		printf(" %d", r->val);
		inorder(r->right);
	}
}

int find_l(BSTNode* &r)
{
    if(r->left == NULL)
    {
    	int v = r->val;
    	r = NULL;
        return v;
    }
    else
    {
        return find_l(r->left);
    }
}

void del(int x, BSTNode* &r)
{
    if(x < r->val)
    {
        del(x, r->left);
    }
    else if(x > r->val)
    {
        del(x, r->right);
    }
    else
    {
        if(r->left == NULL && r->right == NULL)
        {
        	r = NULL;
//            delete r;
        }
        else if(r->left == NULL)
        {
            r = r->right;
//            printf("a %d", r->val);
        }
        else if(r->right == NULL)
        {
            r = r->left;
            
        }
        else
        {
            int temp = find_l(r->right);
//            printf("aaa:%d", temp->val);
            r->val = temp;
        }
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    while(n--)
    {
        char ch[10];
        scanf("%s", ch);
        if(strcmp(ch, "print") == 0)
        {
			inorder(root);
			printf("\n");
			preorder(root);
			printf("\n");
        }
        else if(strcmp(ch, "find") == 0)
        {
            int x;
            scanf("%d", &x);
            find(x, root);
        }
        else if(strcmp(ch, "delete") == 0)
        {
        	int x;
        	scanf("%d", &x);
        	del(x, root);
		}
        else
        {
            int x;
            scanf("%d", &x);
            insert(x, root);
        }
    }
    return 0;
}

ps:这题的del是用的替换右子树的最小节点(卡了有一小会呜呜

pps:aoj体验感觉好好!还有测试数据呜呜呜

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值