树和二叉树应该是考研中比较重要的考点,这里的代码仅供自己参考!!!
/**
* 作者:LinX 2017-6-29 - 2017-7-1
*
* 内容: 二叉树的结构及其操作
*
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef char ElemType;
typedef struct BTNode
{
ElemType data;
struct BTNode *lChild;
struct BTNode *rChild;
}BTNode,BTree;
BTree* createBTree(); //创建二叉树
void preOrder(BTree *bt); //递归方法先序遍历
void inOrder(BTree *bt); //递归方法中序遍历
void postOrder(BTree *bt); //递归方法后序遍历
void preorderNonRecursion(BTree *bt); //非递归方法先序遍历
void inorderNonRecursion(BTree *bt); //非递归方法中序遍历
void postorderNonRecursion(BTree *bt); //非递归方法后序遍历
int leavesCount(BTree *bt); //求叶子结点数目
int depthCount(BTree *bt); //求二叉树深度
int main()
{
int depth,nums=0;
BTree *bt=createBTree();
printf("先序遍历:");
preorderNonRecursion(bt);
printf("\n");
printf("中序遍历:");
inorderNonRecursion(bt);
printf("\n");
printf("后序遍历:");
postorderNonRecursion(bt);
/* //先序遍历
preOrder(bt);
printf("\n");
//中序遍历
inOrder(bt);
printf("\n");
//后序遍历
postOrder(bt);
//二叉树深度
depth=depthCount(bt);
printf("\n%d\n",depth);
//求叶子结点的数目
printf("%d",leavesCount(bt));*/
return 0;
}
/*非递归方法中序遍历*/
void inorderNonRecursion(BTree *bt)
{
BTNode *stack[MAXSIZE],*p;
int top=-1;
p=bt;
while(top!=-1||p!=NULL)
{
while(p!=NULL)
{
stack[++top]=p;
p=p->lChild;
}
if(top!=-1)
{
p=stack[top--];
printf("%c ",p->data);
p=p->rChild;
}
}
}
/*非递归方法先序遍历:先将根节点入栈,出栈,再将右孩子,左孩子入栈,再将左孩子出栈
出栈的同时将右孩子,左孩子入栈,以此类推,直到栈空*/
void preorderNonRecursion(BTree *bt)
{
BTNode *stack[MAXSIZE],*p;
int top=-1;
stack[++top]=bt;
while(top!=-1)
{
p=stack[top--];
printf("%c ",p->data);
if(p->rChild!=NULL)
{
stack[++top]=p->rChild;
}
if(p->lChild!=NULL)
{
stack[++top]=p->lChild;
}
}
}
/*非递归方法后序遍历*/
void postorderNonRecursion(BTree *bt)
{
BTNode *p;
BTNode* stack1[MAXSIZE];
int top1=-1;
BTNode* stack2[MAXSIZE];
int top2=-1;
stack1[++top1]=bt;
while(top1!=-1)
{
p=stack1[top1--];
stack2[++top2]=p;
printf("%c ",p->data);
if(p->lChild!=NULL)
{
stack1[++top1]=p->lChild;
}
if(p->rChild!=NULL)
{
stack1[++top1]=p->rChild;
}
}
while(top2!=-1)
{
printf("%c ",stack2[top2--]->data);
}
}
/*求叶子结点的数目*/
int nums=0;
int leavesCount(BTree *bt)
{
if(bt!=NULL)
{
if(bt->lChild==NULL&&bt->rChild==NULL)
{
return ++nums;
}
else
{
leavesCount(bt->lChild);
leavesCount(bt->rChild);
}
}
}
/*求二叉树深度*/
int depthCount(BTree *bt)
{
int left,right;
if(bt==NULL)
{
return 0;
}
left=depthCount(bt->lChild)+1;
right=depthCount(bt->rChild)+1;
return (left>right)?left:right;
//简便写法
//return depthCount(bt->lChild)+1>depthCount(bt->rChild)+1?depthCount(bt->lChild)+1:depthCount(bt->rChild)+1;
}
/*先序遍历*/
void preOrder(BTree *bt)
{
if(bt!=NULL)
{
printf("%c ",bt->data);
preOrder(bt->lChild);
preOrder(bt->rChild);
}
}
/*中序遍历*/
void inOrder(BTree *bt)
{
if(bt!=NULL)
{
inOrder(bt->lChild);
printf("%c ",bt->data);
inOrder(bt->rChild);
}
}
/*后序遍历*/
void postOrder(BTree *bt)
{
if(bt!=NULL)
{
postOrder(bt->lChild);
postOrder(bt->rChild);
printf("%c ",bt->data);
}
}
/*创建二叉树 */
BTree* createBTree()
{
BTNode *btn;
ElemType c;
scanf("%c",&c);
if(c=='#')
{
btn=NULL;
}
else
{
btn=(BTNode *)malloc(sizeof(BTNode));
btn->data=c;
btn->lChild=createBTree();
btn->rChild=createBTree();
}
return btn;
}