第1个回答 2009-05-23
#include <iostream>
using namespace std;
#include <iomanip>
typedef int T;
class bst{
struct Node{
T data;
Node* L;
Node* R;
Node(T const& d):data(d),L(NULL),R(NULL){}
};
Node* root;
int num;
void clear(Node*& t){
if(t==NULL) return;
clear(t->L);
clear(t->R);
delete t;
t = NULL;
}
void midtravel(Node* t){
if(t==NULL) return;
travel(t->L);
cout << t->data << ' ';
travel(t->R);
}
void fronttravel(Node* t){
if(t==NULL) return;
cout << t->data << ' ';
travel(t->L);
travel(t->R);
}
void backtravel(Node* t){
if(t==NULL) return;
travel(t->L);
travel(t->R);
cout << t->data << ' ';
}
void insert(Node*& t, Node* p){
if(t==NULL) t=p;
else if(p->data < t->data) insert(t->L, p);
else insert(t->R, p);
}
void printTree(Node* t, int space, char c){
if(t==NULL) return;
printTree(t->R, space+4, '/');
cout << setw(space) << c << t->data << endl;
printTree(t->L, space+4, '\\');
}
Node*& find(Node*& t, T const& d){
if(t==NULL) return t;
if(t->data==d) return t;
if(d<t->data) return find(t->L, d);
return find(t->R, d);
}
int getHeight (Node* p)
{
int Llevel, Rlevel;
if (NULL == p)
return 0;
Llevel = getHeight (p->L);
Rlevel = getHeight (p->R);
return (Llevel>Rlevel)?(Llevel+1):(Rlevel+1);
}
public:
bst():root(NULL),num(0){}
~bst(){clear();}
void clear(){clear(root);num=0;}
void Midtravel(){travel(root);cout<<endl;}
bst& insert(T const& d){
Node* p = new Node(d);
num++;
insert(root, p);
return *this;
}
void Fronttravel(){travel(root);cout<<endl;}
bst& insert(T const& d){
Node* p = new Node(d);
num++;
insert(root, p);
return *this;
}
void Backtravel(){travel(root);cout<<endl;}
bst& insert(T const& d){
Node* p = new Node(d);
num++;
insert(root, p);
return *this;
}
bool find(T const& d){
return (find(root, d)!=NULL);
}
int count(T const& d){
int cnt=0;
Node* p=root;
while( (p=find(p, d))!=NULL ){
cnt++;
p = p->R;
}
return cnt;
}
bool remove(T const& d){
Node*& pn = find(root, d);
if(pn==NULL) return false;
Node* p = pn;
if(pn->L!=NULL) insert(pn->R, pn->L);
pn = pn->R;
delete p;
num--;
return true;
}
int removeall(T const& d){
int cnt=0;
while(remove(d))
cnt++;
return cnt;
//
return 1 + remove(d);
}
void printTree(){
printTree(root, 1, '*');
}
int update(T const& olddata, T const& newdata){
int cnt = remove(olddata);
for(int i=0; i<cnt; i++)
insert(newdata);
return cnt;
}
int size(){return num;}
bool empty(){return root==NULL;}
T const& getRootData()throw(char const*){
if(empty()) throw "empty tree!";
return root->data;
}
int getHeight()
{
return getHeight(root);
}
};
类给你了
实现自己看看吧 Midtravel中序遍历 Fronttravel前序 Backtravel后续注意大写 小写的是给类内部用的
第3个回答 推荐于2016-04-05
#include<stdio.h>
#include<stdlib.h>
typedef struct BNode
{
char data;
struct BNode *lchild;
struct BNode *rchild;
}BTNode;
typedef BTNode *BinTree;
void CreateBinTree(BinTree *root)//以先序来建立二叉树
{
char ch;
if((ch=getchar())==' ')//这个代表空格,可换别的字符
*root=NULL; //建立空二叉树
else
{
*root=(BTNode*)malloc(sizeof(BTNode));//开辟空间,生成节点
(*root)->data=ch;
CreateBinTree(&((*root)->lchild));
CreateBinTree(&((*root)->rchild));
}
}
void PreOrder(BinTree root)//先序遍历
{
if(root!=NULL)
{
printf("%c",root->data);//访问根节点
PreOrder(root->lchild);//遍历左子树
PreOrder(root->rchild);//遍历右子树
}
}
void InOrder(BinTree root)//中序遍历
{
if(root!=NULL)
{
PreOrder(root->lchild); //遍历左子树
printf("%c",root->data);//访问根节点
PreOrder(root->rchild);//遍历右子树
}
}
void PostOrder(BinTree root)//后序遍历
{
if(root!=NULL)
{
PreOrder(root->lchild);//遍历左子树
PreOrder(root->rchild);//遍历右子树
} printf("%c",root->data);//访问根节点
}
void main()
{
BinTree root;
CreateBinTree(&root);
printf("先序:");
PreOrder(root);
printf("\n");
printf("中序:");
InOrder(root);
printf("\n");
printf("后序:");
PostOrder(root);
printf("\n");
}本回答被提问者采纳