您的当前位置:首页二叉树的基本操作(大学生学习心得体会)

二叉树的基本操作(大学生学习心得体会)

来源:小侦探旅游网

最近学习二叉树,代码如下:

#include<iostream>
using namespace std;
#include<stdlib.h>
typedef struct Node{
	char data;
	struct Node *LChild;
	struct Node *RChild;
}Tree,*Tee;
void create(Tee &root){
	char ch;
	cin>>ch;    //换成ch=getchar()的话,不能断断续续,需要一次性输入完,但cin都可以
	if(ch=='#')
	root=NULL;
	else{
		root=(Tree *)malloc(sizeof(Tree));  //一定要申请空间 
		root->data=ch;
		create(root->LChild);
		create(root->RChild);
	}
}
void show(Tree *root){
	if(root!=NULL){
	    cout<<root->data;
	    show(root->LChild);
	    show(root->RChild);
	}	
}
//计算树高 
int GetHigh(Tree *root){
	int h1,h2,max;
	if(root!=NULL){
		h1=GetHigh(root->LChild);
		h2=GetHigh(root->RChild);
		max=h1>h2?h1:h2;
		return max+1;
	}
	else return 0;
}	
int main(){
	Tree *root;
	create(root);
	show(root);
	cout<<endl;
	cout<<GetHigh(root)<<endl;
	return 0;
}

 

因篇幅问题不能全部显示,请点此查看更多更全内容