二叉树的操作

二叉树特点:非线性

#include"Bitree.h"
#ifndef _STACK_H_
#define _STACK_H_
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef BiTree ElemType;
typedef struct
{
 ElemType *base;
 ElemType *top;
 int stacksize;
}SqStack;
Status InitStack(SqStack &S);
Status DestroyStack(SqStack &S);
Status ClearStack(SqStack &S);
Status StackEmpty(SqStack S);
Status Push(SqStack &S, ElemType e);
Status Pop(SqStack &S, ElemType &e);
#endif

#include"Bitree.h"
#ifndef _QUEUE_H_
#define _QUEUE_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef BiTree ElemType;
typedef struct QNode
{
 ElemType data;
 struct QNode *next;
}QNode, *Queueprt;
typedef struct
{
 Queueprt front;//队头指针
 Queueprt rear;//队尾指针
}LinkQueue;
Status InitQueue(LinkQueue &Q);
Status DestoryQueue(LinkQueue &Q);
Status ClearQueue(LinkQueue &Q);
Status EmptyQueue(LinkQueue &Q);
Status EnterQueue(LinkQueue &Q, ElemType e);
Status DelecetQueue(LinkQueue &Q, ElemType &e);
#endif

#ifndef _BITREE_H_
#define _BITREE_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW_s -2
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
 TElemType data;
 struct  BiTNode *lchild, *rchild;
} BiTNode,*BiTree;

Status CreateBiTree(BiTree &T);
Status DestroyBiTree(BiTree &T);
Status BiTreeDepth(BiTree T);
Status countleaf(BiTree T);
Status count_n(BiTree T);
Status single_point(BiTree T);
Status double_point(BiTree T);
Status FindPoint(BiTree T, TElemType e);
void change_left_right(BiTree T);
void PreOrderTraverse(BiTree T);
void InOrderTraverse(BiTree T);
void NextOrderTraverse(BiTree T);
void LevelOrder(BiTree T);
void U_InOrderTraverse(BiTree T);
#endif

#include"stack.h"
#include"Bitree.h"
#include<iostream>
using namespace std;
//构造空栈
Status InitStack(SqStack &S)
{
 S.base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
 if (!S.base)
 {
  return OVERFLOW;
 }
 else
 {
  S.top = S.base;
  S.stacksize = STACK_INIT_SIZE;
  return OK;
 }
}
//销毁栈
Status DestroyStack(SqStack &S)
{
 ElemType e;
 if (S.top != S.base)
 {
  Pop(S, e);
 }
 return OK;
}
//清空栈
Status ClearStack(SqStack &S)
{
 if (S.top != S.base)
 {
  S.top = S.base;
 }
 return OK;
}
//置空栈
Status StackEmpty(SqStack S)
{
 if (S.top == S.base)
 {
  return OK;
 }
 else
 {
  return ERROR;
 }
}
//入栈
Status Push(SqStack &S, ElemType e)
{
 if (S.top - S.base >= S.stacksize)
 {
  S.base = (ElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(ElemType));
  if (!S.base)
  {
   return OVERFLOW;
  }
  else
  {
   S.top = S.base + S.stacksize;
   S.stacksize += STACKINCREMENT;
  }
 }
 else
 {
  *S.top++ = e;
  return OK;
 }
}
//出栈
Status Pop(SqStack &S, ElemType &e)
{
 if (S.top == S.base)
 {
  return ERROR;
 }
 else
 {
  e = *--S.top;
  return OK;
 }
}

#include"Queue.h"
#include"Bitree.h"
#include<iostream>
using namespace std;
//构造空队列
Status InitQueue(LinkQueue &Q)
{
 Q.rear = Q.front = (Queueprt)malloc(sizeof(QNode));
 if (!Q.front)
 {
  return OVERFLOW;
 }
 else
 {
  Q.front->next = NULL;
  return OK;
 }
}
//销毁队列
Status DestoryQueue(LinkQueue &Q)
{
 while (Q.front)
 {
  Q.rear = Q.front->next;
  free(Q.front);
  Q.front = Q.rear;
 }
 return OK;
}
//清空队列
Status ClearQueue(LinkQueue &Q)
{
 Q.rear = Q.front;
 return OK;
}
//置空队列
Status EmptyQueue(LinkQueue &Q)
{
 if (Q.rear == Q.front)
 {
  return TRUE;
 }
 else
 {
  return ERROR;
 }
}
//入队
Status EnterQueue(LinkQueue &Q, ElemType e)
{
 Queueprt p;
 p = (Queueprt)malloc(sizeof(QNode));
 if (!p)
 {
  return OVERFLOW;
 }
 else
 {
  p->data = e;
  p->next = NULL;
  Q.rear->next = p;
  Q.rear = p;
  return OK;
 }
}
//出队
Status DelecetQueue(LinkQueue &Q, ElemType &e)
{
 Queueprt p;
 p = Q.front->next;
 if (Q.rear == Q.front)
 {
  return ERROR;
 }
 else
 {
  e = p->data;
  Q.front->next = p->next;
  if (Q.rear == p)
  {
   Q.rear = Q.front;
  }
  free(p);
  return OK;
 }
}

#include"Bitree.h"
#include"Queue.h"
#include"Stack.h"
#include<iostream>
using namespace std;
//构造二叉树
Status CreateBiTree(BiTree &T)
{
 char ch;
 ch = getchar();
 if (ch == '#')
 {
  T = NULL;
 }
 else
 {
  if (!(T = (BiTNode*)malloc(sizeof(BiTNode))))
  {
   exit(OVERFLOW_s);
  }
  else
  {
   T->data = ch;
   CreateBiTree(T->lchild);
   CreateBiTree(T->rchild);
  }
 }
 return OK;
}
//销毁二叉树
Status DestroyBiTree(BiTree &T)
{
 if (T)
 {
  DestroyBiTree(T->lchild);
  DestroyBiTree(T->rchild);
  free(T);
  T = NULL;
 }
 return OK;
}
//求二叉树的深度
Status BiTreeDepth(BiTree T)
{
 int depth,depthleft,depthright;
 if (!T)
 {
  depth = 0;
 }
 else
 {
  depthleft = BiTreeDepth(T->lchild);
  depthright = BiTreeDepth(T->rchild);
  depth = 1 + (depthleft > depthright ? depthleft : depthright);
 }
 return depth;
}
//求叶子节点数
Status countleaf(BiTree T)
{
 int  num,num1,num2;
 if (!T)
 {
  num = 0;
 }
 else
 {
  if (T->lchild == NULL&&T->rchild == NULL)
  {
   num = 1;
  }
  else
  {
   num1 = countleaf(T->lchild);
   num2 =   countleaf(T->rchild);
   num = num1 + num2;
  }
 }
 return num;
}
//求总结点数
Status count_n(BiTree T)
{
 int num1, num2, num;
 if (!T)
 {
  num = 0;
 }
 else if (T->lchild == NULL&&T->rchild == NULL)
 {
  num = 1;
 }
 else
 {
  num1 = count_n(T->lchild);
  num2 = count_n(T->rchild);
  num = num1 + num2+1;
 }
 return num;
}
//求单分支结点个数
Status single_point(BiTree T)
{
 int  num;
 if (!T)
 {
  num = 0;
 }
 else
 {
  if (T->lchild == NULL&&T->rchild == NULL)
  {
   num = 0;
  }
  else
  {
   if (T->lchild == NULL||T->rchild == NULL)
   {
    if (T->lchild != NULL && T->rchild == NULL)
    {
     num= single_point(T->lchild) + 1;
    }
    else
    {
     num= single_point(T->rchild) + 1;
    }
   }
   else
   {
    num= single_point(T->lchild) + single_point(T->rchild);
   }
  }
 }
 return num;
}
//求双分支结点的个数
Status double_point(BiTree T)
{
 int num1,num2,num;
 if (!T)
 {
  num = 0;
 }
 else
 {
  if (T->lchild == NULL&&T->rchild == NULL)
  {
   num = 0;
  }
  else
  {
   num1 = double_point(T->lchild);
   num2 = double_point(T->rchild);
   if (T->lchild != NULL&&T->rchild != NULL)
   {
    num = num1 + num2 + 1;
   }
   else
   {
    num = num1 + num2;
   }
  }
 }
 return num;
}
//查找某个结点
Status FindPoint(BiTree T, TElemType ch)
{
 if (!T)
 {
  return ERROR;
 }
 else
 {
  if (T->data == ch)
  {
   return OK;
  }
  else
  {
   if (FindPoint(T->lchild, ch) || FindPoint(T->rchild, ch))
   {
    return OK;
   }
   else
   {
    return ERROR;
   }
  }
 }
}
//交换左右子树
void change_left_right(BiTree T)
{
 BiTree temp;
 if (T)
 {
  change_left_right(T->lchild);
  change_left_right(T->rchild);
  temp = T->lchild;
  T->lchild = T->rchild;
  T->rchild = temp;
 }
}
//前序遍历
void PreOrderTraverse(BiTree T)
{
 if (T)
 {
  cout << T->data << " ";
  PreOrderTraverse(T->lchild);
  PreOrderTraverse(T->rchild);
 }
}
//中序遍历
void InOrderTraverse(BiTree T)
{
 if (T)
 {
  InOrderTraverse(T->lchild);
  cout << T->data << " ";
  InOrderTraverse(T->rchild);
 }
}
//后序遍历
void NextOrderTraverse(BiTree T)
{
 if (T)
 {
  NextOrderTraverse(T->lchild);
  NextOrderTraverse(T->rchild);
  cout << T->data << " ";
 }
}
//按层次遍历
void LevelOrder(BiTree T)
{
 LinkQueue Q;
 ElemType p;
 InitQueue(Q);
 if (T)
 {
  EnterQueue(Q, T);
  while (!EmptyQueue(Q))
  {
   DelecetQueue(Q, p);
   cout << p->data<<" ";
   if (p->lchild != NULL)
   {
    EnterQueue(Q, p->lchild);
   }
   if (p->rchild != NULL)
   {
    EnterQueue(Q, p->rchild);
   }
  }
 }
}
//非递归中序遍历
void U_InOrderTraverse(BiTree T)
{
 SqStack S;
 InitStack(S);
 ElemType p = T;
 while (p || !StackEmpty(S))
 {
  if (p)
  {
   Push(S, p);
   p = p->lchild;
  }
  else
  {
   Pop(S, p);
   cout << p->data<<" ";
   p = p->rchild;
  }
 }
}


#include"Bitree.h"
#include<iostream>
using namespace std;
int main()
{
 BiTree T,f=NULL,p,q,s;
 TElemType ch;
 int select,num;
 CreateBiTree(T);
 do
 {
  cout << "1.求二叉树的深度!"<<endl;
  cout << "2.求叶子节点数!"<<endl;
  cout << "3.求总结点数!" << endl;
  cout << "4.求单分支结点个数!" << endl;
  cout << "5.求双分支结点的个数" << endl;
  cout << "6.查找某个结点" << endl;
  cout << "7.交换左右子树" << endl;
  cout << "8.前序遍历" << endl;
  cout << "9.中序遍历" << endl;
  cout << "10.后序遍历" << endl;
  cout << "11.按层遍历!" << endl;
  cout << "12.非递归中序遍历!" << endl;
  cout << "13.操作结束!" << endl;
  cout << "请选择:" << endl;
  cin >> select;
  switch (select)
  {
  case 1:
   cout << "二叉树的深度为:" << BiTreeDepth(T) << endl;
   break;
  case 2:
   cout << "叶子结点的个数为:" << countleaf(T) << endl;
   break;
  case 3:
   cout << "总结点数为:" << count_n(T) << endl;
   break;
  case 4:
   cout << "单分支结点的个数为:" << single_point(T) << endl;
   break;
  case 5:
   cout << "双分支结点的个数为:" << double_point(T) << endl;
   break;
  case 6:
   cout << "请输入结点:" << endl;
   cin >> ch;
   if (FindPoint(T, ch) == ERROR)
   {
    cout << "二叉树为空或未找到!" << endl;
   }
   else
   {
    cout << "找到了!" << endl;
   }
   break;
  case 7:
   change_left_right(T);
   break;
  case 8:
   PreOrderTraverse(T);
   cout << endl;
   break;
  case 9:
   InOrderTraverse(T);
   cout << endl;
   break;
  case 10:
   NextOrderTraverse(T);
   cout << endl;
   break;
  case 11:
   LevelOrder(T);
   cout << endl;
   break;
  case 12:
   U_InOrderTraverse(T);
   cout << endl;
   break;
  case 13:
   cout << "操作结束!" << endl;
   break;
  default:
   cout << "输入错误!" << endl;
  }
 } while (select != 13);
 DestroyBiTree(T);//end of Bitree
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值