Introduction to related algorithms of binary search tree

Source: Internet
Author: User
In a binary search tree, the left subtree has a value greater than the root node, and the right subtree has a value greater than the root node. each layer of subtree complies with the preceding rules. Binary search can greatly accelerate the search speed. regular search can only be compared one by one, and the algorithm complexity is n. due to the results, the binary search tree can reduce the search load to log (n ). First, consider node insertion: starting from the root node, if the value of the node to be inserted is greater than the root node, search for the right subtree; otherwise, search for the left subtree until it reaches the leaf node.

In a binary search tree, the left subtree has a value greater than the root node, and the right subtree has a value greater than the root node. each layer of subtree complies with the preceding rules. Binary search can greatly accelerate the search speed. regular search can only be compared one by one, and the algorithm complexity is n. due to the results, the binary search tree can reduce the search load to log (n ).

First, define the node data structure of the binary tree:

struct Tree{struct Tree *p;struct Tree *l;struct Tree *r;char name[31];int num;};

First, consider the insertion of nodes:

Starting from the root node, if the value of the node to be inserted is greater than that of the root node, the right subtree is searched; otherwise, the left subtree is searched until the leaf node is reached. If the value of the leaf node is greater than the value of the node to be inserted, set the left subtree of the leaf node to be inserted; otherwise, set the value to the right subtree. Note: consider empty trees.

void add_num(char name[],struct Tree * tree,struct Tree *parent){    struct Tree * new_node;    if(flag==true) return;    if(tree==0) {        flag=true;        new_node=new struct Tree();        init(new_node);        strcpy(new_node->name,name);        new_node->num++;;        if(parent==0) root=new_node;        else         {            if(strcmp(new_node->name,parent->name)>0)            {                parent->r=new_node;            }            else                 parent->l=new_node;            new_node->p=parent;        }        return;    }     else     {        if(strcmp(tree->name,name)==0) {tree->num++;flag=true;return;}        else if(strcmp(tree->name,name)>0)        {            add_num(name,tree->l,tree);        }        else         {            add_num(name,tree->r,tree);        }    }}

Delete a node:

Deleting a node is complex and requires classification. If the left son does not exist at the same time, the left son can be deleted as the left son of the father;

If both the left and right sons exist, you need to find the successor node (greater than the smallest node) and copy the value to the location of the node to be deleted, and delete its successor node (its successor node cannot have both the right and right sons at the same time !)

Method for finding a successor node: if the right subtree is not empty, find the minimum value in the right subtree, that is, find the leftmost node value in the right subtree; if its right subtree is empty, its successor node may be an ancestor of a level at the current node. Therefore, the node is continuously searched up until the current node is the left son of the parent node, note that the current node is constantly updated.

Code for finding the successor node:

Struct Tree * successor (struct Tree * tree) {struct Tree * temp, * temp1; if (tree-> r! = 0) return find_min (tree-> r); temp1 = tree; temp = tree-> p; while (temp! = 0 & temp-> r = temp1) {temp1 = temp; temp = temp-> p;} if (temp = 0) return tree; // if no successor exists, else return temp; // otherwise, the successor node is returned}

Code for deleting a node: (the smallest node is deleted here)

Struct Tree * delete_min () {struct Tree * tree = find_min (root); // find the node struct Tree * temp1, * temp2; if (tree-> l = 0 | tree-> r = 0) // if the left or right subtree does not exist temp1 = tree; else temp1 = successor (tree); // if the left and right sons exist, find the successor node if (temp1-> l! = 0) temp2 = temp1-> l; else temp2 = temp1-> r; if (temp2! = 0) temp2-> p = temp1-> p; if (temp1-> p = 0) root = temp2; // else if (temp1 = temp1-> p-> l) temp1-> p-> l = temp2; else temp1-> p-> r = temp2; if (temp1! = Tree) tree-> num = temp1-> num; return temp1 ;}

To find the minimum value: In fact, it is very easy to find the leftmost node of the entire tree!

struct Tree* find_min(struct Tree *tree){    if(tree->l!=0) return find_min(tree->l);    else return tree;}

Finally, the output tree is also very simple recursion, such as output in the middle order:

void search(struct Tree *tree){    if(tree!=0)    {        printf("%s\n",tree->name);        if(tree->l!=0)  search(tree->l);        else if(tree->r!=0) search(tree->r);        }}

The above are some basic operations of the binary search tree in the form of a linked list for memo.

This article is available at http://www.nowamagic.net/librarys/veda/detail/206.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.