[Study notes] binary search tree / BST

Binary search tree ( \ (BST \) ), is similar to a stack data structure,

And it is also the basis for the balance of the tree.

So let's look at it a binary search tree.

(In fact, Benpian as pre-knowledge on balanced tree before, but in order to avoid duplicationToo lazy to writeThey alone carry out)

First, a binary search tree is a tree data structurenonsenseEach node in the tree has a weight \ (Val \) .

And any node in the tree, the following properties are satisfied:

  1. The right node value is not less than its weight in the left subtree of any node.

  2. The right node is not greater than the weight of any node that a right subtree.

Obviously, in order binary search tree traversal is an ascending sequence.

So then, let us know \ (BST \) operating it.

The establishment of BST: step 1

In fact, this is very simple, is to avoid cross-border inserted \ (INF \) and \ (- INF \) only.

Look at the code it (is not simply good):

struct BST{int l,r,val;}t[100001];
int tot=0,rt/*根节点*/,INF=1<<30;

inline int New(int val){
    t[++tot].val=val;
    return tot;
}

inline void build(){
    New(-INF);New(INF);
    rt=1;t[1].r=2;
}

step 2: BST Find

Find also well understood.

Suppose we want to find is the weight \ (v \) ,

Then according to two properties,

If \ (v \) is less than the weight of the current node, then go to the left, or go out and go.

Equal or empty node (i.e. no), the processing returns.

On the code (should be a good think about it):

int find(int p,int val){
    if(!p||t[p].val==val) return p;
    return find(val<t[p].val? t[p].l:t[p].r,val);
}

step 3: BST insertion

shanchu In fact, this idea and find the same.

When inserted, if has been inserted through, direct return (or according to the title), the

When the empty nodes on the new node.

step 4: BST precursor & seeking successor

Take as an example the precursor of it (as is the same reason).

First, let's look for the point required \ (the p-\) ,

So there are several situations:

  1. Not found \ (the p-\) .
  2. Found \ (p \) , but \ (p \) no left subtree.
  3. We found \ (p \) , and \ (p \) has left subtree.

In the first case, the answer lies in looking over the path.

Because if you want to insert \ (p \) , then it is certainly in the right subtree of its predecessor (insert simulate what will know).

While the second case, because there is no left subtree, so the answer is also in the path before.

Third, we first find its left subtree, and then has to go to the right, we can find (think about the nature of the BST will be able to understand).

To the code on it:

inline int getpre(int val){
    int ans=1,p=rt;//t[1].val=-INF
    while(p){
        if(val==t[p].val){
            if(!t[p].l) return ans;
            p=t[p].l;
            while(t[p].r) p=t[p].r;
            return p;
        }
        if(t[p].val<val&&t[p].val>t[ans].val) ans=p;
        p=val<t[p].val? t[p].l:t[p].r;
    }
    return ans;
}

step 5: Delete the BST

Delete some of the complex will qwq.

If the node is empty, return directly. (To exclude one case)

So suppose we find the point you want to delete,

There are three cases:

  1. \ (p \) is a leaf node.
  2. \ (p \) no left / right subtree.
  3. \ (p \) with a left / right subtree.

For the first, direct delete just fine.

While the second, then you can replace it with a child.

The third bit difficult to think, ah ...

In fact, we can find its predecessor / successor node (that is, the last step of the third case).

Then it instead of the line.

To the code on it:

inline void remove(int &p,int val){
    if(!p) return ;
    if(val==t[p].val){
        if(!t[p].l) p=t[p].r;
        else if(!t[p].r) p=t[p].l;
        else {
            int next=t[p].r;
            while(t[p].l) p=t[p].l;
            remove(t[p].r,t[next].val);
            t[next].l=t[p].l;t[next].r=t[p].r;
            p=next;
        }
        return ;
    }
    remove(val<t[p].val? t[p].l:t[p].r,val);
}

to sum up

BST Several operations are finished it!

However, one thing did not find?

If we turn insert an up / down sequence, BST will be card into a chain.

So, we have a balanced tree ...

Guess you like

Origin www.cnblogs.com/zsq259/p/10981484.html