二叉排序樹

//以下是頭文件BST.h
void Inorder(BTreeNode* BT)//中序遍歷
 {
     if(BT!=NULL){
         Inorder(BT->left);
         cout
<<'('<<BT->data.key<<','<<BT->data.rest<<')'<<',';
         Inorder
(BT->right);
     }
 }

void PrintBTree(BTreeNode* BT)
 {
     if(BT!=NULL)
     {
         cout
<<BT->data.key;
         if(BT->left!=NULL||BT->right!=NULL)
         {
             cout
<<'(';
             PrintBTree
(BT->left);
             if(BT->right!=NULL)
             cout
<<',';
             PrintBTree
(BT->right);
             cout
<<')';
         }
     }
 }


int 
Find(BTreeNode* BST,ElemType& item)
{
    if(BST
==NULL)
        
return 0;
        else{
            if(item.key
==BST->data.key)
            {
                item=BST->data;
                return 1;
            }
            else if(item.key
<BST->data.key)
                return Find(BST->left,item);
            else
                return Find(BST->right,item);
        }
}
//由於此遞歸算法中的遞歸調用屬於末尾遞歸,
//所以爲了避免無效開銷在進出系統棧操作上的時
//間和使用系統棧的空間,可以把它改寫成非遞歸算法如下:
int Find2(BTreeNode* BST,ElemType& item)
{
    while(BST!=NULL)
    {
        if(item.key==BST->data.key){
            item=BST->data;
            return 1;
        }
            else if(item.key
<BST->data.key)
                BST=BST->left;
            else
                BST=BST->right;
    }    
        return 0;
}

int Update(BTreeNode* BST,const ElemType& item)//這是我自己寫的,書上省略了
{
    while(BST!=NULL)
    {
        if(item.key==BST->data.key){
            BST->data=item;
            return 1;
        }
            else if(item.key
<BST->data.key)
                BST=BST->left;
            else
                BST=BST->right;
    }    
        return 0;
}

void Insert(BTreeNode*
&BST,const ElemType& item)
{
    if(BST==NULL)
    {
        BTreeNode* p=new BTreeNode;
        p->data=item;
        p->left=p->right=NULL;
        BST=p;
    }
    else if(item.key
<BST->data.key)
        Insert(BST->left,item);
    else
        Insert(BST->right,item);
}
//此算法也屬於末尾遞歸調用,改爲非遞歸算法如下:
void Insert2(BTreeNode*
&BST,const ElemType& item)
{
    BTreeNode* t=BST,*parent=NULL;
    while(t!=NULL){
        parent=t;
        if(item.key
<t->data.key)
            t=t->left;
        else
            t=t->right;
    }
    BTreeNode* p=new BTreeNode;
    p->data=item;
    p->left=p->right=NULL;
    if(parent==NULL)
        BST=p;
    else if(item.key
<parent->data.key)
        parent->left=p;
    else
        parent->right=p;
}
void CreateBSTree(BTreeNode*
&BST,ElemType a[],int n)//利用數組中的元素建立二叉排序樹
{
    BST=NULL;
    for(int i=0;i
<n;i++)
    Insert(BST,a[i]);
}
int Delete(BTreeNode*& BST,const ElemType& item)
{
    BTreeNode* t
=BST,*s=NULL;    
    
while(t!=NULL)
    
{
        if(item.key
==t->data.key)
            break;
        else if(item.key
<t->data.key){
            s=t;
            t=t->left;
        }
        else{
            s=t;
            t=t->right;
        }
    }
    if(t==NULL)return 0;
    if(t->left==NULL&
&t->right==NULL)
    {
        if(t==BST)
            BST=NULL;
        else if(t==s->left)
            s->left=NULL;
        else
            s->right=NULL;
        delete t;
    }
    else if(t->left==NULL||t->right==NULL)
    {
        if(t==BST){
            if(t->left==NULL)
                BST=t->right;
            else
                BST=t->left;
    }
    else{
        if(t==s->left&
&t->left!=NULL)
            s->left=t->left;
        else if(t==s->left&
&t->right!=NULL)
            s->left=t->right;
        else if(t==s->right&
&t->left!=NULL)
            s->right=t->left;
        else if(t==s->right&
&t->right!=NULL)
            s->right=t->right;
    }
    delete t;
    }
    else if(t->left!=NULL&
&t->right!=NULL)
    {
        BTreeNode* p=t,*q=t->left;
        while(q->right!=NULL){
            p=q;
            q=q->right;
        }
        t->data=q->data;
        if(p==t)
            t->left=q->left;
        else
            p->right=q->left;
            delete q;
    }
    return 1;
}
//以下是源文件BST.cpp
#include
<iostream.h>
#include
<stdlib.h>
#include
<conio.h>

struct student{
    int key;
    int rest;
};
typedef student ElemType;//不能放在結構體student前面
struct BTreeNode{
    ElemType data;
    BTreeNode* left;
    BTreeNode* right;
};

#include"BST.h"
void main()
{
    ElemType a[8]={{30,50},{20,70},{25,80},{23,40},{28,50},{15,90},{60,12},{48,60}};
    BTreeNode* bst=NULL;
    ElemType x={28};
    ElemType y={20,37};
    CreateBSTree(bst,a,8);
    PrintBTree(bst);cout
<<endl;
    cout<<"中序遍歷:"<<endl;
    Inorder(bst);
    cout<<endl;
    if(Find(bst,x))
        cout<<"查找成功!得到x爲:("<<x.key<<","<<x.rest<<')'<<endl;//注間此行的x.rest
    if(Update(bst,y)){
        cout<<"更新成功:"<<endl;
        Inorder(bst);
    }
    cout<<endl;
    Delete(bst,x);
    Delete(bst,y);
    cout<<"刪除關鍵字爲28和20的元素後的中序遍歷爲:"<<endl;
    Inorder(bst);
    cout<<endl;
    getch();
}
 
發佈了39 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章