码迷,mamicode.com
首页 > 其他好文 > 详细

大话数据结构——二叉树的建立于遍历

时间:2015-03-20 17:46:40      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

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

#define OK 1
#define TURE 1
#define ERROR 0
#define FALSE 0
typedef char elemtype;

elemtype ch;
//二叉树的二叉链表结点结构
typedef struct T_tree_node
{
	T_tree_node *L_child;//指向左孩子
	T_tree_node *R_child;//指向右孩子
	elemtype data;//结点数据
}T_tree_node ,*T_tree_pr;

//二叉树的前序遍历算法
void first_search_tree(T_tree_pr T)
{
	if(T==NULL)
	{cout<<‘*‘<<endl;return;}
	cout<<T->data<<endl;//最先显示(操作)双亲结点
	first_search_tree(T->L_child);//先序遍历左子树
	first_search_tree(T->R_child);//先序遍历右子树
}

//二叉树的中序遍历
void mid_search_tree(T_tree_pr T)
{
	if(T==NULL)
		return;
	mid_search_tree(T->L_child);//中序遍历左子树
	cout<<T->data<<endl;//在中间将双亲结点显示
	mid_search_tree(T->R_child);//中序遍历右子树
}

//二叉树的后序遍历
void last_search_tree(T_tree_pr T)
{
	if(T==NULL)
		return;
	last_search_tree(T->L_child);//后序遍历左子树
	last_search_tree(T->R_child);//后序遍历右子树
	cout<<T->data<<endl;//最后显示双亲结点
}

//按前序遍历的顺序输入值
void Great_first_tree(T_tree_pr *T)
{
	
	//cin>>ch;
	scanf("%c",&ch);
	fflush(stdin);
	if(ch==‘#‘)
		*T=NULL;
	else
	{
		*T=(T_tree_pr)malloc(sizeof(T_tree_node));
		assert((*T)!=NULL);
		(*T)->data=ch;//先输入双亲结点的值
		Great_first_tree(&(*T)->L_child);//左孩子的值
		Great_first_tree(&(*T)->R_child);//右孩子的值
	}
}
int main()
{ 
	
	T_tree_pr T;
	Great_first_tree(&T);

	first_search_tree(T);
	cout<<"what‘s the func?"<<endl;
	system("pause");
	return 1;
}

  

大话数据结构——二叉树的建立于遍历

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4353792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!