⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2796474_re.cpp

📁 北大大牛代码 1240道题的原代码 超级权威
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INIT (node *)malloc(sizeof(node))

struct node
{
	char name[21];
	node *l, *r;
};

node *root;

void print(node *t,int n)
{
	int i;

	if(t==NULL)
		return ;
	for(i = 0; i < n; i++)
		printf("+");
	printf("%s\n",t->name);
	if(t->l)
		print(t->l,n+1);
	if(t->r)
		print(t->r,n);
}

int hire(node *t,char str[],char tmp[])
{
	if(t==NULL)
		return 0;
	if(strcmp(t->name,str)==0)
	{
		node *p;
		p = INIT;
		strcpy(p->name,tmp);
		p->l = p->r = NULL;
		if(t->l)
		{
			t = t->l;
			while(t->r)
				t = t->r;
			t->r = p;
		}
		else
			t->l = p;
		return 1;
	}
	else
		return hire(t->l,str,tmp)||hire(t->r,str,tmp);
}

int fire(node *t,char str[],node *q,char ch)
{
	node *p;

	if(t==NULL)
		return 0;
	if(strcmp(t->name,str)==0)
	{
		if(t->l==NULL&&t->r==NULL)
		{
			if(t==q)
				root = NULL;
			else
			{
				if(ch=='l')
					q->l = NULL;
				else
					q->r = NULL;
			}
			return 1;
		}
		while(t->l)
		{
			strcpy(t->name,t->l->name);
			p = t;
			t = t->l;
		}
		p->l = t->r;
		return 1;
	}
	else
		return fire(t->l,str,t,'l')||fire(t->r,str,t,'r');
}

int main()
{
	char tmp[21], str[21];

	root = INIT;
	scanf("%s",root->name);
	root->l = root->r = NULL;
	while(scanf("%s",tmp)==1)
	{
		if(strcmp(tmp,"print")==0)
		{
			print(root,0);
			puts("------------------------------------------------------------");
		}
		else
			if(strcmp(tmp,"fire")==0)
			{
				scanf("%s",tmp);
				fire(root,tmp,root,'Y');
			}
			else
			{
				scanf("%s",str);
				scanf("%s",str);
				hire(root,tmp,str);
			}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -