二叉树删除,重建,交换

1.删除以元素值x为根结点的子树,并释放其空间

#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct BTreeNode
{
	int data;
	struct BTreeNode *lchild,*rchild;
}BTree;
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}
void DeleteXTree (BTree *bt)//删除以bt为根的字树
{
	DeleteXTree(bt->lchild);
	DeleteXTree(bt->rchild);
	free(bt);
}
void Search(BTree *bt,int x)
{
	if(bt)
	{
		if(bt->data==x)DeleteXTree(bt);
		else
		{
		Search(bt->lchild,x);
		Search(bt->rchild,x);
		}
	}
}

 2.交换左右孩子结点

1)递归方法,中序遍历不适合本题

void exchange(BTree *bt)
{
	if(bt)
	{
		BTree *s;
		s=bt->lchild;bt->lchild=bt->rchild;bt->rchild=s;//左右子女交换
		exchange(bt->lchild);//交换左子树上所有结点的左右子树
		exchange(bt->rchild);//交换右子树上所有结点的左右子树
	}
}

 2)非递归方法

void exchange(BTree *bt)
{
	int top=-1;//栈顶指针
	BTree *s[100],*p;
	if(bt)
	{
		s[++top]=bt;//入栈
		while(top>-1)
		{
			bt=s[top--];
			if(bt->lchild||bt->rchild)
			{
			    p=bt->lchild;bt->lchild=bt->rchild;bt->rchild=p;//左右子女交换
			}
		
			if(bt->lchild)s[++top]=bt->lchild;//交换左子树上所有结点的左右子树
			if(bt->rchild)s[++top]=bt->rchild;//交换右子树上所有结点的左右子树
		}	
	}
}

 3.前序和中序建立二叉树

#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct BTreeNode
{
	int data;
	struct BTreeNode *lchild,*rchild;
}BTree;
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}
void PreInCreate(BTree *root,int pre[100],int in[100],int l1,int h1,int l2,int h2)//l1,h1,l2,h2为两个序列的首尾元素下标
{
	root=(BTree*)malloc(sizeof(BTreeNode));
	root->data=pre[l1];//根结点
	int i;
	for(i=l2;i<=h2;i++)if(in[i]==pre[l1])break;//找到中序的根结点,分解左右子树
	if(i==l2)//无左子树
	{
		root->lchild=NULL;
	}
	else
	{
		PreInCreate(root->lchild,pre,in,l1+1,l1+(i-l2),l2,i-1);//建立左子树
	}
	if(i==h2)//无右子树
	{
		root->rchild=NULL;
	}
	else
	{
		PreInCreate(root->rchild,pre,in,l1+(i-l2)+1,h1,i+1,h2);//建立右子树
	}
}

 4.中序和后续建立二叉树

void InPostCreate(BTree *root,int in[100],int post[100],int l1,int h1,int l2,int h2)//l1,h1,l2,h2为两个序列的首尾元素下标
{
	root=(BTree*)malloc(sizeof(BTreeNode));
	root->data=post[h2];//根结点
	int i;
	for(i=l1;i<=h1;i++)if(in[i]==post[h2])break;//找到中序的根结点,分解左右子树
	if(i==l1)//无左子树
	{
		root->lchild=NULL;
	}
	else
	{
		InPostCreate(root->lchild,in,post,l1,i-1,l2,l2+i-l1-1);//建立左子树
	}
	if(i==h1)//无右子树
	{
		root->rchild=NULL;
	}
	else
	{
		InPostCreate(root->rchild,in,post,i+1,h1,l2+i-l1,h2-1);//建立右子树
	}
}

5.前序和后序建立二叉树

分析:前序的第一个是根结点,若无其他节点,则该结点是为叶子。否则该结点必有左右子树,且根结点的第一个结点就是左子树的根,到后序序列中去寻找这个左子树的根,它将后序序列分为两部分:左部分(包括所查到的结点)是二叉树的左子树(可能为空),右部分(除去最后的根结点)则是右子树(可能为 空)。这样,在确定根结点后,可递归确定左右子树。

void PrePostCreate(BTree *root,int pre[100],int post[100],int l1,int h1,int l2,int h2)//l1,h1,l2,h2为两个序列的首尾元素下标
{
	BTree *p=root;
	if(l1<=h1)
	{
		p=(BTree*)malloc(sizeof(BTreeNode));
		p->data=pre[l1];//根结点
		if(l1==h1)//只有一个叶结点的二叉树
		{
			p->lchild=p->rchild=NULL;
		}
		else
		{
			int i;
			for(i=l2;i<=h2;i++)
			{
				if(post[i]==pre[l1+1])break;
			}
			int L=i-l2+1;//左子树结点数
			PrePostCreate(root->lchild,pre,post,l1+1,l1+L,l2,i);//建立左子树
			PrePostCreate(root->lchild,pre,post,l1+L+1,h1,i+1,h2-1);//建立右子树	
		}
	}
}

 

posted @ 2012-08-15 20:42  代码改变未来  阅读(417)  评论(0编辑  收藏  举报