SJTU OJ (1039)

本文介绍了一个使用链表实现的队列类和一个顺序存储的二叉树类。链表队列提供了基本的入队和出队操作,并实现了动态内存管理。顺序二叉树采用数组存储节点信息,支持创建、展示及后序遍历等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

using namespace std;

template <class elemType>
class linkQueue{
  private:
      struct node{
        elemType data;
        node *next;
        node(const elemType &x,node *p=NULL){data=x; next=p;}
        node():next(NULL){}
        ~node(){}
      };
      node *Front,*rear;
  public:
    linkQueue(){Front=rear=NULL;}
    ~linkQueue();
    void enQueue(const elemType &x);
    elemType deQueue();
    bool isEmpty(){return (Front==NULL);}
    elemType getHead(){return Front->data;}
};

template <class elemType>
linkQueue<elemType>::~linkQueue(){
  node *q=Front,*p;
  while(q!=NULL){
    p=q->next;
    delete q;
    q=p;
  }
}

template <class elemType>
void linkQueue<elemType>::enQueue(const elemType &x){
  node *tmp;
  if (Front==NULL) {
    tmp=new node(x);
    rear=Front=tmp;
  }
  else{
    tmp=new node(x);
    rear->next=tmp;
    rear=tmp;
  }
}

template <class elemType>
elemType linkQueue<elemType>::deQueue(){
  node *tmp;
  elemType value=Front->data;
  tmp=Front;
  Front=Front->next;
  if (Front==NULL) rear=NULL;
  delete tmp;
  return value;
}


class seqBinaryTree{
  private:
      int *ltree,*rtree,*tree;
      int currentSize,maxSize1,maxSize2;
      void doubleSpace(int *&p,int &maxSize);
      void post(int i);
  public:
      seqBinaryTree(){ltree=new int[30000]{0}; rtree=new int[30000]{0}; tree=new int[30000]{0}; maxSize1=maxSize2=30000; currentSize=0;}
      ~seqBinaryTree(){delete [] ltree; delete [] rtree; delete [] tree;}
      void createTree(int n);
      void post();
      void show(int n);
};

void seqBinaryTree::post(){
  post(1);
}
void seqBinaryTree::post(int i){
    if (i==-1) return;
    post(ltree[i-1]);
    post(rtree[i-1]);
    cout<<i<<' ';
}

void seqBinaryTree::doubleSpace(int *&q,int &n){
  int *p=new int[n*2]{0};
  for (int i=0;i<n;i++)
     p[i]=q[i];
  delete [] q;
  q=p;
  n=n*2;
}

void seqBinaryTree::createTree(int n){
  while(n>maxSize1) {doubleSpace(ltree,maxSize1); doubleSpace(rtree,maxSize1);}
  int index,ct=0,tmp,j=0;
  linkQueue<int> que;
  for (int i=0;i<n;i++){
     cin>>index;
     cin>>ltree[index-1];
     cin>>rtree[index-1];
  }
  que.enQueue(1);
  while(ct<n){
    tmp=que.deQueue();
    if (j>=maxSize2-1) doubleSpace(tree,maxSize2);
    tree[j]=tmp;
    j++;
    if (tmp==-1){que.enQueue(-1);que.enQueue(-1);}
    else {ct++; que.enQueue(ltree[tmp-1]); que.enQueue(rtree[tmp-1]);}
  }
  currentSize=j;
}

void seqBinaryTree::show(int n){
  int *judge=new int[n]{0};
  for (int i=0;i<currentSize;i++)
    if (tree[i]!=-1)
    judge[tree[i]-1]=i+1;
  for (int j=0;j<n;j++)
    cout<<judge[j]<<' ';
}

int main()
{
    seqBinaryTree tree;
    int n;
    cin>>n;

    tree.createTree(n);
    tree.show(n);
    cout<<endl;
    tree.post();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值