// Author: sky
// Date: 2011.11.8
// Content: 树的基本程序
///
#include <iostream>
using namespace std;
#define treeNodeMaxNum 20;
typedef char ElementType;
typedef struct Node //树中的节点
{
ElementType element;
Node *lChild;
Node *rChild;
}*TreeNode;
struct BitTree //树
{
BitTree();
bool Initialize(TreeNode &node,ElementType data);//创建树
//bool Initialize(string preOrderStr);//输入一个先序遍历的序列来创建树 原理跟上面那个方法一样
void PreOrder(TreeNode &node);//采用的是递归的方法,因为比较简单所以 这里只写一个
/*void InOrder();
void PostOrder();*/
void LevelOrder(); //层次遍历 同时输出该节点所在的层次
void PreOrderByNorecursive();//采用非递归方法 前序遍历树
void InOrderByNorecursive();//采用非递归方法 中序遍历
void PostOrderByNorecursive();// 采用非递归方法 后序遍历
int GetTreeDepth(TreeNode &node);//树的深度
int GetLeafNum(TreeNode &node);//叶子节点数目
TreeNode root;//执行根节点
int nodeNum;//可有可无
};
BitTree::BitTree()
{
nodeNum=0;
root=NULL;
}
bool BitTree::Initialize(TreeNode &node,ElementType data)
{
if (data=='#')//使用 # 代替空
{
node=NULL;
}
else
{
node=new Node();
if(!node) return false;
node->element=data;
cout<<"Input "<<node->element<<"'s LeftChild:";
cin>>data;
Initialize(node->lChild,data);
cout<<"Input "<<node->element<<"'s RightChild:";
cin>>data;
Initialize(node->rChild,data);
}
return true;
}
void BitTree::PreOrder(TreeNode &node)
{
if (node!=NULL)
{
cout<<" "<<node->element;
PreOrder(node->lChild);
PreOrder(node->rChild);
}
}
void BitTree::LevelOrder()
{
//说明 level count 变量是用来判断层次的
// 层次遍历 需要队列的辅助,可以自己写一个队列结构 然后出队入队操作
//此处是使用结构体指针数组 模拟队列的操作
TreeNode treeQueue[20];
TreeNode p=NULL;
int front=-1,last=-1,level=1,count=1;
last++;
treeQueue[last]=root;// 这两句是入队操作
cout<<"\n Level "<<level<<"'s Element : \t\t";
do
{
if (count==0)//此处是判断level的 不需要的话 可以删除
{
level++;
cout<<"\n Level "<<level<<"'s Element : \t\t";
count=last-front;
}
front++;
p=treeQueue[front];// 出队操作
count--;
cout<<'\t'<<p->element;
if (p->lChild!=NULL)
{
last++;
treeQueue[last]=p->lChild;
}
if (p->rChild!=NULL)
{
last++;
treeQueue[last]=p->rChild;
}
}while(front!=last);
}
void BitTree::PreOrderByNorecursive()
{
//同理 使用TreeNode类型数组 模拟栈的出栈入栈操作
if (root==NULL)
{
cout<<"Tree is Null";
return;
}
TreeNode treeStack[20];
TreeNode p=root;
int top=-1;
do
{
while(p!=NULL)
{
cout<<" "<<p->element;
top++;
treeStack[top]=p;
p=p->lChild;
}
p=treeStack[top];
top--;
p=p->rChild;
}while(top!=-1||p!=NULL);
}
void BitTree::InOrderByNorecursive()
{
if (root==NULL)
{
cout<<"Tree is Null";
return;
}
TreeNode treeStack[20];
TreeNode p=root;
int top=-1;
do
{
while(p!=NULL)
{
top++;
treeStack[top]=p;
p=p->lChild;
}
p=treeStack[top];
top--;
cout<<p->element<<" ";
p=p->rChild;
}while(top!=-1||p!=NULL);
}
void BitTree::PostOrderByNorecursive()
{
//非递归后序遍历 此处采用了 双栈操作的原理,只不过为了简化操作,依然是一个栈
// 对outPutchar的使用其实就相当于一个栈
if (root==NULL)
{
cout<<"Tree is NULL";
return;
}
TreeNode treeStack[20];
ElementType outPutChar[20];//存放的是后续遍历 的结果
TreeNode p;
int top=-1,count=-1;
top++;
treeStack[top]=root;
while(top!=-1)
{
p=treeStack[top];
top--;
count++;
outPutChar[count]=p->element;
if (p->lChild!=NULL)
{
top++;
treeStack[top]=p->lChild;
}
if (p->rChild!=NULL)
{
top++;
treeStack[top]=p->rChild;
}
}
for (int i=count;i>=0;i--)
{
cout<<outPutChar[i]<<" ";
}
}
int BitTree::GetTreeDepth(TreeNode &node)
{
int dl=0,dr=0,max=0;
if (node==NULL)
{
return 0;
}
dl=GetTreeDepth(node->lChild);
dr=GetTreeDepth(node->rChild);
max=dl>dr?dl:dr;
return ++max;
}
int BitTree::GetLeafNum(TreeNode &node)
{
if(node==NULL)
{
return 0;
}
if (node->lChild==NULL&&node->rChild==NULL)
{
return 1;
}
else
{
return (GetLeafNum(node->lChild)+GetLeafNum(node->rChild));
}
}
int main(int argc,char* argv[])
{
BitTree bt;
char rot;
cout<<"CARE: If the node of Tree is NULL ,please, Input # instead of it\n";
cout<<"\n\n\t\tPlease Input root Element: "; cin>>rot;
if (bt.Initialize(bt.root,rot)==false)
{
cout<<"Initialize Error!";return 0;
}
cout<<"PreOrder :";
bt.PreOrder(bt.root);
cout<<"\n\n";
cout<<"LevelOrder :";
bt.LevelOrder();
cout<<"\n\n";
cout<<"PreOrderByNorecursive :";
bt.PreOrderByNorecursive();
cout<<"\n\n";
cout<<"InOrderByNorecursive :";
bt.InOrderByNorecursive();
cout<<"\n\n";
cout<<"InOrderByNorecursive :";
bt.PostOrderByNorecursive();
cout<<"\n\n";
cout<<"The leaf number of Tree is "<<bt.GetLeafNum(bt.root)<<endl;
cout<<"The Depth of Tree is "<<bt.GetTreeDepth(bt.root)<<endl;
return 0;
}