二叉树操作--包括树的创建、树的高度、树的宽度、层序遍历、非递归遍历等
#include<iostream>
#include<algorithm>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
using namespace std;
typedef struct BiTNode{
struct BiTNode *lchild;
struct BiTNode *rchild;
int data;
}BiTNode,*BiTree;
typedef struct{
BiTree *top;
BiTree *base;
int stack_size;
}SqStack;
bool InitStack(SqStack &s)
{
s.base = (BiTree *)malloc(STACK_INIT_SIZE * sizeof(BiTNode));
if(!s.base) exit(-1);
s.top = s.base;
s.stack_size = STACK_INIT_SIZE;
return true;
}
bool GetTop(const SqStack &s, BiTree &e)
{
if(s.base == s.top) exit(-1);
e = *(s.top-1);
return true;
}
bool Push(SqStack &s,BiTree e)
{
if((s.top - s.base ) >= STACK_INIT_SIZE)
{
s.base = (BiTree *)realloc(s.base,(STACK_INIT_SIZE + STACKINCREMENT) * sizeof(BiTNode));
if(!s.base) exit(-1);
s.stack_size += STACKINCREMENT;
s.top = s.base + s.stack_size;
}
*(s.top++) = e;
return true;
}
bool Pop(SqStack &s,BiTree &e)
{
if(s.top == s.base) exit(-1);
e = *(--s.top);
return true;
}
bool Clear(SqStack &s)
{
s.top = s.base;
return true;
}
bool IsEmpty(SqStack &s)
{
if(s.base == s.top)
return true;
return false;
}
BiTree CreateTree(BiTree &root)
{
int data = 0;
cin>>data;
if(data != 0)
{
//BiTree root = (BiTree)malloc(sizeof(BiTNode));
root->data = data;
BiTree root1 = (BiTree) malloc(sizeof(BiTNode));
root->lchild = CreateTree(root1);
BiTree root2 = (BiTree) malloc(sizeof(BiTNode));
root->rchild = CreateTree(root2);
}
else
{
root = NULL;
}
return root;
}
void PreOderPrintTree(const BiTree root)
{
if(root != NULL)
{
cout<<root->data<<endl;
PreOderPrintTree(root->lchild);
PreOderPrintTree(root->rchild);
}
}
void InOderPrintTree(const BiTree root)
{
if(root != NULL)
{
InOderPrintTree(root->lchild);
cout<<root->data<<endl;
InOderPrintTree(root->rchild);
}
}
void PostOderPrintTree(const BiTree root)
{
if(root != NULL)
{
PostOderPrintTree(root->lchild);
PostOderPrintTree(root->rchild);
cout<<root->data<<endl;
}
}
void TreeHeight(const BiTree root,int &Height)
{
int LHeight = 0;
int RHeight = 0;
if(root != NULL)
{
TreeHeight(root->lchild,LHeight);
TreeHeight(root->rchild,RHeight);
Height = 1+(LHeight>=RHeight?LHeight:RHeight);
}
else
{
Height = 0;
}
}
void TreeLeaves(const BiTree root,int &Leaves)
{
if(root != NULL)
{
if(root->lchild == NULL && root->rchild == NULL)
{
Leaves++;
}
TreeLeaves(root->lchild,Leaves);
TreeLeaves(root->rchild,Leaves);
}
}
void LevelOrderPrintTree(const BiTree root)
{
int front = 0;
int rear = 1;
BiTree TreeNode[100]; //最好用队列模拟,可以实时的删除元素。现在只是用了数组,最多有100个节点。
TreeNode[front] = root;
if(root == NULL)
return;
while(front < rear)
{
if(TreeNode[front] != NULL)
{
cout<<TreeNode[front]->data<<"\t";
TreeNode[rear++] = TreeNode[front]->lchild;
TreeNode[rear++] = TreeNode[front]->rchild;
}
front++;
}
cout<<"\n";
}
void TreeWidth(const BiTree root,int &Width)
{
if(root->lchild == NULL && root->rchild == NULL)
Width++;
else
{
int LWidth = 0;
int RWidth = 0;
if(root->lchild != NULL)
{
TreeWidth(root->lchild,LWidth);
}
if(root->rchild != NULL)
{
TreeWidth(root->rchild,RWidth);
}
Width = LWidth + RWidth;
}
}
void NonRecursionPrintTree(const BiTree root)
{
SqStack s;
InitStack(s);
BiTree TNode;
TNode = root;
while(TNode || !IsEmpty(s))
{
if(TNode)
{
Push(s,TNode);
TNode = TNode->lchild;
}
else
{
Pop(s,TNode);
cout<<TNode->data<<"\t";
TNode = TNode->rchild;
}
}
cout<<"\n";
}
int main()
{
BiTree root = (BiTree) malloc(sizeof(BiTNode));
root = CreateTree(root);
int Height = 0;
TreeHeight(root,Height);
cout<<"树的高度为:"<<Height<<endl;
int Leaves = 0;
TreeLeaves(root,Leaves);
cout<<"树的叶子节点数为:"<<Leaves<<endl;
cout<<"层序遍历二叉树:";
LevelOrderPrintTree(root);
int Width = 0;
TreeWidth(root,Width);
cout<<"树的宽度为:"<<Width<<endl;
cout<<"非递归访问二叉树为:";
NonRecursionPrintTree(root);
return 0;
}

浙公网安备 33010602011771号