请从用什么软件讲起……我们学过C++,但是数据结构课用的是清华大学的教材,老师说上面用的语言是类C,然后也没讲过用什么语句什么的突然就叫我们写程序,都不会啊……
我们用的软件是Microsoft Visual C++ 6.0
求高手!求讲解!
自己写的,运行通过,MFC的有点难弄,将就着用吧。
queue.h//链式队列用于层序遍历树
// queue.h: interface for the queue class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_QUEUE_H__6515EF6D_27AA_4799_95F8_6FE216E99F0F__INCLUDED_)
#define AFX_QUEUE_H__6515EF6D_27AA_4799_95F8_6FE216E99F0F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream.h>
#include <assert.h>
template<class T>
struct LinkNode
{
T data;
LinkNode<T> *link;
LinkNode(LinkNode<T> *ptr = NULL){link = ptr;}//仅初始化指针成员的初始函数.
LinkNode(const T& item,LinkNode<T> *ptr = NULL)
{data = item;link = ptr;} //初始化数据与指针成员的构造函数.
};
template <class T>
class queue
{
private:
LinkNode<T> *front,*rear;//
public:
queue():rear(NULL),front(NULL){};//
~queue(){makeempty();};//
bool EnQueue(T &x);//
bool DeQueue(T &x);//
bool getFront(T &x);//
void makeempty();//
bool Isempty()const{return front==NULL;}//
int getSize()const;//
friend ostream &operator<<(ostream &os,queue<T> &q);//
};
template<class T>
void queue<T>::makeempty()
{
LinkNode<T> *p;
while(front!=NULL)
{
p=front;
front=front->link;
delete p;
}
}
template <class T>
bool queue<T>::EnQueue(T &x)
{
if(front==NULL)
{
front=rear=new LinkNode<T>(x);
if(front==NULL)
return false;
}
else
{
rear->link=new LinkNode<T>(x);
if(rear==NULL)
return false;
rear=rear->link;
}
return true;
}
template <class T>
bool queue<T>::DeQueue(T &x)
{
if(Isempty())return false;
LinkNode<T> *p=front;
x=front->data;
front=front->link;
delete p;
return true;
}
template<class T>
bool queue<T>::getFront(T &x)
{
if(Isempty())return false;
x=front->data;
return true;
}
template <class T>
int queue<T>::getSize() const
{
LinkNode<T> *p=front;
int k=0;
while(p)
{
k++;
p=p->link;
}
return k;
}
#endif // !defined(AFX_QUEUE_H__6515EF6D_27AA_4799_95F8_6FE216E99F0F__INCLUDED_)
BinaryTree.h
// BinaryTree.h: interface for the BinaryTree class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_BINARYTREE_H__61FEB349_65EE_40FB_82DF_25FFBCBDFC6E__INCLUDED_)
#define AFX_BINARYTREE_H__61FEB349_65EE_40FB_82DF_25FFBCBDFC6E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream.h>
typedef int T;
struct BinTreeNode
{
T data;
BinTreeNode *leftChild,*rightChild;
BinTreeNode():leftChild(NULL),rightChild(NULL){}
BinTreeNode(T x,BinTreeNode *l=NULL,BinTreeNode *r=NULL):data(x),leftChild(l),rightChild(r){}
};
class BinaryTree {
public:
BinaryTree():root(NULL){} //
BinaryTree(T value):RefValue(value) ,root(NULL){} //
~BinaryTree(){destroy(root);}/////////
void CreateBinTree(){CreateBinTree(root);}
bool IsEmpty(){return(root==NULL)?true:false;}////////
BinTreeNode *Parent(BinTreeNode *current)/////////
{return(root==NULL||root==current)?NULL:Parent(root,current);}//////
BinTreeNode *LeftChild(BinTreeNode *current)////
{return(current!=NULL)?current->leftChild:NULL;}
BinTreeNode *RightChild(BinTreeNode *current)///////
{return(current!=NULL)?current->rightChild:NULL;}
int Height(){return Height(root);}///////
int Size(){return Size(root);}///////
BinTreeNode *getRoot()const{return root;}///////
void preOrder()///////
{preOrder(root);}
void inOrder()///////
{inOrder(root);}
void postOrder()///
{postOrder(root);}
void levelOrder()/////
{levelOrder(root);}
void destroy(){destroy(root);};
int Leaves (){return leaves(root);}
protected:
BinTreeNode *root;
T RefValue;
void CreateBinTree(BinTreeNode * &subTree);
bool Insert(BinTreeNode * &subTree,const T &x);////////
void destroy(BinTreeNode * &subTree);/////
bool Find(BinTreeNode * &subTree,const T &x);///////
BinTreeNode *Copy(BinTreeNode *orignode);///////
int Height(BinTreeNode * subTree);////////
int Size(BinTreeNode * subTree);///////
BinTreeNode *Parent(BinTreeNode *Parent,BinTreeNode *current);//////
BinTreeNode *Find(BinTreeNode * &subTree,const T &x)const;////////
void Traverse(BinTreeNode * &subTree,ostream &out);///////
void preOrder(BinTreeNode * &subTree);///////
void inOrder(BinTreeNode * &subTree);///////
void postOrder(BinTreeNode * &subTree);///////
void levelOrder(BinTreeNode *&subtree);
int leaves(BinTreeNode * &subTree);
};
#endif // !defined(AFX_BINARYTREE_H__61FEB349_65EE_40FB_82DF_25FFBCBDFC6E__INCLUDED_)
BinaryTree.cpp
// BinaryTree.cpp: implementation of the BinaryTree class.
//
//////////////////////////////////////////////////////////////////////
#include "BinaryTree.h"
#include<stdlib.h>
#include "queue.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void BinaryTree::CreateBinTree(BinTreeNode * &subTree) /////////建立二叉树
{
T item;
cin>>item;
if(item!=RefValue)
{
subTree=new BinTreeNode(item);
if(subTree==NULL)
{
cerr<<"存储分配错误!"<<endl;
exit(1);
}
CreateBinTree(subTree->leftChild);
CreateBinTree(subTree->rightChild);
}
else subTree=NULL;
}
void BinaryTree::destroy(BinTreeNode *& subTree) ////删除二叉树
{
if(subTree!=NULL)
{
destroy(subTree->leftChild);
destroy(subTree->rightChild);
delete subTree;
}
}
BinTreeNode* BinaryTree::Parent(BinTreeNode *subTree, BinTreeNode *current) //找父母结点
{
if(subTree==NULL) return NULL;
if(subTree->leftChild==current||subTree->rightChild==current)
return subTree;
BinTreeNode *p;
if((p=Parent(subTree->leftChild,current))!=NULL) return p;
else return Parent(subTree->rightChild,current);
}
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)
{
inOrder(subTree->leftChild);
cout<<subTree->data<<" ";
inOrder(subTree->rightChild);
}
}
void BinaryTree::postOrder( BinTreeNode *&subTree ) // 后序遍历
{
if(subTree!=NULL)
{
postOrder(subTree->leftChild);
postOrder(subTree->rightChild);
cout<<subTree->data<<" ";
}
}
void BinaryTree::levelOrder(BinTreeNode *&subtree) //层序遍历
{
if(root==NULL)return;
queue<BinTreeNode*> Q;
BinTreeNode *p=root;
Q.EnQueue(p);
while(!Q.Isempty())
{
Q.DeQueue(p);
cout<<p->data<<" ";
if(p->leftChild)
Q.EnQueue(p->leftChild);
if(p->rightChild)
Q.EnQueue(p->rightChild);
}
}
int BinaryTree::Size(BinTreeNode *subTree) //求结点个数
{
if(subTree==NULL) return 0;
else return 1+Size(subTree->leftChild)+Size(subTree->rightChild);
}
int BinaryTree::Height(BinTreeNode *subTree) //求数的深度
{
if(subTree==NULL) return 0;
else
{
int i=Height(subTree->leftChild);
int j=Height(subTree->rightChild);
return (i>j)?i+1:j+1;
}
}
int BinaryTree::leaves(BinTreeNode * &subTree) //求叶子节点个数
{
if(subTree==NULL) return 0;
else if(subTree->leftChild==NULL&&subTree->rightChild==NULL)return 1;
else
{
int i=leaves(subTree->leftChild);
int j=leaves(subTree->rightChild);
return i+j;
}
}
主函数:
#include "BinaryTree.h"
int main_menu()
{
char option;
while(1)
{
cout<<" 主菜单"<<endl;
cout<<endl;
cout<<" 请选择操作:"<<endl;
cout<<" 1 建立树:"<<endl;
cout<<" 2 清空树"<<endl;
cout<<" 3 退出"<<endl;
cin>>option;
switch(option)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
default:
cout<<"输入错误,请检查输入!"<<endl;
break;
}
}
}
int tree_menu(BinaryTree &btn)
{
char option;
while(1)
{
cout<<" 树的相关操作:"<<endl;
cout<<" 请选择操作:"<<endl;
//cout<<" 1 输入节点"<<endl;
cout<<" 2 按前序输出"<<endl;
cout<<" 3 按中序输出"<<endl;
cout<<" 4 按后序输出"<<endl;
cout<<" 5 按层序输出"<<endl;
cout<<" 6 求节点个数"<<endl;
cout<<" 7 求树的深度"<<endl;
cout<<" 8 求树的高度"<<endl;
cout<<" 9 返回主菜单"<<endl;
cin>>option;
switch(option)
{
// case '1':
// {
// cout<<"输入节点,-1为结束标志"<<endl;
// btn.CreateBinTree();
// break;
// }
case '2':
{
cout<<"按前序输出为:"<<endl;
btn.preOrder();
break;
}
case '3':
{
cout<<"按中序输出为:"<<endl;
btn.inOrder();
break;
}
case '4':
{
cout<<"按后序输出为:"<<endl;
btn.postOrder();
break;
}
case '5':
{
cout<<"按层序输出为:"<<endl;
btn.levelOrder();
break;
}
case '6':
{
cout<<"节点个数为:"<<btn.Size()<<endl;
break;
}
case '7':
{
cout<<"求树的深度为:"<<btn.Height()<<endl;
break;
}
case '8':
{
cout<<"求树的高度为:"<<btn.Height()<<endl;
break;
}
case '9':
return 9;
default:
cout<<"输入有误,请检查输入!"<<endl;
break;
}
}
}
int main()
{
bool bCreated=false;
int op;
BinaryTree btn(-1);
while(1)
{
op=main_menu();
switch(op)
{
case 1:
{
if(!bCreated)
{
cout<<"1 输入节点,-1为结束标志"<<endl;
btn.CreateBinTree();
}
bCreated=true;
tree_menu(btn);
break;
}
case 2:
{
btn.destroy();
bCreated=false;
break;
}
case 3:
return 0;
}
}
return 0;
}
把书上的直接写到C++程序软件里?那↑怎么输入啊?完全不会!
你能不能给个完整的C++程序让我复制粘贴到软件里就能用啊?
你问->怎么输入吗,↑是什么符号,
代码
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;//左右孩子指针
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T) {
// 按先序次序输入二叉树中结点的值(一个字符),’#’字符表示空树,
// 构造二叉链表表示的二叉树T。
char ch;
scanf("%c",&ch);
if (ch=='#') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
T->data=ch; // 生成根结点
CreateBiTree(T->lchild); // 构造左子树
CreateBiTree(T->rchild); // 构造右子树
}
return OK;
} // CreateBiTree
Status PrintElement( ElemType e ) { // 输出元素e的值
printf("%c", e );
return OK;
}// PrintElement
Status PreOrderTraverse( BiTree T, Status(*Visit)(ElemType) ) {
if(T){
if (Visit(T->data))
if (PreOrderTraverse(T->lchild,Visit))
if (PreOrderTraverse(T->rchild,Visit)) return OK;
return ERROR;
}
else return OK;
} // PreOrderTraverse
Status InOrderTraverse( BiTree T, Status(*Visit)(ElemType) ) {
if (T){
if (InOrderTraverse(T->lchild,Visit))
if (Visit(T->data))
if (InOrderTraverse(T->rchild,Visit)) return OK;
return ERROR;
}
else return OK;
} // InOrderTraverse
Status PostOrderTraverse( BiTree T, Status(*Visit)(ElemType) ) {
if (T){
if (PostOrderTraverse(T->lchild,Visit))
if (PostOrderTraverse(T->rchild,Visit))
if (Visit(T->data)) return OK;
return ERROR;
}else return OK;
} // PostOrderTraverse
int main() //主函数
{
BiTree T;
CreateBiTree(T);
PreOrderTraverse(T,PrintElement);
printf("\n");
InOrderTraverse(T,PrintElement);
printf("\n");
PostOrderTraverse(T,PrintElement);
这明显是C语言麼……C++怎么写啊……我都快疯了,都看不懂,啥也不会……
我就想问出一套语句,我把它复制粘贴到程序软件里,然后交上去,就结束。
现在看来,我把你的这个复制粘贴一下,用我们用的C++的软件都不能Build,不知道为什么
还有,老师说是要做出一个程序……程序到底是什么?是一个.exe的文件?
要疯了要疯了……
C和C++对于你要实现的东西来说差别不大,程序就是编译出来一个exe,你们老师是让你们出一个黑屏类似dos界面的exe程序。光图省事,老师是让你们好好学习
这样给你一个网址http://edu.codepub.com/2009/0805/12310.php,把这里面的代码粘贴到C++里面,注意分成三个文件,.h .cpp 还有个包含main函数的文件。