#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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐