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

#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;
}

  

转载于:https://www.cnblogs.com/yanliang12138/p/4353792.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值