Data structure: implementation of binary search tree

二叉查找树

A binary tree is a tree-like structure with at most two subtrees as nodes, which can be called left subtree and right subtree. However, for ordinary binary trees, there is not enough information to prompt us what the left subtree and right subtree store, and we have no way to perform efficient information search. In order to improve search efficiency, we will upgrade the binary tree to a binary search tree (Binary Search Tree).

A binary search tree is a binary tree that conforms to the following definition:

1. All node key values ​​in the left subtree of the node are smaller than the node.

2. All node key values ​​in the right subtree of the node are greater than the node.

3. No duplicate key values

In this way, when we search for a node based on the key value, can we search in a direction? Starting from the root node, if the key value is not equal to the target key value and the target key value is smaller than the current node, we will go to the left subtree to continue searching, otherwise we will go to the right subtree. This way you can be sure to advance one level every time. In this way, query efficiency will be greatly improved.

节点结构定义
class BSTNode{
public:
    BSTNode(int d=0,BSTNode* l=0,BSTNode* r=0):data(d),left(l),right(r){};
    int data;
    BSTNode* left;
    BSTNode* right;
 
};

The code implementation in this article is only to illustrate the relevant algorithms and cannot be directly applied to industrial design, so I will not use templates here. The node data is all an integer data.

A data node is required, a left subtree pointer left and a right subtree pointer right.

二叉查找树结构定义
class BST{
public:
    BST(BSTNode* r=0):root(r){
        size = 0;
    }
    ~BST();
    enum dfstype{PRE=0,IN=1,POST=2};
    enum deletetype{MERGE=0,COPY=1};
    //插入
    void insert(int data);
    //查找
    bool find(int data) const;
    //层次遍历
    void bfs() const;
    //深搜遍历
    void dfs(BST::dfstype) const;
    //递归写法
    void predfs(BSTNode*) const;
    void indfs(BSTNode*) const;
    void postdfs(BSTNode*) const;
    //回溯写法
    void predfsNormal() const;
    void indfsNormal() const;
    void postdfsNormal() const;
    //计数写法
    void predfsCount() const;
    void indfsCount() const;
    void postdfsCount() const;
    void clearTime();
    //删除相关
    void deleteELement(int data,enum deletetype type);
    void mergeDelete(BSTNode*&);
    void copyDelete(BSTNode*&);
    //平衡树
    /*
    *中序遍历得到有序数组,然后根据数组重新构建树,效率较低
    */
    void balance();
    void balance(int data[],int first,int last);
    //DSW旋转算法
    void rightRotate(BSTNode* z,BSTNode* f,BSTNode* n);
    void leftRotate(BSTNode* z,BSTNode* f,BSTNode* n);
    void createList();
    void createPerfectTree();
    //AVL树
 
    //其它
    int getSize() const;
    void clear();//清空节点
     
private:
    BSTNode* root;
    int size;
};
插入算法
void BST::insert(int data){
    BSTNode* newnode = new BSTNode(data);
    BSTNode* temp = root;
    BSTNode* pre = 0;
    while(temp!=0){
        pre = temp;
        if(temp->data>data){
            temp = temp->left;
        }else if(temp->data<data){
            temp = temp->right;
        }else{
            return;
        }
    }
    if(pre==0)
        root = newnode;
    else{
        if(pre->data>data){
            pre->left = newnode; 
        }else{
            pre->right = newnode;
        }
    }
    size++;
}
查找元素算法
bool BST::find(int data) const{
    BSTNode* temp = root;
    while(temp!=0)
    {
        if(temp->data==data)
            return true;
        else if(temp->data>data)
            temp = temp->left;
        else
            temp = temp->right;
    }
    return false;
}
树的遍历
树的遍历
方法有两种:深度优先遍历(Depth First Search,DFS),广度优先遍历(Breadth First Search,BFS)
广度优先遍历利用队列一层一层访问即可。
深度优先遍历分未3种:先序、中序、后序遍历。
值得一提的是,对于符合定义的二叉查找树来说,中序遍历的结果恰好是有序的(有趣不有趣?),所以中序遍历应用最为广泛的。

Guess you like

Origin blog.csdn.net/qq_32301683/article/details/101381758
Recommended