树-二叉搜索树

二叉搜索树

#include<iostream>
using namespace std;

template<class type>
class Node{
    type data;
    Node<type>* leftChild,rightChild,parent;
    Node(type key):data(key){}
};
template<class type>
class BinaryTree{
public:
    Node<type>* root;

    BinaryTree():root(NULL){};
    bool isEmpty();
    BSTree();
    void preOrder();
    void inOrder();
    void postOrder();
    Node<type>* search(type key);
    Node<type>* iterativeSearch(type key);
    type minNode();
    type maxNode();
    Node<type>* successor(Node<type>* x);
    Node<type>* predecessor(Node<type>* x);
    void insert(type key);
    bool remove(type key);

    void preOrder(Node<type>* n) const;
    void inOrder(Node<type>* n) const;
    void postOrder(Node<type>* n) const;
    Node<type>* search(Node<type>* root,type key);
    Node<type>* iterativeSearch(Node<type>* r,type key);
    /*
    // 删除结点(key为节点键值)
    void remove(T key);
    // 打印二叉树
    void print();
    */

};

template<class type>
bool BinaryTree<type>::isEmpty(){
    if(root==NULL){
        return true;
    }else{
        return false;
    }
}
template<class type>
void BinaryTree<type>::preOrder(){
    preOrder(root);
}
template<class type>
void BinaryTree<type>::preOrder(Node<type>* a)const{
    if(a!=NULL){
        cout<<a->data<<" ";
        preOrder(a->leftChild);
        preOrder(a->rightChild);
    }
}
template<class type>
void BinaryTree<type>::inOrder(){
    inOrder(root);
}
template<class type>
void BinaryTree<type>::inOrder(Node<type>* root)const{
    if(root!=NULL){
        inOrder(root->leftChild);
        cout<<root->data<<" ";
        inOrder(root->rightChild);
    }
}
template<class type>
void BinaryTree<type>::postOrder(){
    postOrder(root);
}
template<class type>
void BinaryTree<type>::postOrder(Node<type>* root)const{
    if(root!=NULL){
        postOrder(root->leftChild);
        cout<<root->data<<" ";
        postOrder(root->rightChild);
    }
}
template<class type>
Node<type>* BinaryTree<type>::search(type key){
    search(root,key);
}
template<class type>
Node<type>* BinaryTree<type>::search(Node<type>* root,type key){
    if(root==NULL||root->data==key){
        return root;
    }
    if(root->data>key){
        return search(root->leftChild,key);
    }else{
        return search(root->rightChild,key);
    }
}
template<class type>
Node<type>* BinaryTree<type>::iterativeSearch(type key){
    return iterativeSearch(root,key);
}
template<class type>
Node<type>* BinaryTree<type>::iterativeSearch(Node<type>* t,type key){
    if(t==NULL||t->data==key){
        return t;
    }
    while((t->data!=key)&&(t!=NULL)){
        if(t->data>key){
            t = t->leftChild;
        }else{
            t = t->rightChild;
        }
    }
    return t;
}
template<class type>
type BinaryTree<type>::minNode(){
    if(root==NULL){
        return NULL;
    }
    while(root->leftChild){
        root = root->leftChild;
    }
    return root->data;
}
template<class type>
type BinaryTree<type>::maxNode(){
    if(root==NULL){
        return NULL;
    }
    while(root->rightChild){
        root = root->rightChild;
    }
    return root->data;
}
template<class type>
Node<type>* BinaryTree<type>::successor(Node<type>* x){
    if(x->rightChild){
        return x->rightChild;
    }
    Node<type>* y = x->parent;
    while((y!=NULL)&&(x!=y->rightChild)){
        x = y;
        y = x->parent;
    }
    return y;
}
template<class type>
Node<type>* BinaryTree<type>::predecessor(Node<type>* x){
    if(x->leftChild){
        return x->leftChild;
    }
    Node<type>* y = x->parent;
    while((y!=NULL)&&(x!=y->leftChild)){
        x = y;
        y = x->parent;
    }
    return y;
}
template<class type>
void BinaryTree<type>::insert(type key){
    Node<type> newNode(key),y(NULL);
    if(root==NULL){
        root = newNode;
    }
    y = root;
    while(true){
        if(y->data>newNode->data){
            if(y->leftChild!=NULL){
                y = y->leftChild;
            }else{
                y->leftChild = newNode;
                newNode->parent = y;
                return;
            }
        }else{
            if(y->rightChild!=NULL){
                y = y->rightChild;
            }else{
                y->rightChild = newNode;
                newNode->parent = y;
                return;
            }
        }
    }
}
template<class type>
bool BinaryTree<type>::remove(type key){
    Node<type>* tmp = root;
    if(root==NULL){
        cout<<"NOOOOO!"<<endl;
        return false;
    }
    while(tmp!=NULL){
        if(tmp->data>key){
            tmp = tmp->leftChild;
        }else if(tmp->data<key){
            tmp = tmp->rightChild;
        }else{
            break;
        }
    }
    Node<type>* n ;
    if((tmp->leftChild==NULL)&&(tmp->rightChild==NULL)){
        tmp->parent = NULL;
    }else if((tmp->leftChild==NULL)||(tmp->rightChild==NULL)){
        (tmp->leftChild==NULL)?n = tmp->rightChild : n = tmp->leftChild;
        tmp->parent = n->parent;
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值