数据结构与算法(C语言版)__二叉查找树

BST:二叉查找树
Binary Search Tree
二叉查找树的性质:
1.每一元素有一个键值,而且不允许重复
2.左子树的键值都小于根结点的键值
3.右子树的键值都大于根节点的键值
4.左右子树都是二叉查找树

在VS2013中新建项目,在头文件中加入 二叉查找树.h在源文件中添加main.h

//二叉查找树.h
#ifndef 二叉查找树_H
#define 二叉查找树_H

enum Boolean {FALSE,TRUE};
template<class Type>class BST;//前置声明
template<class Type>
class Element{
public:
    Type key;
};

template<class Type>
class BstNode{//树节点
    friend class BST<Type>;
//private:
public:
    Element<Type> data;
    BstNode* LeftChild;
    BstNode* RightChild;
    void display(int i);
};


template<class Type>
class BST{
public:
    BST(BstNode<Type> *init = 0){
        root = init;
    }
    Boolean Insert(const Element<Type>& x);
    //增加 Delete
    //增加前序遍历PreOrder
    //增加中序遍历InOrder
    //增加后序遍历PostOrder
    BstNode<Type>* Search(const Element<Type>& x);
    BstNode<Type>* Search(BstNode<Type>*, const Element<Type>&);
    BstNode<Type>* IterSearch(const Element<Type>&);
    void display(){
        cout << endl;
        if (root)
            root->display(1);
        else
            cout << "这是一个空树" << endl;
    }
private:
    BstNode<Type>* root;
};

template<class Type>
void BstNode<Type>::display(int i){
    std::cout << "Position:" << i << ",data.key = " << data.key << endl;
    if (LeftChild)LeftChild->display(2 * i);
    if (RightChild)RightChild->display(2 * i + 1);
}

template<class Type>
Boolean BST<Type>::Insert(const Element<Type> &x){
    BstNode<Type> *p = root;
    BstNode<Type> *q = 0;//q指向p的父结点
    //insert之前要查找
    while (p){
        q = p;
        if (x.key == p->data.key)return FALSE;//发生重复,失败
        if (x.key < p->data.key)p = p->LeftChild;
        else p = p->RightChild;
    }
    //找到的位置就是q
    p = new BstNode<Type>;
    p->LeftChild = p->RightChild = 0;
    p->data = x;
    if (!root) root = p;
    else if (x.key < q->data.key)q->LeftChild = p;
    else q->RightChild = p;
    return TRUE;
}

template<class Type>
BstNode<Type>* BST<Type>::Search(const Element<Type> &x){
    return Search(root, x);
}

template<class Type>
BstNode<Type>* BST<Type>::Search(BstNode<Type> * b, const Element<Type> &x){
    if (!b) return 0;
    if (x.key == b->data.key)return b;
    if (x.key < b->data.key)return Search(b->LeftChild,x);
    return Search(b->RightChild,x);
}

template<class Type>
BstNode<Type>* BST<Type>::IterSearch(const Element<Type> &x){
    for (BstNode<Type>* t = root; t;){
        if (x.key == t->data.key)return t;
        if (x.key < t->data.key)
            t = t->LeftChild;
        else
            t = t->RightChild;
    }
    return 0;
}
#endif
#include<iostream>
#include"二叉查找树.h"

using namespace std;


int main(){

    cout << "测试二叉查找树:" << endl;

    BST<int> m;
    Element<int> a, b, c, d, e, f, g, h, i, j, k, l;
    a.key = 5;
    b.key = 3;
    c.key = 11;
    d.key = 3;
    e.key = 15;
    f.key = 2;
    g.key = 8;
    h.key = 22;
    i.key = 20;
    j.key = 9;

    cout << endl;
    cout << m.Insert(a) << endl;
    cout << m.Insert(b) << endl;
    cout << m.Insert(c) << endl;
    cout << m.Insert(d) << endl;
    cout << m.Insert(e) << endl;
    cout << m.Insert(f) << endl;
    cout << m.Insert(g) << endl;
    cout << m.Insert(h) << endl;
    cout << endl;
    cout << endl;
    m.display();
    cout << endl;
    cout << endl;
    BstNode<int>* p = m.Search(f);
    cout << "找到的是" << p->data.key << endl;

    BstNode<int>* p2 = m.IterSearch(e);
    cout << "找到的是" << p2->data.key << endl;
    system("pause");
    return 0;

}

总结:二叉查找树集合了数组的优点(可以进行二分查找)和链表的优点(添加和 删除数据都比较快),但是二叉树有可能会进入一个致命缺点,就是当它退化成一个链表时,就比较麻烦。我们这里的二叉查找树还有四个功能没有实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值