1. 程式人生 > >資料結構--二叉樹的建立和相關操作

資料結構--二叉樹的建立和相關操作

下面給出兩個關於二叉樹的題目:

1.編寫程式任意輸入二叉樹的結點個數和結點值,構造一棵二叉樹,採用三種遞迴遍歷演算法(前序、中序、後序)對這棵二叉樹進行遍歷並計算出二叉樹的高度

2 .編寫程式生成下面所示的二叉樹,並採用先序遍歷的非遞迴演算法對此二叉樹進行遍歷。

 

下面實現這兩個題目:

1.

#include "stdafx.h"
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#include<stack>
using namespace std;
#define NULL 0
#define OK 1
#define OVERFLOW -1
typedef int Status;
typedef struct node 
{
	char data;
	struct node *lchild;
	struct node *rchild;
}*bitree;
int k=0;
int depth(bitree T)//樹的高度 
{ 
	if(!T)return 0; 
	else 
	{ 
		int m=depth(T->lchild); 
		int n=depth(T->rchild); 
		return (m>n?m:n)+1; 
	} 
} 
//先序,中序 建樹
struct node *create(char *pre,char *ord,int n)
{
	struct node * T;
	int m;
	T=NULL;
	if(n<=0)
	{
		return NULL;
	}
	else 
	{
		m=0;
		T=new(struct node);
		T->data=*pre;
		T->lchild=T->rchild=NULL;
		while(ord[m]!=*pre)
			m++;
		T->lchild=create(pre+1,ord,m);
		T->rchild=create (pre+m+1,ord+m+1,n-m-1);
		return T;
	}
}
//中序遞迴遍歷
void inorder(struct node *T)
{
	if(!T)
		return;
	else
	{
		inorder(T->lchild );
		cout<<T->data;
		inorder(T->rchild );
	}
}
void inpre(struct node *T)
{
	if(!T)
		return;
	else
	{
		cout<<T->data;
		inpre(T->lchild );
		inpre(T->rchild );
		
	}
}
void postorder(struct node *T)
{
	if(!T)
		return;
	else
	{
		
		postorder (T->lchild );
		postorder (T->rchild );
		cout<<T->data;
		
	}
}
//先序非遞迴遍歷
void inpre1(struct node *T)
{
	struct node *p;
	struct node *stack[20];
	int top=0;
	p=T;
	cout<<"非遞迴先序 ";
	while(p||top!=0)
	{
		while (p)
		{
			stack[top++]=p;
			cout<<p->data;
			p=p->lchild;
		}
		p=stack[--top];
		
		p=p->rchild ;
	}
}
//中序非遞迴遍歷
void inorder1(struct node *T)
{
	struct node *p;
	struct node *stack[20];
	int top=0;
	p=T;
	cout<<"非遞迴中序 ";
	while(p||top!=0)
	{
		while (p)
		{
			stack[top++]=p;
			p=p->lchild ;
		}
		p=stack[--top];
		cout<<p->data;
		p=p->rchild ;
	}
}
//主函式
int main()
{
	bitree T;
	char pre[30],ord[30];
	int n,m;
	gets(pre);
	gets(ord);
    n=strlen(pre);
	T=create(pre,ord,n);
	inpre(T);
	cout<<endl;
	postorder (T);
    cout<<endl;
	inorder(T);
    cout<<endl;
	inpre1(T);
	cout<<endl;
	inorder1(T);
    cout<<endl;
	m=depth(T);
	cout<<"二叉樹高度為:"<<m<<endl;
	return 0;
}

2.

#include "stdafx.h"
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#include<stack>
using namespace std;

#define NULL 0
#define OK 1
#define OVERFLOW -1
typedef int Status;
typedef struct node 
{
	char data;
	struct node *lchild;
	struct node *rchild;
}*bitree;

Status Create(bitree &T)  //按先序次序輸入二叉樹中結點的值,!表示空樹
{
	char e;
	cout<<"輸入樹的元素:"<<endl;
    cin>>e;
    if(e=='!') T=NULL;
		else
	{
		if(!(T=(node *)malloc(sizeof(node))))
			exit (OVERFLOW);
		T->data=e;
		Create(T->lchild);
		Create(T->rchild);

	}
	return OK;

}


//先序非遞迴遍歷
void inpre(struct node *T)
{
	struct node *p;
	struct node *stack[20];
	int top=0;
	p=T;
	cout<<"非遞迴先序 ";
	while(p||top!=0)
	{
		while (p)
		{
			stack[top++]=p;
			cout<<p->data;
			p=p->lchild;
		}
		p=stack[--top];
		
		p=p->rchild ;
	}
}
//主函式
int main()
{
	bitree T;
    Create(T);
	cout<<"輸出的元素為:"<<endl;
	inpre(T);
	return 0;
}