void Inorder(struct Tree *T); //中序
void Preorder(struct Tree *T); //前序
void Postorder(struct Tree *T); //后序
struct Tree * InsertTree(struct Tree * T, int z);
struct Tree * Delete(struct Tree *T, int z);
struct Tree * FindMin(struct Tree *T);
struct Tree * FindMax(struct Tree *T);
DataType Maximum(struct Tree *T);
DataType Minimum(struct Tree *T);
bool Search(struct Tree * T, int z);
就这些功能
#include <iostream> #include <cstdlib> using namespace std; typedef int DataType; struct Tree { //struct Tree *Parent; struct Tree *Left; struct Tree *Right; DataType key; }; void Inorder(struct Tree *T); //中序 void Preorder(struct Tree *T); //前序 void Postorder(struct Tree *T); //后序 struct Tree * InsertTree(struct Tree * T, int z); struct Tree * Delete(struct Tree *T, int z); struct Tree * FindMin(struct Tree *T); struct Tree * FindMax(struct Tree *T); DataType Maximum(struct Tree *T); DataType Minimum(struct Tree *T); bool Search(struct Tree * T, int z); struct Tree *s; int main() { s=InsertTree(s, 12); s=InsertTree(s, 2); s=InsertTree(s, 5); s=InsertTree(s, 13); s=InsertTree(s, 17); s=InsertTree(s, 18); s=InsertTree(s, 15); s=InsertTree(s, 19); cout << "It's the result of Preorder tree walk.\n"; Preorder(s); cout << "It's the result of inorder tree walk.\n"; Inorder(s); cout << "It's the result of Postorder tree walk.\n"; Postorder(s); cout << "The maximum element is "; cout << Maximum(s) << endl; cout << "The minimum element is "; cout << Minimum(s) << endl; Delete(s, 17); cout << "It's the result of Preorder tree walk.\n"; Preorder(s); cout << "It's the result of inorder tree walk.\n"; Inorder(s); cout << "It's the result of Postorder tree walk.\n"; Postorder(s); return 0; } bool Search(struct Tree * T, int z) { if(z<T->key) Search(T->Left, z); else if(z>T->key) Search(T->Right, z); else if(z==T->key) return true; else return false; } struct Tree * InsertTree(struct Tree * T, int z) { if(T==NULL) { T=(struct Tree *)malloc(sizeof(struct Tree*)); T->key=z; T->Left=T->Right=NULL; } if(z<T->key) T->Left=InsertTree(T->Left, z); else if(z>T->key) T->Right=InsertTree(T->Right, z); return T; } void Inorder(struct Tree *T) { /*if(T!=NULL) { if(T->Left) Inorder(T->Left); cout << T->key << endl; if(T->Right) Inorder(T->Right); }*/ if(T) { Inorder(T->Left); cout << T->key << endl; Inorder(T->Right); } } void Preorder(struct Tree *T) { if(T!=NULL) { cout << T->key << endl; if(T->Left) Preorder(T->Left); if(T->Right) Preorder(T->Right); } } void Postorder(struct Tree *T) { if(T!=NULL) { if(T->Left) Postorder(T->Left); if(T->Right) Postorder(T->Right); cout << T->key << endl; } } DataType Minimum(struct Tree *T) { while(T->Left) T=T->Left; return T->key; } DataType Maximum(struct Tree *T) { while(T->Right) T=T->Right; return T->key; } struct Tree * Delete(struct Tree *T, int z) { struct Tree *Tmp; if(T==NULL) cout << "We don't have enough node to delete!\n"; else if(z<T->key) T->Left=Delete(T->Left, z); else if(z>T->key) T->Right=Delete(T->Right, z); else if(T->Left&&T->Right) { Tmp=FindMin(T->Right); T->key=Tmp->key; T->Right=Delete(T->Right, T->key); } else { Tmp=T; if(T->Left==NULL) T=T->Right; else if(T->Right==NULL) T=T->Left; free(Tmp); } return T; } struct Tree * FindMin(struct Tree *T) { while(T->Left) T=T->Left; return T; } struct Tree * FindMax(struct Tree *T) { while(T->Right) T=T->Right; return T; }