# 二叉树的一些操作

854人阅读 评论(0)

/*-------------------------------------------------------------------------------
*
*Author: Tao Yunxin
*History: 2007/04/02
-------------------------------------------------------------------------------*/

#include "stdafx.h"
#include <iostream>
using namespace std;

#include <fstream>
#include <iomanip>
#include <math.h>
#include <malloc.h>
#include <time.h>
#include <assert.h>

const int MAXSIZE = 10;

typedef struct BTNode
{
char data;
struct BTNode *lchild;
struct BTNode *rchild;
}*pBTree;

ifstream in("data.txt");

/*-------------------------------------------------------------------------------
*Function definition of BTree.cpp
*
*Author: Tao Yunxin
*History: 2007/04/02
-------------------------------------------------------------------------------*/

//Pre order create the bi-tree
bool CreateBiTree(pBTree *T) //or BTNode **T ---is also right!
{
pBTree p;
char ch;

if (ch == '#')
{
p = NULL;
}
else
{
p = new BTNode;
if (p == NULL)
{
cout<<"memory exhausted!"<<endl;
exit(1);
}
p->data = ch;
if ( !CreateBiTree( &(p->lchild) ) )
{
return false;
}
if ( !CreateBiTree( &(p->rchild) ) )
{
return false;
}
}

*T = p; //lay the result in T rather than p
return true;
}

//Any binary tree store in data[] according orderly, creating the binary tree from data[]
//comparing with pBTree *T, pBTree &T is simple and more understandable
void CreateFrArr(/*pBTree *T*/pBTree &T, char data[], int n, int i)
{
pBTree p;

if (i > n-1)
{
p = NULL;
}
else
{
if (data[i] == '#')
{
p = NULL;
}
else
{
p = new BTNode;
if (p == NULL)
{
cout<<"memory exhausted!"<<endl;
exit(1);
}
p->data = data[i];
CreateFrArr( /*&*/p->lchild, data, n, 2*i+1 );
CreateFrArr( /*&*/p->rchild, data, n, 2*i+2 );
}
}

T = p; //*T = p;
}

//return the height of bi-tree
int Height(const pBTree p)
{
if (p == NULL)
{
return 0;
}
else
{
int h;
h = Height(p->lchild) > Height(p->rchild) ? Height(p->lchild) : Height(p->rchild);
return h+1;
}
}

//Calculating total number of node in binary tree
int NodeNum(const pBTree t)
{
if (t == NULL)
{
return 0;
}
else
{
return NodeNum(t->lchild) + NodeNum(t->rchild) + 1;
}
}

//Calculating number of node who has only a child
int OneChildNum(const pBTree t)
{
int n = 0;

if (t == NULL)
{
return 0;
}
//operator priority: first ==,then &&, last ||
else if ( (t->lchild == NULL && t->rchild != NULL)
|| (t->lchild != NULL && t->rchild == NULL) )
{
n = 1;
}

return OneChildNum(t->lchild) + OneChildNum(t->rchild) + n;
}

//Calculating the leaf number of the binary tree
int LeafNum(const pBTree t)
{
if (t == NULL)
{
return 0;
}
else if (t->lchild==NULL && t->rchild==NULL)
{
return 1;
}
else
{
return LeafNum(t->lchild) + LeafNum(t->rchild);
}
}

//Pre order traverse the tree recursively
void PreOrder(const BTNode *p)
{
if (p != NULL)
{
cout<<setw(2)<<p->data; //visiting the root node
PreOrder(p->lchild); //visiting the left child tree
PreOrder(p->rchild); //visiting the right child tree
}
}

//In order traverse the tree recursively
void InOrder(const pBTree p)
{
if (p != NULL)
{
InOrder(p->lchild);
cout<<setw(2)<<p->data;
InOrder(p->rchild);
}
}

//Post order traverse the tree recursively
void PostOrder(const pBTree p)
{
if (p != NULL)
{
PostOrder(p->lchild);
PostOrder(p->rchild);
cout<<setw(2)<<p->data;
}
}

//Pre order traverse the tree un-recursively
void PreOrderN(const pBTree t)
{
pBTree stack[MAXSIZE], p;
int top = -1;

if (t != NULL)
{
top++;
stack[top] = t;
while (top > -1)
{
p = stack[top];
top--;
cout<<setw(2)<<p->data;

if (p->rchild != NULL)
{
top++;
stack[top] = p->rchild;
}
if (p->lchild != NULL)
{
top++;
stack[top] = p->lchild;
}
}//end while
}//end if
}

//In order traverse the tree un-recursively
void InOrderN(const pBTree t, int depth)
{
pBTree *stack ; //depth = height+1
stack = new pBTree[depth];
pBTree p;
int top = -1;

if (t != NULL)
{
top++;
stack[top] = t;

while (top > -1)
{
while (stack[top] != NULL)
{
p = stack[top];
top++;
stack[top] = p->lchild;
}
top--; //NULL pointer pop stack
if (top > -1)
{
p = stack[top];
cout<<setw(2)<<p->data;
stack[top] = p->rchild;
}
}//end while
}//end if

delete []stack;
}

//Post order traverse the tree un-recursively
void PostOrderN(const pBTree t, int depth)
{
pBTree *stack; //depth = height
stack = new pBTree[depth];
pBTree p;
int top = -1;
p = t;

do
{
while (p != NULL)
{
top++;
stack[top] = p;
p = p->lchild;
}

while ((top >- 1) && (stack[top]->rchild == p))
{
p = stack[top];
top--;
cout<<setw(2)<<p->data;
}

if (top > -1)
{
p = stack[top]->rchild;
}
}while (top > -1);

delete []stack;
}

//Level order traverse the tree
void LevelTrav(const pBTree t, int length)
{
pBTree *queue;
queue = new pBTree[length]; //length is the number of node in binary tree
pBTree p;
int front = 0,rear = 0;

if (t != NULL)
{
queue[rear++] = t;

while (front < rear)
{
p = queue[front];
front++;
cout<<setw(2)<<p->data;

if (p->lchild != NULL)
{
queue[rear] = p->lchild;
rear++;
}
if (p->rchild != NULL)
{
queue[rear] = p->rchild;
rear++;
}
}//end while
}//end if

delete []queue;
}

//Pre-order display binary tree using recursively
void PreDisp(const pBTree t)
{
if(t != NULL)
{
cout<<t->data;

if (t->lchild != NULL || t->rchild != NULL)
{
cout<<"(";
PreDisp(t->lchild);

if (t->rchild != NULL)
{
cout<<",";
}

PreDisp(t->rchild);
cout<<")";
}
}
}

//Releasing the memory space of the binary tree
void Release(pBTree t)
{
if (t != NULL)
{
Release(t->lchild);
Release(t->rchild);
delete t;
}

t = NULL;
}

//Judging whether t1 and t2 is alike
bool Like(const pBTree t1, const pBTree t2)
{
//assert(t1 != NULL && t2 != NULL);

if (t1==NULL && t2==NULL)
{
return true;
}
else if (t1==NULL || t2==NULL)
{
return false;
}
else
{
return Like(t1->lchild, t2->lchild) && Like(t1->rchild, t2->rchild);
}
}

/*-------------------------------------------------------------------------------
*some operation of binary tree
*
*Author: Tao Yunxin
*History: 2007/04/02
-------------------------------------------------------------------------------*/

#include "stdafx.h"

#ifndef BTREE_H
#define BTREE_H
#include "BTree.h"
#endif

int _tmain(int argc, _TCHAR* argv[])
{
pBTree biTree = NULL;
pBTree biTree2 = NULL;

//It is used to test "assert"
//Like(biTree, biTree2);

bool flag;
int heigh, num;
char *data;

clock_t start, end;
time_t start2, end2;
start2 = time(0); //#define NULL 0

cout<<"size of BTNode: "<<sizeof(BTNode)<<endl;
cout<<"size of pBTree: "<<sizeof(pBTree)<<endl;
cout<<"---------------------------------------------"<<endl;
cout<<"Creating the binary tree from the array!"<<endl;
cout<<"please input the height of the binary tree:"<<endl;

cin>>heigh;
num = (int)pow((double)2, heigh) - 1;
//data = new char[num];
data = (char *)malloc(num); //we can allocate space using new or malloc
//Input: abc#de#
for(int i=0; i<num; i++)
{
cin>>data[i];
}

start = clock();

CreateFrArr( /*&*/biTree2, data, num, 0 );

cout<<"The height of bi-tree: "<<Height(biTree2)<<endl;
cout<<"Total number of node: "<<NodeNum(biTree2)<<endl;
cout<<"One child number: "<<OneChildNum(biTree2)<<endl;
cout<<"Leaf Number: "<<LeafNum(biTree2)<<endl;
cout<<"Pre-order display binary tree:"<<endl;
PreDisp(biTree2);

cout<<"/n---------------------------------------------/n/n"<<endl;
cout<<"Creating the binary tree from the file!"<<endl;
if (!in)
{
cerr<<"cannot open input file!"<<endl;
return -1;
}
flag = CreateBiTree(&biTree);
if (flag)
{
cout<<"Creating the binary tree succ!/n"<<endl;
}
else
{
cerr<<"Creating the binary tree failure!/n"<<endl;
}

cout<<"The height of bi-tree: "<<Height(biTree)<<endl;
cout<<"Number of node: "<<NodeNum(biTree)<<endl;
cout<<"One child number: "<<OneChildNum(biTree)<<endl;
cout<<"Leaf Number: "<<LeafNum(biTree)<<endl;
cout<<"Pre-order display binary tree:"<<endl;
PreDisp(biTree);
cout<<"/n---------------------------------------------"<<endl;

cout<<"Pre order traverse the bi-tree recursively:"<<endl;
PreOrder(biTree);
cout<<"/nPre order traverse the bi-tree recursively:"<<endl;
PreOrderN(biTree);
cout<<"/n---------------------------------------------"<<endl;

cout<<"/nIn order traverse the bi-tree recursively:"<<endl;
InOrder(biTree);
cout<<"/nIn order traverse the bi-tree un-recursively:"<<endl;
InOrderN(biTree, Height( biTree ) + 1);
cout<<"/n---------------------------------------------"<<endl;

cout<<"/nPost order traverse the bi-tree recursively:"<<endl;
PostOrder(biTree);
cout<<"/nPost order traverse the bi-tree un-recursively:"<<endl;
PostOrderN(biTree, Height(biTree));
cout<<"/n---------------------------------------------"<<endl;

cout<<"/nLevel traverse the bi-tree:"<<endl;
LevelTrav(biTree, NodeNum(biTree));
cout<<"/n---------------------------------------------"<<endl;

cout<<"/nbiTree and biTree2 similar ? "<<endl;
if (Like(biTree, biTree2))
{
cout<<"yes";
}
else
{
cout<<"no";
}
cout<<"/n---------------------------------------------"<<endl;

//delete []data;
free(data);
data = NULL;
Release(biTree);
Release(biTree2);
biTree = NULL;
biTree2 = NULL;
cout<<"Releasing space succ!/n"<<endl;

end = clock();

end2 = time(0);
printf("/1: The different is %6.3f/n", (double)(end-start));
printf( "The creating operation used %f seconds/n",
(double)(end-start) / CLOCKS_PER_SEC );
printf( "The total operation used %f seconds/n", difftime(end2, start2) );

char buf[32];
time_t tNow = time(NULL);
//We can use the function strftime( ) to covert the time to the format we want

strftime( buf, 32, "%Y/%m/%d   %H:%M:%S", localtime(&tNow) );
printf("Current time: ");
cout<<buf<<endl;
system("PAUSE");
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1340827次
• 积分：20820
• 等级：
• 排名：第365名
• 原创：810篇
• 转载：12篇
• 译文：1篇
• 评论：227条
评论排行
最新评论
ATL/WTL
Big Deal
Book
English
social