数据结构 实验五 二叉树操作

 microsoft visal C++ 2008 编译通过

/OutOfBound.h

#ifndef OutOfBound_
#define OutOfBound_

class OutOfBound
{
public:
OutOfBound(){}
};
#endif

BadInput.h

#ifndef BadInput_
#define BadInput_
class BadInput
{
public:
BadInput(){}
};
#endif

///NoMem.h

#ifndef NoMem_
#define NoMem_
class NoMem
{
public:
NoMem(){}
};
#endif

//LinkedQueue.h

#ifndef LinkedQueue_
#define LinkedQueue_
#include <iostream.h>
//#include "LinkedQueue.h"
#include "NoMem.h"
#include "Node.h"
#include "BinaryTreeNode.h"
#include "Classes.h"
class LinkedQueue
{
public:
   LinkedQueue()
   {front=rear=0;}
   ~LinkedQueue();
   bool IsEmpty()const
   {return ((front)? false: true);}
   bool IsFull() const;
   BinaryTreeNode* First() const;
   BinaryTreeNode* Last() const;
   LinkedQueue& Add( BinaryTreeNode * x);
   LinkedQueue& Delete(BinaryTreeNode * x);
private:
   Node *front;
   Node *rear;
};
LinkedQueue::~LinkedQueue()
{
Node *next;
while(front)
{
   next=front->link;
   delete front;
   front=next;
}
}

bool LinkedQueue::IsFull() const
{
Node *p;
try{
   p=new Node;
   delete p;
   return false;
}
catch(NoMem){return true;}
}

BinaryTreeNode* LinkedQueue::First()const
{
if(IsEmpty())throw OutOfBound();
return front->data;
}

BinaryTreeNode* LinkedQueue::Last()const
{
if(IsEmpty())throw OutOfBound();
return rear->data;
}
LinkedQueue& LinkedQueue::Add( BinaryTreeNode * x)
{
Node *p=new Node;
p->data=x;
p->link=0;
if(front)
   rear->link=p;
else
   front=p;
rear=p;
return *this;
}
LinkedQueue& LinkedQueue::Delete(BinaryTreeNode* x)
{
if(IsEmpty())throw OutOfBound();
x=front->data;
Node *p=front;
front=front->link;
delete p;
return *this;
}
#endif

///Node.h

#ifndef Node_
#define Node_
#include <iostream.h>
#include "LinkedQueue.h"
#include "BinaryTreeNode.h"

class Node
{
friend class LinkedQueue;
private:
   BinaryTreeNode* data;
   Node *link;
};
#endif

/BinaryTreeNode.h

#ifndef BinaryTreeNode_
#define BinaryTreeNode_
#include <iostream.h>
#include "LinkedQueue.h"
//#include "BinaryTree.h"
//#include "Node.h"
//#include "BinaryTreeNode.h"
#include "Classes.h"
class BinaryTreeNode//链表二叉树的节点类
{
friend class BinaryTree;
public:
BinaryTreeNode(){LeftChild=RightChild=0;}
BinaryTreeNode(const int& e)
{
   data=e;
   LeftChild=RightChild=0;}
BinaryTreeNode(const int& e,BinaryTreeNode *l,BinaryTreeNode *r)
{data=e;
LeftChild=l;
RightChild=r;}
private:
int data;
BinaryTreeNode *LeftChild,*RightChild;//左右子树
};
#endif
BinaryTree.h

#ifndef BinaryTree_
#define BinaryTree_

#include <iostream.h>
#include "LinkedQueue.h"
#include "BinaryTreeNode.h"
#include "Classes.h"
#include "BadInput.h"
int _count;
class BinaryTree
{friend class BinaryTreeNode;
public:
BinaryTree(){root=0;}
//BinaryTree(BinaryTreeNode* t){root=t;}
~BinaryTree(){}
bool IsEmpty() const
{return ((root)?false :true);}
bool Root(int & x)const;
void MakeTree(const int &element,BinaryTree & left,BinaryTree & right);
void BreakTree(int & element,BinaryTree & left,BinaryTree & right);
void PreOrder(void(*Visit)(BinaryTreeNode *u))
{PreOrder(Visit,root);}
void PostOrder(void(*Visit)(BinaryTreeNode *u))
{PostOrder(Visit,root);}
void InOrder(void(*Visit)(BinaryTreeNode *u))
{InOrder(Visit,root);}
void LevelOrder(void(*Visit)(BinaryTreeNode *u));
BinaryTreeNode* store(int*A,int*B,int mid,int pre,int last,int n);
static void Output(BinaryTreeNode *t)
{cout<<t->data<<" ";}
void PreOutput()
{PreOrder(Output,root);
cout<<endl;}
void InOutput()
{InOrder(Output,root);
cout<<endl;}
void PostOutput()
{PostOrder(Output,root);
cout<<endl;}
void LevelOutput()
{LevelOrder(Output);
cout<<endl;}
void Delete()
{PostOrder(Free,root);
root=0;}
int Height() const
{return Height(root);}
int size()
{
   _count=0;
   PreOrder(Addl,root);
   return _count;
}
void yun(BinaryTreeNode* t);
private:
BinaryTreeNode *root;
int Height(BinaryTreeNode *t)const;
void PreOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t);
void PostOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t);
void InOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t);
static void Free(BinaryTreeNode *t)
{delete t;}
static void Addl(BinaryTreeNode *t)
{_count++;}

};

//前序遍历
void BinaryTree::PreOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t)
{//对t*进行前序遍历
if(t)
{Visit(t);//访问根节点
PreOrder(Visit,t->LeftChild);//前序遍历左子树
PreOrder(Visit,t->RightChild);//前序遍历右子树
}
}


//中序遍历
void BinaryTree::InOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t)
{//对t*进行中序遍历
if(t)
{
   InOrder(Visit,t->LeftChild);//中序遍历左子树
   Visit(t);//访问根节点
   InOrder(Visit,t->RightChild);//中序遍历右子树
}
}

//后序遍历
void BinaryTree::PostOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t)
{//对t*进行后序遍历
if(t)
{
   PostOrder(Visit,t->LeftChild);//后序遍历左子树
   PostOrder(Visit,t->RightChild);//后序遍历右子树
   Visit(t);//访问根节点
}
}


//逐层遍历
void BinaryTree::LevelOrder(void(*Visit)(BinaryTreeNode *u))
{//对t*进行遍历
    LinkedQueue Q;
BinaryTreeNode *t;
t=root;
while(t)
{ Visit(t);//访问根节点
   cout<<"访问根节点~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  
   if(t->LeftChild)
   { Q.Add(t->LeftChild);//遍历左子树
   cout<<"访问左子树~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
   }
   if(t->RightChild)
   { Q.Add(t->RightChild);//遍历右子树
   cout<<"访问右子树~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
   }
   try{Q.Delete(t);
   cout<<"出栈操作~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
   }
   catch(OutOfBound){return;}
   return;
  
}
}

//共享成员函数的实现
bool BinaryTree::Root(int & x)const
{
if(root)
{
   x=root->data;
   return true;}
else
   return false;
}

void BinaryTree::MakeTree(const int &element,BinaryTree & left,BinaryTree & right)
{
root=new BinaryTreeNode(element,left.root,right.root);
left.root=right.root=0;
}

void BinaryTree::yun(BinaryTreeNode* t)
{
root=t;
}

void BinaryTree::BreakTree(int &element,BinaryTree & left,BinaryTree & right)
{
if(!root)throw BadInput();
element=root->data;
left.root=root->LeftChild;
right.root=root->RightChild;
delete root;
root=0;
}

int BinaryTree::Height(BinaryTreeNode *t)const
{
if(!t)
   return 0;
int hl=Height(t->LeftChild);
int hr=Height(t->RightChild);
if(hl>hr)
   return ++hl;
else
   return ++hr;
}
BinaryTreeNode* BinaryTree::store(int*A,int*B,int mid,int pre,int last,int n)
{
BinaryTreeNode *p;
int r,i;
r=A[mid];
p=new BinaryTreeNode(r);

for(i=pre;i<=last;i++)
if(B[i]==r)
   break;


if((pre<i)&&(pre<last)&&(mid+1<n)&&(i<=last))
{
   p->LeftChild=store(A,B,mid+1,pre,i-1,n);}
if((i<last)&&(pre<last)&&(mid+i+1-pre<n)&&(i>=pre))
{
p->RightChild=store(A,B,mid+i+1-pre,i+1,last,n);}
return p;
}

#endif

main

#include <iostream>
using namespace std;
#include "LinkedQueue.h"
#include "BinaryTree.h"
#include "Node.h"
#include "BinaryTreeNode.h"

void main(){
    int choose;
    BinaryTree e;
    while(1){
      cout<<"请输入你要进行的操作:"<<endl;
      cout<<"1.由前序和后序构建一颗Tree   2.前序遍历  3.中序遍历  4.后序遍历  5.计算二叉树结点数目  6.计算二叉树高度"<<endl;
      cin>>choose;
      switch(choose){
        case 1:
           {int n;
      int * A;
            int * B;
   cout<<"输入元素的个数为"<<endl;
   cin>>n;
   A=new int[n];
            B=new int[n];
            cout<<"前序输入:"<<endl;
            for(int i=0;i<n;i++)
               cin>>A[i];
            cout<<"中序输入:"<<endl;
            for(int j=0;j<n;j++)
                cin>>B[j];
            cout<<endl;
            e.yun(e.store(A,B,0,0,n-1,n));
   cout<<"前序输出新建立的树:"<<endl;
            e.PreOutput();
            }
  case 2:
   {
            cout<<"前序输出:"<<endl;
            e.PreOutput();
            cout<<endl;
      }
        case 3:
   {
            cout<<"中序输出:"<<endl;
            e.InOutput();
            cout<<endl;
   }
        case 4:
   {
            cout<<"后序输出:"<<endl;
            e.PostOutput();
            cout<<endl;
   }
  case 5:
   {
            cout<<"结点个数为:"<<e.size()<<endl;
            cout<<endl;
   }
  case 6:
   {
   cout<<"树高为:"<<e.Height()<<endl;
            cout<<endl;
   break;
   }
  default:
   cout<<"选择错误"<<endl;
     }
    int * C;
    int * D;
    int m;
 cout<<"接收键盘录入的二叉树前序序列和中序序列,输出该二叉树的后序序列"<<endl;
    cout<<"输入元素个数:"<<endl;
    cin>>m;
    C=new int[m];
    D=new int[m];
    cout<<"前序输入:"<<endl;
    for(int i=0;i<m;i++)
      cin>>C[i];
    cout<<"中序输入:"<<endl;
    for(int j=0;j<m;j++)
      cin>>D[j];
    cout<<endl;
    BinaryTree y;
    y.yun(e.store(C,D,0,0,m-1,m));
    cout<<"后序输出:"<<endl;
    y.PostOutput();
    cout<<endl;
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值