建立一棵链表二叉树啊~输出前序,中序和后序遍历

一定要用c++来编写

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
typedef struct BiTNode {
char data;
struct BiTNode *Lchild, *Rchild; // 左、右孩子指针
} *BiTree;

void CreateBiTree(BiTree &T){
// 字符'#'表示空树,其它字母字符为结点的数据元素
char ch;
cin >> ch ;
if (ch=='#'){
T=NULL; // 建空树
}else {
T = new BiTNode ; // "访问"操作为生成根结点
T->data = ch;
CreateBiTree(T->Lchild); // 递归建(遍历)左子树
CreateBiTree(T->Rchild); // 递归建(遍历)右子树
}//else
}//CreateBiTree

//先序遍历以T为根指针的二叉树
void PreOrder(BiTree &T){
if(T){ // T=NULL时,二叉树为空树,不做任何操作
cout<< T->data << " "; // 通过函数指针 *visit 访问根结点
PreOrder(T->Lchild);
PreOrder(T->Rchild);
}// if
}
//中序遍历以T为根指针的二叉树
void InOrder(BiTree &T){
if(T){ //
InOrder(T->Lchild); //
cout<< T->data << " "; //
InOrder(T->Rchild); //
}// if
}
//后序遍历以T为根指针的二叉树
void PostOrder(BiTree &T){
if(T){ // T=NULL时
PostOrder(T->Lchild); //
PostOrder(T->Rchild); //
cout<< T->data << " "; //

}//
}
// 先序遍历二叉树,以 count 返回二叉树中叶子结点的数目
void CountLeaf(BiTree &T, int &count){
if (T) {
if ((!T->Lchild)&& (!T->Rchild))
count++; // 对叶子结点计数
CountLeaf( T->Lchild, count);
CountLeaf( T->Rchild, count);
} // if
} // CountLeaf

// T指向二叉树的根,level 为 T 所指结点所在层次,
// 其初值为1,depth 为当前求得的最大层次,其初值为0
void BiTreeDepth(BiTree T, int level, int &depth){
if (T){
if (level>depth) depth=level;
BiTreeDepth(T->Lchild, level+1, depth);
BiTreeDepth(T->Rchild, level+1, depth);
}// if
}// BiTreeDepth
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-26
楼上的挺辛苦的,我也提供一个吧。供楼主参考:

#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void CreateBiTree(BiTree &T){
char ch;
ch=getchar();
if(ch==' '){
T=NULL;
}
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(0);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}

void PreOrder(BiTree root){
if(root==NULL) return;
printf("%c",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
void InOrder(BiTree root){
if(root==NULL) return;
InOrder(root->lchild);
printf("%c",root->data);
InOrder(root->rchild);
}
void PostOrder(BiTree root){
if(root==NULL) return;
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c",root->data);
}
void main(){
BiTree T;
cout<<"请输入一个二叉树:"<<endl;
CreateBiTree(T);
cout<<endl;
cout<<"前序输出这个二叉树为:"<<endl;
PreOrder(T);
cout<<endl;
cout<<"中序输出这个二叉树为:"<<endl;
InOrder(T);
cout<<endl;
cout<<"后序输出这个二叉树为:"<<endl;
PostOrder(T);
cout<<endl;

}本回答被网友采纳
第2个回答  2013-09-26
等我写完之后你补充说要C++版本的,我无语了...还是贴上来吧!
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int status;

typedef struct BiNode
{
char Data;
struct BiNode* lChild;
struct BiNode* rChild;
}BiNode,*pBiNode;

status CreateTree(BiNode** pTree);
status InOrderTraval(BiNode* pTree);
status PreOrderTraval(BiNode* pTree);
status PostOrderTraval(BiNode* pTree);
status ShowLeaves(BiNode* pTree);
status DelTree(BiNode* pTree);
status Visit(char Data);
status Display(BiNode* pTree,int Level);
status Clear(BiNode* pTree);
BiNode *pRoot=NULL;

int main()
{
CreateTree(&pRoot);
printf("\nPreOrder:");//先序遍历
PreOrderTraval(pRoot);
printf("\n");

printf("\nInOrder:");
InOrderTraval(pRoot);//中序遍历
printf("\n");

printf("\nPostOrder:");
PostOrderTraval(pRoot);//后续遍历
printf("\n");
Display(pRoot,0);
printf("\n");
printf("\nDeleting Tree:\n");
DelTree(pRoot);
printf("BiTree Deleted.");
getch();
return 0;
}
相似回答