2015计算机学科夏令营上机考试H:Falling Leaves(搜索二叉树——重建、遍历)

在这里插入图片描述
在这里插入图片描述

题目大意

迭代删除“搜索二叉树”的叶子结点,直至根结点。要求重建该“搜索二叉树”,并输出其先序遍历结果。

思路分析

逆向考虑。从根结点到叶子结点(从后向前)进行结点插入,重建“搜索二叉树”,再先序遍历。

相关知识——二叉搜索树

数据结构
struct Node
{
	int data;
	Node* lchild;
	Node* rchild;
};
1. 结点构建
Node* newnode(int data)
{
	Node* node = new Node;
	node -> data = data;
	node -> lchild = NULL;
	node -> rchild = NULL;
	return node;
};
2. 插入操作
void insert(Node* &root, int data) // 一定要加引用&
{
	if(root == NULL) // 不存在权值为x的结点,则在该位置新建结点插入
	{
		root = newnode(data);
		return ;
	}
	if(data == root->data)
	{
		return ;
	}
	else if(data < root->data)
	{
		insert(root->lchild, data);
	}
	else
	{
		insert(root->rchild, data);
	}
}
3. 二叉搜索树建立
Node* Create(int data[], int n)
{
	Node* root = NULL;
	for(int i=0; i<n; i++)
	{
		insert(root, data[i]);
	}
	return root;
}
4. 搜索操作
void search(Node* root, int x)
{
	if(root == NULL) // 不存在权值为x的结点
	{
		printf("search failed\n");
		return ;
	}
	if(x == root->data)
	{	
		printf("%d\n", root->data);
	}
	else if(x < root->data)
	{
		search(root->lchild, x);
	}
	else
	{
		search(root->rchild, x);
	}
}
5. 删除操作
// 寻找以root为根结点的树中的最大权值结点
Node* findMax(Node* root)
{
	while(root->rchild != NULL)
	{
		root = root->rchild;
	}
	return root;
}

// 寻找以root为根结点的树中的最小权值结点
Node* findMin(Node* root)
{
	while(root->lchild != NULL)
	{
		root = root->lchild;
	}
	return root;
}
void deletenode(Node* root, int x)
{
	if(root == NULL) // 不存在权值为x的结点
	{
		return ;
	}
	if(x == root->data)
	{
		if(root->lchild==NULL && root->rchild==NULL) // 删除的是叶子结点
		{
			root = NULL; // 把root地址设为NULL,父结点就引用不到它了。
		}
		else if(root->lchild != NULL) // 删除的结点具有左孩子——寻找其前驱
		{
			Node* pre = findMax(root->lchild);
			root->data = pre->data;
			deletenode(root->lchild, pre->data);
		}
		else // 删除的结点具有右孩子——寻找其后继
		{
			Node* next = findMin(root->rchild);
			root->data = next->data;
			deletenode(root->rchild, next->data);
		}
	}
	else if(x < root->data)
	{
		delete(root->lchild, x);
	}
	else 
	{
		delete(root->rchild, x);
	}
}

代码

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

struct Node
{
    char data;
    Node* lchild;
    Node* rchild;
};

char data_[30];

Node* newNode(int data)
{
    Node* node = new Node;
    node -> data = data;
    node -> lchild = NULL;
    node -> rchild = NULL;
    
    return node;
}

void insert(Node* &root, char data)
{
    if(root == NULL)
    {
        root = newNode(data);
        return ;
    }
    if(data == root->data)
    {
        return ;
    }
    else if(data < root->data)
    {
        insert(root->lchild, data);
    }
    else 
    {
        insert(root->rchild, data); 
    }
}

void preorder(Node* root)
{
    if(root == NULL)
    {
        return ;
    }
    cout << root->data;
    preorder(root->lchild);
    preorder(root->rchild);
}

int main()
{
    freopen("input.txt", "r", stdin);
    char input;
    int num = 0; // 记录结点个数
    Node* root = NULL;
    while(cin >> input)
    {
        if(input == '$')
        {
            for(int i=num-1; i>=0; i--)
            {
                insert(root, data_[i]);
            }
            preorder(root);
            cout << endl;
            break;
        }
        else if(input == '*')
        {
            for(int i=num-1; i>=0; i--)
            {
                insert(root, data_[i]);
            }
            preorder(root);
            cout << endl;
            num = 0;
            root = NULL;
        }
        else 
        {
            data_[num++] = input; 
        }
    }
    fclose(stdin);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值