链表队列与二叉树基本操作

/*********************************
 * file : m.cpp
 * 功能: 1,建链表队列
         2,定义二叉树
   3,对二叉树进行中序,后序遍历
   4,对二叉树进行层次遍历
   5,计算二叉树的叶子数目
   6,计算二叉树的深度
   7,实现复制二叉树
   8,删除二叉树
 * 创建日期:2007-9-24
*********************************/

#include <iostream>
using namespace std;

template <typename T>
class qnode
{
public:
 qnode()
 {
  next = 0;
 }
 qnode(const T &v):value(v)
 {
  next = 0;
 }

public:
 T value;
 qnode<T> *next;
};

template <typename T>
class Quen
{
public:
 qnode<T> *front;
 qnode<T> *rear;

public:
 Quen():front(0),rear(0)
 {}
 Quen(const T &v)
 {
  qnode<T> *p = new qnode<T>(v);
  front = p;
  rear = p;
 }

 void push(const T &v)
 {
  qnode<T> *p = new qnode<T>(v);
  rear->next = p;
  rear = p;
 }
 
 void pop()
 {
  if(empty() == false)
  {
   qnode<T> *p = front;
   front = front->next;
   delete p;
  }
  else
   cout << "Queen empty!" << endl;
 }

 bool empty()
 {
  return !((front != 0) && (rear != 0));
 }

 T getfront()
 {
  return front->value;
 }
};

// 以上为链表队列的实现
// 以下为二叉树的遍历操作

template <typename T>
class tnode
{
public:
 tnode():left(0), right(0)
 {}
 tnode(const T &v, tnode<T> *lef =  0, tnode<T> *rig = 0):value(v),left(lef),right(rig)
 {}

public:
 T value;
 tnode<T> *left;
 tnode<T> *right;
};

//中序遍历
template <typename T>
void InOrder(tnode<T> *root)
{
 if( root != 0)
 {
  InOrder(root->left);
  cout << root->value << ' ';
  InOrder(root->right);
 }
}

// 后序遍历
template <typename T>
void PostOrder(tnode<T> *root)
{
 if( root != 0)
 {
  PostOrder(root->left);
  PostOrder(root->right);
  cout << root->value << ' ';
 }
}

// 借助链表队列实现迭代层次遍历
template <typename T>
void level(tnode<T> *root)
{
 Quen<tnode<T> *> *q = new Quen<tnode<T> *>(root);
 tnode<T> *p;

// q->push(root);
 while(q->empty() == false)
 {
  p = q->getfront();
  cout << p->value << ' ';
  if( p->left != 0 )
   q->push(p->left);
  if( p->right != 0 )
   q->push(p->right);
  q->pop();
 }
}

// 计算叶子数目
template <typename T>
void countleaf(tnode<T> *root, int &count)
{
 if( root != 0 )
 {
  if( (root->left == 0) && (root->right == 0))
   count++;

  countleaf(root->left, count);
  countleaf(root->right, count);
 }
}

// 计算树高度
template <typename T>
int depth(tnode<T> *root)
{
 int ldepth, rdepth, ndepth;
 if( root->left == 0 )
  ldepth = -1;
 else
  ldepth = depth(root->left);

 if( root->right == 0 )
  rdepth = -1;
 else
  rdepth = depth(root->right);

 ndepth = 1 + ((ldepth > rdepth) ? ldepth : rdepth);

 return ndepth;
}

// 复制二叉树
template <typename T>
tnode<T> *copytree( tnode<T> *root )
{
 tnode<T> *lp, *rp, *np;
 if( root == 0 )
  return 0;
 lp = copytree( root->left );
 rp = copytree( root->right );
 np = new tnode<T>(root->value, lp, rp);

 return np;
}

// 删除二叉树
template <typename T>
void deltree(tnode<T> *root)
{
 if( root != 0 )
 {
  deltree(root->left);
  deltree(root->right);
  delete root;
 }
}

int main()
{
 tnode<int> *p, *root;
 
 root = new tnode<int>(1);
 root = new tnode<int>(11, 0, root);
 p = new tnode<int>(4);
 root->left = p;
 p = new tnode<int>(2);
 p = new tnode<int>(6, 0, p);
 root = new tnode<int>(5, p, root);

 InOrder(root);
 cout << endl;

 PostOrder(root);
 cout << endl;

 level(root);
 cout << endl;

 int count = 0;
 countleaf(root, count);
 cout << "count = " << count << endl;

 cout << "depth = " << depth(root) << endl;

 level(copytree(root));
 cout << endl;
 
 //deltree(root);
// level(root);
 cout << endl;

 return 0;

 

输出:

6 2 5 4 11 1
2 6 4 1 11 5
5 6 11 2 4 1
count = 3
depth = 2
5 6 11 2 4 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值