二叉搜索树

#include<iostream>

using namespace std;

class binaryNode{
    public:
        binaryNode(int &a):key(a),p(NULL),Left(NULL),Right(NULL){ }
        binaryNode* p;
        binaryNode* Left;
        binaryNode* Right;
        int key;
};

//二叉树插入 
void tree_insert(binaryNode** T, int t)
{
    binaryNode* p = new binaryNode(t);
//    cout << p->key;
    //return;
    binaryNode* root = *T;
    if(root == NULL)
    {
        *T = p;
        p->p = NULL;
    }
    else
    {
        binaryNode* temp = root;
        binaryNode* y;
        while(temp != NULL)
        {
            y = temp;
            if(temp->key < p->key)
            {    
                temp = temp->Right;
            }
            else
            {
                temp = temp->Left;
            }
        }
        
        if(y->key < p->key)
        {
            y->Right = p;
            p->p = y;
        }    
        else
        {
            y->Left = p;
            p->p = y;
        }
    }
}


//二叉树中序遍历 
void tree_inorde(binaryNode** T)
{
    binaryNode* root = *T;
    if(root == NULL)
    {
        return;
    }
    else
    {
        if(root->Left != NULL)
            tree_inorde(&root->Left);
        cout << root->key << " ";
        if(root->Right != NULL)
            tree_inorde(&root->Right);
    }
}

//二叉树最大值

binaryNode* tree_max(binaryNode **T)
{
    binaryNode* root = *T;
    binaryNode* y;
    while(root != NULL)
    {
        y = root;
        root = root->Right;
    }
    
    return y;
}
//二叉树最小值 
binaryNode* tree_min(binaryNode **T)
{
    binaryNode* root = *T;
    binaryNode* y;
    while(root != NULL)
    {
        y = root;
        root = root->Left;
    }
    
    return y;
}


//前驱 
binaryNode* tree_pre(binaryNode **T)
{
    binaryNode* root = *T;
    if(root->Left != NULL)
    {
        return tree_max(&root->Left);
    }
    else
    {
        cout << "jianlai";
        if(root->p->Left == root)
        {
            cout << "最小点,无前驱" << endl;
            return root;
        }
        binaryNode *y = root->p;
    
        while(y->Left == root && y != NULL)
        {
            cout << "YYY " << root->key << " YYY ";
            root = y;
            y = root->p;
        }
        return y;
    }
}

//后继 


binaryNode* tree_successor(binaryNode **T)
{
    binaryNode* root = *T;
    if(root->Right != NULL)
    {
        return tree_max(&root->Right);
    }
    else
    {
        if(root->p->Right == root)
        {
            cout << "最大点,无后继" << endl;
            return root;
        }
        while(root->p->Right != root && root->p != NULL)
        {
            root = root->p;
        }
        return root->p;
    }
}

//查找
binaryNode* tree_search(binaryNode** T, int k)
{
    binaryNode* root = *T;
    if(root == NULL)
    {
        cout << "没有找到" << endl;
    }
    else
    {
        if(root->key == k)
        {
            return root;
        }
        else if(root->key < k)
        {
            return tree_search(&root->Right,k);
        }
        else
        {
            return tree_search(&root->Left,k);
        }
    }
} 


//用一棵子树替换另一课子树,为删除节点服务
void transplant(binaryNode** T,binaryNode* u, binaryNode* v)
{
    binaryNode* root = *T;
    if(root->p == NULL)
    {
        T = &v;
    }
    else
    {
        if(u->p->Left == u)
        {
            u->p->Left = v;
        }
        else
        {
            u->p->Right = v;
        }
        
        if(v != NULL)
        {
            v->p = u->p;
        }
    }
} 

//删除
void tree_delete(binaryNode** T, binaryNode* z)
{
    if(z->Left == NULL)
    {
         transplant(T,z,z->Left);
    }
    else if(z->Right == NULL)
    {
         transplant(T,z,z->Right);
    }
    else
    {
        binaryNode* y = tree_min(&z->Right);
        //右子树不是y 
        if(z->Right != y)
        {
            transplant(T,y,y->Right);
            y->Right = z->Right;
            y->Right->p = y;
        }    
        transplant(T,z,y);
        y->Left = z->Left;
        y->Left->p = y;
    }
} 
int main()
{
    //return 0;
    binaryNode **T;
    binaryNode *root = NULL;
    T = &root; // 此处必须这样写,不能写作 *T = NULL;因为T根本好还没分配地址呢 
    //return 0;
    int a[10] = {12,456,22,355,22,11,55,666,22,33};
    for(int i= 0;i!=10;++i )
    {
        tree_insert(T,a[i]);
    }
    
    tree_inorde(T);
    cout << (*T)->key << endl;
    cout << (*T)->Left->key << endl;
    cout << (*T)->Right->key << endl;
    
    cout << endl;
    cout << tree_max(T)->key << endl;
    
    cout << endl;
    binaryNode* p = tree_search(T,666);
    cout << tree_pre(&p)->key;
    cout << p->key << " " << 333 << endl;
    tree_search(T,1000); 
    return 0;
}

 

posted on 2014-04-25 10:01  yi_meng  阅读(225)  评论(0编辑  收藏  举报

导航