二叉树的创建方法【递归】【循环】【插入】

二叉树是一种比较常用的数据结构,这里介绍三种创建二叉树的方法:
第一:递归创建无序二叉树。
include <stdio.h>
#include <stdlib.h>
#define len sizeof(struct bintree)
struct bintree    //二叉树的节点结构体,只存储一个int的数据;
{
	int data;
	struct bintree *lchild;
	struct bintree *rchild;
};
struct bintree* create()   //建立二叉树,主要采用递归的思想,因为你不知到二叉树的节点个数;不能用迭代,迭代必须要知道最后一个的值;
{
	int data; 
	struct bintree *T;
	scanf("%d",&data);//【注意最后结束时的输入情况,每个叶子结点都必须判断是否为0】
	if(data==0)
		T=NULL;
	else
	{
		T=(struct bintree *)malloc(len);
		T->data=data;
		T->lchild=create();
		T->rchild=create();
	}
	return T;     //返回头节点;
}
C++实现:
Binarytree * createsequence()
{
	Binarytree *root=NULL;
	int number;
	cout<<"please input the data:"<<endl;
	cin>>number;
	if(0==number)
		return NULL;
	else
	{
		root=new Binarytree;
		root->data=number;
		root->pLeft=createsequence();
		root->pRight=createsequence();
		return root;
	}
}
第二:循环创建排序二叉树【可以将数组作为输入数据】;
void CreateTreeLoop(Binarytree **tree,int data)
{
	Binarytree *pNode=new Binarytree;
	pNode->data=data;
	pNode->pLeft=pNode->pRight=NULL;
	if(NULL==*tree)
		*tree=pNode;
	else
	{
		Binarytree *back=NULL;
		Binarytree *current=*tree;
		while(current!=NULL)
		{
			back=current;
			if(current->data>data)
				current=current->pLeft;
			else
				current=current->pRight;
		}
		if(back->data>data)
			back->pLeft=pNode;
		else
			back->pRight=pNode;
	}
}
for(int i=0;i<length;i++)
{
	CreateTreeLoop(&pRoot1,arr1[i]);
}
第三:插入创建排序二叉树【可以将数组作为输入数据】;
void inserttree(Binarytree *tree,int value)
{
	//看是放在左边还是右边;
	if(tree->data > value)//左边
	{
		if(NULL==tree->pLeft)
		{
			Binarytree *pNode=new Binarytree;
			pNode->data=value;
			pNode->pLeft=pNode->pRight=NULL;
			tree->pLeft=pNode;
		}
		else
		{
			inserttree(tree->pLeft,value);
		}
	}
	else
	{
		if(NULL==tree->pRight)
		{
			Binarytree *pNode=new Binarytree;
			pNode->data=value;
			pNode->pLeft=pNode->pRight=NULL;
			tree->pRight=pNode;
		}
		else
		{
			inserttree(tree->pRight,value);
		}
	}
}
void CreateTree(Binarytree **tree,int *arr,int length)
{
	for(int i=0;i<length;i++)
	{
		if(NULL==*tree)
		{
			Binarytree *pNode=new Binarytree;
			pNode->data=arr[i];
			pNode->pRight=pNode->pLeft=NULL;
			*tree=pNode;
		}
		else
			inserttree(*tree,arr[i]);
	}
}

二叉树的建立及其前、中、后序遍历代码实现如下:

void PreOrderTraverse(struct bintree *T)   //前序遍历;
{
	if(T)
	{
		printf("%d ",T->data);
		PreOrderTraverse(T->lchild);
		PreOrderTraverse(T->rchild);
	}
}
void InOrderTraverse(struct bintree *T)  //中序遍历;
{
	if(T)
	{
		InOrderTraverse(T->lchild);
		printf("%d ",T->data);
		InOrderTraverse(T->rchild);
	}
}
void PostOrderTraverse(struct bintree* T)  //后续遍历;
{
	if(T)
	{
		PostOrderTraverse(T->lchild);
		PostOrderTraverse(T->rchild);
		printf("%d ",T->data);
	}
}
二叉树的销毁:

void Destory(struct bintree *T)    //销毁二叉树,释放所分配的内存,用了malloc()函数后就一定要用free()函数,这是一个习惯;
{
	if(T!=NULL)
	{
		Destory(T->lchild);
		free(T->lchild);
		Destory(T->rchild);
		free(T->rchild);
	}
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值