二叉树的创建和前序,中序,后序遍历

第一种写法:

使用指针的指针初始化

#include<iostream>
#include<assert.h>
using namespace std;
 

typedef struct Lnode
{
	char data;
	struct Lnode *LeftChild;
	struct Lnode *RightChild;
}BiTreeNode;


//*head表示指向申请的内存区的指针
void Initiate(BiTreeNode **head)
{
	(*head)=(BiTreeNode *)malloc(sizeof(BiTreeNode));
	assert((*head)!=NULL); //if the judging conditio is true,this statement is not executed
	(*head)->LeftChild=NULL;//如果指针没确切地址值,这句报错
	(*head)->RightChild=NULL;
}

//插入左孩子节点

BiTreeNode * InsertLeftChild(BiTreeNode *head,char a )
{
	if(head==NULL) return NULL;
	BiTreeNode * p,*t;
	t=head->LeftChild;
	p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
	assert(p!=NULL);
	p->data=a;
	p->RightChild=NULL;
	p->LeftChild=t;
	head->LeftChild=p;
	return head->LeftChild;
}
//插入有孩子节点

BiTreeNode *InsertRightChild(BiTreeNode *head,char a)
{
	if(head==NULL)  return NULL;
	BiTreeNode *p,*t;
	t=head->RightChild;
	p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
	assert(p!=NULL);
	p->data=a;
	p->LeftChild=NULL;
	p->RightChild=t;
	head->RightChild=p;
	return head->RightChild;
}

void Destroy(BiTreeNode *head)
{
	if(head!=NULL && head->LeftChild!=NULL)
		Destroy(head->LeftChild);//左子树非空,递归销毁左子树
	if(head!=NULL && head->RightChild!=NULL)
		Destroy(head->RightChild);//右子树非空,递归销毁右子树
	free(head);
}

//中序遍历
void LDR(BiTreeNode *head)
{
	if(head!=NULL)
	{
		LDR(head->LeftChild);
		cout<<head->data<<"  ";
		LDR(head->RightChild);
	}
}


//前序遍历
void DLR(BiTreeNode *head)
{
	if(head!=NULL)
	{
		cout<<head->data<<"  ";
		DLR(head->LeftChild);	
		DLR(head->RightChild);
	}
}

//后序遍历
void LRD(BiTreeNode *head)
{
	if(head!=NULL)
	{
		LRD(head->LeftChild);
		LRD(head->RightChild);
		cout<<head->data<<"  ";
	}
}

//在二叉树中查找某个值
BiTreeNode * Search(BiTreeNode *head, char x)
{
	BiTreeNode *p=NULL;//初始化为NULL
	if(head!=NULL)
	{
		if(head->data==x)  //如果查找到的节点是x,那么把该节点的值给p
			p=head;
		else
		{
			p=Search(head->LeftChild,x); //否则递归查找左孩子树
			if(p==NULL) //如果左孩子树没找到,递归查找右孩子树
				p=Search(head->RightChild,x);
		}
	}
	return p;
}


void main()
{
	BiTreeNode *head,*p,*q;
	//声明了一个指向BiTreeNode的指针head,由于指针head没有地址
	//所以作为实参要用&head,表示指向指针的地址
	//此时调用的函数的形参需要使用指针的指针
	Initiate(&head);
	head->data='g';
	p=InsertLeftChild(head,'a');
	p=InsertLeftChild(p,'b');
	p=InsertRightChild(head,'c');
	DLR(head);
	cout<<endl;
	LDR(head);
	cout<<endl;
	LRD(head);
	cout<<endl;
	q=Search(head,'c');//Find c
	cout<<"FIND :"<<q->data<<endl;
	Destroy(head);
	system("pause");
}
也可以不适用指针的指针初始化,那么可以这样写

第2种写法:

#include<iostream>
#include<assert.h>
using namespace std;
 

typedef struct Lnode
{
	char data;
	struct Lnode *LeftChild;
	struct Lnode *RightChild;
}BiTreeNode;

//主程序的实参是一个有内存地址的值,所以不用malloc申请动态空间
void Initiate(BiTreeNode *head)
{
	head->LeftChild=NULL;
	head->RightChild=NULL;
}

BiTreeNode * InsertLeftChild(BiTreeNode *head,char a )
{
	if(head==NULL) return NULL;
	BiTreeNode * p,*t;
	t=head->LeftChild;
	p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
	assert(p!=NULL);
	p->data=a;
	p->RightChild=NULL;
	p->LeftChild=t;
	head->LeftChild=p;
	return head->LeftChild;
}

BiTreeNode *InsertRightChild(BiTreeNode *head,char a)
{
	if(head==NULL)  return NULL;
	BiTreeNode *p,*t;
	t=head->RightChild;
	p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
	assert(p!=NULL);
	p->data=a;
	p->LeftChild=NULL;
	p->RightChild=t;
	head->RightChild=p;
	return head->RightChild;
}

void Destroy(BiTreeNode *head)
{
	if(head!=NULL && head->LeftChild!=NULL)
		Destroy(head->LeftChild);
	if(head!=NULL && head->RightChild!=NULL)
		Destroy(head->RightChild);
	free(head);
}

void LDR(BiTreeNode *head)
{
	if(head!=NULL)
	{
		LDR(head->LeftChild);
		cout<<head->data<<"  ";
		LDR(head->RightChild);
	}
}

void DLR(BiTreeNode *head)
{
	if(head!=NULL)
	{
		cout<<head->data<<"  ";
		DLR(head->LeftChild);	
		DLR(head->RightChild);
	}
}

void LRD(BiTreeNode *head)
{
	if(head!=NULL)
	{
		LRD(head->LeftChild);
		LRD(head->RightChild);
		cout<<head->data<<"  ";
	}
}

BiTreeNode * Search(BiTreeNode *head, char x)
{
	BiTreeNode *p=NULL;
	if(head!=NULL)
	{
		if(head->data==x)
			p=head;
		else
		{
			p=Search(head->LeftChild,x);
			if(p==NULL)
				p=Search(head->RightChild,x);
		}
	}
	return p;
}


void main()
{
	//定义BiTreeNode变量,head,此时已经有内存空间
	BiTreeNode head,*p,*q;  
	Initiate(&head);//实参传入head的内存地址
	head.data='g';
	p=InsertLeftChild(&head,'a');
	p=InsertLeftChild(p,'b');
	p=InsertRightChild(&head,'c');

	DLR(&head);
	cout<<endl;
	LDR(&head);
	cout<<endl;
	LRD(&head);
	cout<<endl;

	q=Search(&head,'c');//Find c
	cout<<"FIND :"<<q->data<<endl;

	//此时销毁二叉树不能使用destroy函数,因为程序直接声明一个变量head,
	//没有用malloc函数申请空间,而destroy函数调用了free函数。所以调用后会报错。
	system("pause");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值