提高题3-2

3-2. Merge two binary sorting trees using post-order traversal thought.

后序遍历合并后的二叉搜索树。

#include<iostream>
using namespace std;
int mx=0; //全局变量
typedef struct BTNode
{
    int element;
    struct BTNode *lChild,*rChild;
}BTNode,*BTree;//二叉树节点*/

void MakeTree(BTree *T) //前序创建树
{
	int m;
	cin>>m;
	if (-1==m)  *T=NULL;
	else
	{
		(*T)=new BTNode;
		(*T)->element=m;
		MakeTree(& (*T)->lChild);
	    MakeTree(& (*T)->rChild);
	}
}
void LDR(BTree T)     //后序输出
{
	if(T)
	{
		LDR(T->lChild);
		LDR(T->rChild);
		cout<<T->element<<" ";
	}
}
 

void InsertBT(BTree *T,int key) //某一个元素插入进来,根据此位置有啥来分分配到对应的地方去
{
	while((*T)!=NULL)
	{
		if(key<(*T)->element) T=&(*T)->lChild;
		else if (key>(*T)->element) T=& (*T)->rChild;
		else if (key=(*T)->element)
		{
			mx=1;
			break;
		}
	}
    (*T)=new BTNode;
    (*T)->lChild=NULL;
    (*T)->rChild=NULL;
    (*T)->element=key;

}
void InsertRTL(BTree T1,BTree T2) 
{
    if(T2)
    {
        InsertRTL(T1,T2->lChild);
	    InsertBT(&T1,T2->element);
		InsertRTL(T1,T2->rChild);
    }
	

}
int main()
{

    BTree T1,T2;
	cout<<"请输入第一棵二叉排序树"<<endl;
    MakeTree(&T1);
    cout<<"请输入第二棵二叉排序树"<<endl;
    MakeTree(&T2);
    InsertRTL(T1,T2);
	if(mx==0)
	{
		cout<<"得到后序遍历二叉排序树"<<endl;
	    LDR(T1);
	}
	else
	{
		cout<<"有重复数字无法构成二叉排序树";
	}
	cout<<endl;
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值