Pages: 1/1 First page 1 Final page [ View by Articles | List ]

实验十  二叉树的基本操作

| May 27, 2008 16:34 | timmy | Via Original
Test10.cpp

#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
typedef char ElemType;
struct BTreeNode{
  ElemType data;
  BTreeNode* left;
  BTreeNode* right;
};
#include "test9_stack.h"
#include "test10.h"
void main()
{
  BTreeNode* bt;
  InitBTree(bt);
  char b[50];
  cout<<"输入二叉树用广义表表示的字符串:"<<endl;
  cin.getline(b,sizeof(b));
  CreateBTree(bt,b);
  PrintBTree(bt);
  cout<<endl;
  cout<<"前序:";
  PreOrder(bt);
  cout<<endl;
  cout<<"中序:";
  InOrder(bt);
  cout<<endl;
  cout<<"中序(非递归算法):";
  InOrder_fdg(bt);
  cout<<endl;
  cout<<"后序:";
  PostOrder(bt);
  cout<<endl;
  cout<<"按层:";
  LevelOrder(bt);
  cout<<endl;
  ElemType x;
  cout<<"输入一个待查字符:"<<endl;
  cin>>x;
  if(FindBTree(bt,x))
    cout<<"查找字符"<<x<<"成功!"<<endl;
  else
    cout<<"查找字符"<<x<<"失败!"<<endl;
  cout<<"深度:";
  cout<<DepthBTree(bt)<<endl;
  if(ChangeBTree(bt)){
    printf("将二叉树中的所有结点的左右子树进行交换后: \n");
    PrintBTree(bt);
    cout<<endl;
  }
  printf("二叉树中的结点总数: %d \n",CountBTree(bt));
  BTreeNode* newbt;
  newbt=CopyBTree(bt);
  printf("复制二叉树得新二叉树:\n");
  PrintBTree(newbt);
  ClearBTree(bt);
  ClearBTree(newbt);
}
Tags: ,
Pages: 1/1 First page 1 Final page [ View by Articles | List ]