博文

已知二叉树的前,中遍历结果,求后序遍历结果(2005-09-16 13:13:00)

摘要:// pre -- 先序
// in  -- 中序
void Post(List *pre, List *in)
{
  Element  e;
  POSITION pos;
  List     *lin, *rin;    
  List     *lpre,*rpre;
  e = GetFront(pre);  // 取得先序表中的第一个元素(必定为二叉树的根节点)
  pos = FindElement(in, e); // 根节点必定把中序分成左右两部分,
                            //左半部为左子树序列,右半部为右子树序列
  lin = GetLeft(in, pos);     // 以pos为界取得中序的左半部分
  rin = GetRight(in, pos);    // 取得右半部分
  lpre = FindLeft(pre, lin);  // 从先序中找到左子树部分
  rpre = FindRight(pre,rin); // 从先序中找到右子树部分
  Post(lpre, lin);           ......

阅读全文(3564) | 评论:0

编写程序将数组的前m个元素与后n个元素交换(2005-09-16 13:09:00)

摘要:/************************************************************************
      长度确定的数组大小为m+n(m、n各自大小不确定),
    编写程序将前m个元素与后n个元素交换,要求空间复杂度
 降到最小,时间复杂度尽量最小。
************************************************************************/
#include
#include template
void Swap2(T &x, T &y)
{
 T  temp;  temp = x;
 x    = y;
 y    = temp;
} // 把values数组的前nCount个元素与从nPos位置开始的nCount个元素交换位置
template
void SwapN(T values[], int nPos, int nCount)
{
 int  i;  for(i=0; i......

阅读全文(5181) | 评论:1