天天看點

二叉搜尋樹(BST樹)的簡單實作

二叉搜尋樹(BST樹)的簡單實作

#include <stdlib.h>

二叉搜尋樹(BST樹)的簡單實作

template<typename T>

二叉搜尋樹(BST樹)的簡單實作

class CBinSTree;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

template <typename T>

二叉搜尋樹(BST樹)的簡單實作

class CTreeNode

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

{//樹節點類

二叉搜尋樹(BST樹)的簡單實作

public:

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode(const T& item,CTreeNode<T>* lptr = NULL,CTreeNode<T>* rptr = NULL):data(item),left(lptr),right(rptr)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

{

二叉搜尋樹(BST樹)的簡單實作

    }

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* Left(void)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        return left;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* Right(void)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        return right;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    friend class CBinSTree<T>;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    T data;//資料

二叉搜尋樹(BST樹)的簡單實作

private:

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* left;//左子樹

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* right;//右子樹

二叉搜尋樹(BST樹)的簡單實作

};

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

class CBinSTree  

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

{//二叉搜尋樹類

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CBinSTree();

二叉搜尋樹(BST樹)的簡單實作

    virtual ~CBinSTree();

二叉搜尋樹(BST樹)的簡單實作

    CBinSTree(const CBinSTree<T>& tree);

二叉搜尋樹(BST樹)的簡單實作

    CBinSTree<T>& operator = (const CBinSTree<T>& rhs);

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* FindNode(const T& item,CTreeNode<T>* &parent)const;//尋找節點

二叉搜尋樹(BST樹)的簡單實作

    void PrintTree();//前序周遊樹(非遞歸)

二叉搜尋樹(BST樹)的簡單實作

    void ClearTree();//清空樹

二叉搜尋樹(BST樹)的簡單實作

    void Insert(const T& item);//插入資料

二叉搜尋樹(BST樹)的簡單實作

    void Delete(const T& item);//删除資料

二叉搜尋樹(BST樹)的簡單實作

    bool Contains(const T& item);//是否包含資料

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* FindMin()const;//找最小值

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* FindMax()const;//找最大值

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

protected:

二叉搜尋樹(BST樹)的簡單實作

    //輔助函數區

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* GetTreeNode(const T& item,CTreeNode<T>* lptr=NULL,CTreeNode<T>* rptr=NULL);//配置設定樹節點

二叉搜尋樹(BST樹)的簡單實作

    void FreeTreeNode(CTreeNode<T>* p);//釋放樹節點

二叉搜尋樹(BST樹)的簡單實作

    void DeleteTree(CTreeNode<T>* t);//删除樹

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* CopyTree(CTreeNode<T>* t);//拷貝樹

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T> *root;//二叉搜尋樹樹根

二叉搜尋樹(BST樹)的簡單實作

    int size;//樹節點個數

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

#include "stdafx.h"

二叉搜尋樹(BST樹)的簡單實作

#include "BinSTree.h"

二叉搜尋樹(BST樹)的簡單實作

#include <iostream>

二叉搜尋樹(BST樹)的簡單實作

#include <stack>

二叉搜尋樹(BST樹)的簡單實作

using namespace std;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

/**///////////////////////////////////////////////////////////////////////

二叉搜尋樹(BST樹)的簡單實作

// Construction/Destruction

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CBinSTree<T>::CBinSTree()

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    this->root = NULL;

二叉搜尋樹(BST樹)的簡單實作

    this->size = 0;

二叉搜尋樹(BST樹)的簡單實作

}

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CBinSTree<T>::CBinSTree(const CBinSTree<T>& tree)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    root = this->CopyTree(tree.root);

二叉搜尋樹(BST樹)的簡單實作

    this->size = tree.size;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CBinSTree<T>::~CBinSTree()

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    this->ClearTree();

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CBinSTree<T>& CBinSTree<T>::operator = (const CBinSTree<T>& rhs)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    if(this==&rhs)

二叉搜尋樹(BST樹)的簡單實作

        return *this;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    root = this->CopyTree(rhs.root);

二叉搜尋樹(BST樹)的簡單實作

    size = rhs.size;

二叉搜尋樹(BST樹)的簡單實作

    return *this;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CTreeNode<T>* CBinSTree<T>::GetTreeNode(const T& item,CTreeNode<T>* lptr,CTreeNode<T>* rptr)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* p;

二叉搜尋樹(BST樹)的簡單實作

    p = new CTreeNode<T>(item,lptr,rptr);

二叉搜尋樹(BST樹)的簡單實作

    if(p==NULL)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        cerr<<"配置設定記憶體失敗!"<<endl;

二叉搜尋樹(BST樹)的簡單實作

        exit(1);

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    return p;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CTreeNode<T>* CBinSTree<T>::FindMin()const

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T> *t = root;

二叉搜尋樹(BST樹)的簡單實作

    while(t->left!=NULL)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        t = t->left;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    return t;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CTreeNode<T>* CBinSTree<T>::FindMax()const

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    while(t->right!=NULL)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        t = t->right;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

bool CBinSTree<T>::Contains(const T& item)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T> *p;

二叉搜尋樹(BST樹)的簡單實作

    return (this->FindNode(item,p)!=NULL);

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CTreeNode<T>* CBinSTree<T>::CopyTree(CTreeNode<T>* t)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T> *newnode,*newlptr,*newrptr;

二叉搜尋樹(BST樹)的簡單實作

    if(t==NULL)

二叉搜尋樹(BST樹)的簡單實作

        return NULL;

二叉搜尋樹(BST樹)的簡單實作

    if(t->Left()!=NULL)

二叉搜尋樹(BST樹)的簡單實作

        newlptr = CopyTree(t->Left());

二叉搜尋樹(BST樹)的簡單實作

    else

二叉搜尋樹(BST樹)的簡單實作

        newlptr = NULL;

二叉搜尋樹(BST樹)的簡單實作

    if(t->Right()!=NULL)

二叉搜尋樹(BST樹)的簡單實作

        newrptr = CopyTree(t->Right());

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        newrptr = NULL;

二叉搜尋樹(BST樹)的簡單實作

    newnode = GetTreeNode(t->data,newlptr,newrptr);

二叉搜尋樹(BST樹)的簡單實作

    return newnode;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

void CBinSTree<T>::FreeTreeNode(CTreeNode<T>* p)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    delete p;

二叉搜尋樹(BST樹)的簡單實作

    p = NULL;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

void CBinSTree<T>::DeleteTree(CTreeNode<T>* t)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    if(t!=NULL)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        DeleteTree(t->Left());

二叉搜尋樹(BST樹)的簡單實作

        DeleteTree(t->Right());

二叉搜尋樹(BST樹)的簡單實作

        FreeTreeNode(t);

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

void CBinSTree<T>::ClearTree()

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    DeleteTree(root);

二叉搜尋樹(BST樹)的簡單實作

    root = NULL;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CTreeNode<T>* CBinSTree<T>::FindNode(const T& item,CTreeNode<T>* &parent)const

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    parent = NULL;

二叉搜尋樹(BST樹)的簡單實作

    while(t!=NULL)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        if(item==t->data)

二叉搜尋樹(BST樹)的簡單實作

            break;

二叉搜尋樹(BST樹)的簡單實作

        else

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            parent = t;

二叉搜尋樹(BST樹)的簡單實作

            if(item<t->data)

二叉搜尋樹(BST樹)的簡單實作

                t = t->Left();

二叉搜尋樹(BST樹)的簡單實作

            else 

二叉搜尋樹(BST樹)的簡單實作

                t = t->Right();

二叉搜尋樹(BST樹)的簡單實作

        }

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

void CBinSTree<T>::Insert(const T& item)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* t = root,*parent = NULL,*newnode;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        parent = t;

二叉搜尋樹(BST樹)的簡單實作

        if(item<t->data)

二叉搜尋樹(BST樹)的簡單實作

            t = t->Left();

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            t = t->Right();

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    newnode = this->GetTreeNode(item);

二叉搜尋樹(BST樹)的簡單實作

    if(parent==NULL)

二叉搜尋樹(BST樹)的簡單實作

        root = newnode;

二叉搜尋樹(BST樹)的簡單實作

    else if(item<parent->data)

二叉搜尋樹(BST樹)的簡單實作

        parent->left = newnode;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        parent->right = newnode;

二叉搜尋樹(BST樹)的簡單實作

    size++;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

void CBinSTree<T>::Delete(const T& item)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T> *pDNode,*pRNode,*pParNode;

二叉搜尋樹(BST樹)的簡單實作

    if((pDNode = this->FindNode(item,pParNode))==NULL)

二叉搜尋樹(BST樹)的簡單實作

        return;

二叉搜尋樹(BST樹)的簡單實作

    if(pDNode->left==NULL)

二叉搜尋樹(BST樹)的簡單實作

        pRNode = pDNode->right;

二叉搜尋樹(BST樹)的簡單實作

    else if(pDNode->right==NULL)

二叉搜尋樹(BST樹)的簡單實作

        pRNode = pDNode->left;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        CTreeNode<T> *pParOfRNode = pDNode;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        while(pRNode->right!=NULL)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            pParOfRNode = pRNode;

二叉搜尋樹(BST樹)的簡單實作

            pRNode = pRNode->right;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        if(pParOfRNode==pDNode)

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            pRNode->right = pDNode->right;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            pParOfRNode->right = pRNode->left;

二叉搜尋樹(BST樹)的簡單實作

            pRNode->left = pDNode->left;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    if(pParNode==NULL)

二叉搜尋樹(BST樹)的簡單實作

        root = pRNode;

二叉搜尋樹(BST樹)的簡單實作

    else if(pDNode->data<pParNode->data)

二叉搜尋樹(BST樹)的簡單實作

        pParNode->left = pRNode;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        pParNode->right = pRNode;

二叉搜尋樹(BST樹)的簡單實作

    this->FreeTreeNode(pDNode);

二叉搜尋樹(BST樹)的簡單實作

    this->size--;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

void CBinSTree<T>::PrintTree()

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    stack<CTreeNode<T>* > s;

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<T>* p = root;

二叉搜尋樹(BST樹)的簡單實作

    while (p!=NULL || !s.empty())

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        while (p!=NULL)             //周遊左子樹

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            cout<<p->data<<endl;

二叉搜尋樹(BST樹)的簡單實作

            s.push(p);

二叉搜尋樹(BST樹)的簡單實作

            p=p->Left();

二叉搜尋樹(BST樹)的簡單實作

        }//endwhile

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

        if (!s.empty())

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

            p=s.top();

二叉搜尋樹(BST樹)的簡單實作

            s.pop();

二叉搜尋樹(BST樹)的簡單實作

            p=p->Right();            //通過下一次循環實作右子樹周遊

二叉搜尋樹(BST樹)的簡單實作

        }//endif   

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

// test.cpp : Defines the entry point for the console application.

二叉搜尋樹(BST樹)的簡單實作

//

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

#include "BinSTree.cpp"

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

CBinSTree<int>* MakeSampleTree()

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

{//示例BST樹

二叉搜尋樹(BST樹)的簡單實作

    CBinSTree<int> *tree1 = new CBinSTree<int>();

二叉搜尋樹(BST樹)的簡單實作

    int a = 5;

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(a);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(30);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(65);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(25);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(35);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(50);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(10);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(28);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(26);

二叉搜尋樹(BST樹)的簡單實作

    tree1->Insert(33);

二叉搜尋樹(BST樹)的簡單實作

    return tree1;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

int main(int argc, char* argv[])

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    CBinSTree<int> *tree1 = MakeSampleTree();

二叉搜尋樹(BST樹)的簡單實作

    tree1->PrintTree();

二叉搜尋樹(BST樹)的簡單實作

    std::cout<<"删除節點30:"<<endl;

二叉搜尋樹(BST樹)的簡單實作

    tree1->Delete(30);

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作

    cout<<tree1->Contains(40)<<endl;

二叉搜尋樹(BST樹)的簡單實作

    CTreeNode<int> *p = tree1->FindMin();

二叉搜尋樹(BST樹)的簡單實作

    cout<<p->data<<endl;

二叉搜尋樹(BST樹)的簡單實作

    return 0;

二叉搜尋樹(BST樹)的簡單實作
二叉搜尋樹(BST樹)的簡單實作