文章

59

粉丝

0

获赞

340

访问

14.0k

头像
二叉排序树2 题解:Insert one by one
P1411 华中科技大学机试题
发布于2026年3月11日 22:27
阅读数 464

#include <iostream>
using namespace std;

int pointer;
typedef struct BTNode{
	int value;
	struct BTNode *lc,*rc;
}BTNode,*BST;
void InsertTree(BST &T,int x)
{
	if(T==NULL)
	{
		T=new BTNode;
		T->value=x;
		T->lc=NULL;
		T->rc=NULL;
		return;
	}
	if(T->value==x)
		return;
	else if(T->value>x)
		InsertTree(T->lc,x);
	else
		InsertTree(T->rc,x);
}
void Preorder(BST T)
{
	if(T)
	{
		cout<<T->value<<" ";
		Preorder(T->lc);
		Preorder(T->rc);
	}
}
void Inorder(BST T)
{
	if(T)
	{
		Inorder(T->lc);
		cout<<T->value<<" ";
		Inorder(T->rc);
	}
}
void Postorder(BST T)
{
	if(T)
	{
		Postorder(T->lc);
		Postorder(T->rc);
		cout<<T->value<<" ";
	}
}
void DeleteTree(BST &T)
{
	if(T)
	{
		DeleteTree(T->lc);
		DeleteTree(T->rc);
		delete T;
	}
}
int main()
{
	int n;
	while(cin>>n)
	{
		BST T=NULL;
		for(int i=0;i<n;...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发