经过几个小时的打拼,江山夺了下来,,哈哈,二叉树已被我拿下~~~~ ^_^
Description
按先序遍历序列建立二叉树的二叉链表,已知先序序列为:
A B C 0 0 D E 0 G 0 0 F 0 0 0 // 0表示空的
用文件操作,输入;
用concle操作,输出;
Sample Input
A B C 0 0 D E 0 G 0 0 F 0 0 0 // 0表示空的
Sample Output
depth of tree is 5
count of leaf node is 3
前序遍历:ABCDEGF
中序遍历:CBEGDFA
后续遍历:CGEFDBA
Finished!!
Code
//
author:essence_of_ACMER
// time:2011.8.8
// theme:Traversal_algorithm
#include < iostream >
#define M 500
#include < malloc.h >
#include < fstream >
using namespace std;
ifstream f( " a.txt " );
ofstream f1( " a1.txt " );
typedef struct node
{
char date;
struct node * lchild, * rchild;
}JD ;
JD * crt_bt_pre(JD * bt) // 先序遍历建立二叉树
{ char ch;
// printf("ch="); // 这个非文件输入的情况
f >> ch; // scanf("%c",&ch); // 文件输入(需要自己建立文件a.txt(其中是mian函数的下面的注释))
// getchar(); // 这个非文件输入的情况
if (ch == ' 0 ' ) bt = NULL;
else
{ bt = (JD * )malloc( sizeof (JD));
bt -> date = ch;
bt -> lchild = crt_bt_pre(bt -> lchild);
bt -> rchild = crt_bt_pre(bt -> rchild);
}
return (bt);
}
void inorder_fd(JD * bt) // fd表示非(f)递(d)归
{
JD * p, * s[M];
int i = 0 ;
p = bt;
do {
while (p != NULL)
{
s[i ++ ] = p;
p = p -> lchild;
}
if (i > 0 )
{
p = s[ -- i];
cout << p -> date;
f1 << p -> date;
p = p -> rchild;
}
} while (i > 0 || p != NULL);
}
void preorder_d(JD * bt) // 前序递归遍历
{
if (bt != NULL)
{cout << bt -> date;
f1 << bt -> date;
preorder_d(bt -> lchild);
preorder_d(bt -> rchild);}
}
void inorder_d(JD * bt) // 中序递归遍历
{
if (bt != NULL)
{inorder_d(bt -> lchild);
cout << bt -> date;
f1 << bt -> date;
inorder_d(bt -> rchild);}
}
void postorder_d(JD * bt) // 后序递归遍历
{
if (bt != NULL)
{
postorder_d(bt -> lchild);
postorder_d(bt -> rchild);
cout << bt -> date;
f1 << bt -> date;
}
}
void countleaf(JD * bt, int * count) // 求叶子节点的数目
{ if (bt != NULL)
{ if ((bt -> lchild == NULL) && (bt -> rchild == NULL))
{ ( * count) ++ ;
return ;
}
countleaf(bt -> lchild,count);
countleaf(bt -> rchild,count);
}
}
void treedepth(JD * bt, int * l, int * h) // 求树的深度
{
int l1 = 0 ,l2 = 0 ,h1 = 0 ,h2 = 0 ;
if (bt != NULL)
{ ( * l) ++ ;
if ( * l >* h) * h =* l;
treedepth(bt -> lchild, & l1, & h1);
treedepth(bt -> rchild, & l2, & h2);
if (h1 > h2) * h =* h + h1;
else * h =* h + h2;
}
}
int main()
{ /* ABC00DE0G00F000 */
JD * head = NULL;
int level = 0 ,high = 0 ,count = 0 ;
head = crt_bt_pre(head); // 创建二叉树(前序)
treedepth(head, & level, & high); // 求树的深度
printf( " depth of tree is %d\n\n " ,high);
f1 << " depth of tree is " << high << endl;
countleaf(head, & count); // 求树的叶子节点数
printf( " count of leaf node is %d " ,count);
f1 << " count of leaf node is " << count;
cout << endl << endl << " 前序遍历: " ;
f1 << endl << endl << " 前序遍历: " ;
preorder_d(head); // 前序遍历
cout << endl << endl << " 中序遍历: " ;
f1 << endl << endl << " 中序遍历: " ;
inorder_d(head); // 中序遍历
cout << endl << endl << " 后续遍历: " ;
f1 << endl << endl << " 后续遍历: " ;
postorder_d(head); // 后续遍历
cout << endl << endl << " Finished!! " << endl;
f1 << endl << endl << " Finished!! " << endl;
return ( 0 );
}
// time:2011.8.8
// theme:Traversal_algorithm
#include < iostream >
#define M 500
#include < malloc.h >
#include < fstream >
using namespace std;
ifstream f( " a.txt " );
ofstream f1( " a1.txt " );
typedef struct node
{
char date;
struct node * lchild, * rchild;
}JD ;
JD * crt_bt_pre(JD * bt) // 先序遍历建立二叉树
{ char ch;
// printf("ch="); // 这个非文件输入的情况
f >> ch; // scanf("%c",&ch); // 文件输入(需要自己建立文件a.txt(其中是mian函数的下面的注释))
// getchar(); // 这个非文件输入的情况
if (ch == ' 0 ' ) bt = NULL;
else
{ bt = (JD * )malloc( sizeof (JD));
bt -> date = ch;
bt -> lchild = crt_bt_pre(bt -> lchild);
bt -> rchild = crt_bt_pre(bt -> rchild);
}
return (bt);
}
void inorder_fd(JD * bt) // fd表示非(f)递(d)归
{
JD * p, * s[M];
int i = 0 ;
p = bt;
do {
while (p != NULL)
{
s[i ++ ] = p;
p = p -> lchild;
}
if (i > 0 )
{
p = s[ -- i];
cout << p -> date;
f1 << p -> date;
p = p -> rchild;
}
} while (i > 0 || p != NULL);
}
void preorder_d(JD * bt) // 前序递归遍历
{
if (bt != NULL)
{cout << bt -> date;
f1 << bt -> date;
preorder_d(bt -> lchild);
preorder_d(bt -> rchild);}
}
void inorder_d(JD * bt) // 中序递归遍历
{
if (bt != NULL)
{inorder_d(bt -> lchild);
cout << bt -> date;
f1 << bt -> date;
inorder_d(bt -> rchild);}
}
void postorder_d(JD * bt) // 后序递归遍历
{
if (bt != NULL)
{
postorder_d(bt -> lchild);
postorder_d(bt -> rchild);
cout << bt -> date;
f1 << bt -> date;
}
}
void countleaf(JD * bt, int * count) // 求叶子节点的数目
{ if (bt != NULL)
{ if ((bt -> lchild == NULL) && (bt -> rchild == NULL))
{ ( * count) ++ ;
return ;
}
countleaf(bt -> lchild,count);
countleaf(bt -> rchild,count);
}
}
void treedepth(JD * bt, int * l, int * h) // 求树的深度
{
int l1 = 0 ,l2 = 0 ,h1 = 0 ,h2 = 0 ;
if (bt != NULL)
{ ( * l) ++ ;
if ( * l >* h) * h =* l;
treedepth(bt -> lchild, & l1, & h1);
treedepth(bt -> rchild, & l2, & h2);
if (h1 > h2) * h =* h + h1;
else * h =* h + h2;
}
}
int main()
{ /* ABC00DE0G00F000 */
JD * head = NULL;
int level = 0 ,high = 0 ,count = 0 ;
head = crt_bt_pre(head); // 创建二叉树(前序)
treedepth(head, & level, & high); // 求树的深度
printf( " depth of tree is %d\n\n " ,high);
f1 << " depth of tree is " << high << endl;
countleaf(head, & count); // 求树的叶子节点数
printf( " count of leaf node is %d " ,count);
f1 << " count of leaf node is " << count;
cout << endl << endl << " 前序遍历: " ;
f1 << endl << endl << " 前序遍历: " ;
preorder_d(head); // 前序遍历
cout << endl << endl << " 中序遍历: " ;
f1 << endl << endl << " 中序遍历: " ;
inorder_d(head); // 中序遍历
cout << endl << endl << " 后续遍历: " ;
f1 << endl << endl << " 后续遍历: " ;
postorder_d(head); // 后续遍历
cout << endl << endl << " Finished!! " << endl;
f1 << endl << endl << " Finished!! " << endl;
return ( 0 );
}