首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟我是c++新人,求指教

2012-05-20 
各位大哥我是c++新人,求指教#include iostreamusing namespace stdstruct BinTreeNode {//二叉树结点类

各位大哥我是c++新人,求指教
#include "iostream"
using namespace std;
struct BinTreeNode { //二叉树结点类定义
  char data; //数据域
  BinTreeNode *leftChild, *rightChild; //左子女、右子女链域
  BinTreeNode () //构造函数
  { leftChild=NULL; rightChild=NULL; }
BinTreeNode (char x, BinTreeNode *l,  
  BinTreeNode *r)
  { data=x; leftChild=l; rightChild=r; }
};
class BinaryTree {//二叉树类定义
public:
BinaryTree(){root=NULL;} //构造函数
  BinaryTree(BinTreeNode *current){root=current;}
  ~BinaryTree(){destroy(root);}//析构函数
friend void CreaBintree(istream& in,BinTreeNode *&subTree);//从键盘输入
  
  void InOrder (BinTreeNode *current);
  //中序遍历
  void PreOrder(BinTreeNode *current); 
  //前序遍历
  void PostOrder (BinTreeNode *current);
  //后序遍历
  void LevelOrder (BinTreeNode *current); //层次序遍历

  BinTreeNode *root;
void destroy(BinTreeNode *& subtree);
};
void BinaryTree::CreateBinTree(BinTreeNode* subTree){
  char item;
  if (item!='#') {
  subTree=new BinTreeNode(item);
  CreateBinTree(subTree->leftChild);
  CreateBinTree(subTree->rightChild);
  }
 else subTree=NULL;
   
};

void BinaryTree::destroy (BinTreeNode *subTree) {
//私有函数: 删除根为subTree的子树
  if (subTree!=NULL) {
  destroy (subTree->leftChild); //删除左子树
  destroy (subTree->rightChild); //删除右子树
  delete subTree; //删除根结点
}
};

/*void istream& operator >> (istream& in, BinaryTreeNode *subTree) {
//重载操作: 输入并建立一棵二叉树Tree。in是
//输入流对象。
  CreateBinTree (in, *subTree); //建立二叉树
  return in;
}*/
void BinaryTree::BinaryTree(BinTreeNode* subTree) {
  if (subTree!=NULL) {
  InOrder (subTree->leftChild); //遍历左子树
  cout<<subTree->data;//访问根
InOrder (subTree->rightChild);//遍历右子树结点
}
};

void BinaryTree::PreOrder (BinTreeNode* subTree) {
  if (subTree!=NULL) {
cout<<subtree->data;//访问根结点
PreOrder (subTree->leftChild);
  //遍历左子树
PreOrder (subTree->rightChild);
  //遍历右子树
}
};
void BinaryTree::InOrder (BinTreeNode* subTree) {
  if (subTree!=NULL) {

PreOrder (subTree->leftChild);
cout<<subtree->data;//访问根结点
  //遍历左子树
PreOrder (subTree->rightChild);
  //遍历右子树
}
};

void BinaryTree::PostOrder (BinTreeNode* subTree ) {
  if (subTree!=NULL ) {
  PostOrder (subTree->leftChild);
  //遍历左子树
PostOrder (subTree->rightChild); //遍历右子树
cout<<subtree->data; //访问根结点
}
};
void BinaryTree::PrintBTree(BinTreeNode *BT){
if(BT!=NULL){
cout<<BT->data;
if(BT->leftChild!=NULL||BT->rightChild!=NULL){
cout<<'(';
PrintBTree(BT->leftChild);
cout<<',';
if(BT->rightChild!=NULL)
coutPrintBTree(BT->rightChild);
cout<<')';
}
}
};

void main(){
BinTreeNde *root;
CreatBinTree(std::cin,root);
BinaryTree BT(root);
cout<<"1.以广义表的形式输出:"<<endl;
BT.PrintBTree(root);
cout<<endl<<"2.前序遍历输出结点:"<<endl;
BT.preOrder(root);


cout<<endl<<"3.中序遍历输出结点:"<<endl;
BT.InOrder(root);
cout<<endl<<"4.后序遍历输出结点:"<<endl;
BT.PostOrder(root);
//cout<<endl<<"5.层序遍历输出结点:"<<endl;
//BT.LevelOrder(root);
//cout<endl;
}
请把这个程序改到可以后给我,谢谢了


[解决办法]
C++实现二叉树的代码很多,看懂后再去调试自己的代码

热点排行