树-02_二叉查找树

树-02_二叉查找树

  • 一、定义
        • BST:Binary Search Tree
        • 二叉查找树的性质:
  • 二、代码
        • main.cpp
        • BinarySearchTree.h

一、定义

BST:Binary Search Tree

二叉查找树的性质:

        1、每一个元素有一个键值,而且不允许重复
        2、左子树的键值都小于根节点的键值
        3、右子树的键值都大于根节点的键值
        4、左右子树都是二叉查找树

二、代码

main.cpp

#include 
#include "BinarySearchTree.h"

using namespace std;

int main()
{
    cout << "二叉查找树:" << endl;
    return 0;
}

BinarySearchTree.h

#ifndef BINARTSEARCHTREE_H
#define BINARTSEARCHTREE_H

enum Boolean//自己做的布尔类型
{
    FALSE,
    TRUE
};

template  class BinarySearchTree;//前置声明

/*节点数据类,便于添加新的数据*/
template 
class Element
{
public:
    Type key;
    //此处可添加更多的数据
};

/*二叉查找树节点类*/
template 
class BSTNode
{
    friend class BinarySearchTree;
private:
    Element data;//节点数据值
    BSTNode* leftChild;//左孩子
    BSTNode* rightChild;//右孩子
    void Display(int i);//显示左孩子右孩子
};

/*二叉查找树类:代表整棵树*/
template 
class BinarySearchTree
{
public:
    BinarySearchTree(BSTNode *init = 0)
    {
        root = init;
    }
    Boolean Insert(const Element & x);

    //在整棵树中查找有没有结点值为x的结点,如果找到了就返回指向这个结点的指针,如果没有找到就返回空指针
    BSTNode* Search(const Element& x);
    BSTNode* Search(BSTNode*,const Element&);//方法一:递归查找

    BSTNode* IterSearch(const Element&);//方法二:迭代查找
};

template 
void BSTNode::Display(int i)
{

}

#endif // BINARTSEARCHTREE_H

你可能感兴趣的:(数据结构--C++描述,C++,二叉树)