[Algorithm] binary search tree

Source: Internet
Author: User

"Introduction"

Binary lookup tree is a data structure that supports a variety of dynamic collection operations.

The time of the basic operation performed on the binary lookup tree is proportional to the height of the tree. For a complete binary tree with n nodes, the worst case run time for these operations is O (n).


"struct"

A binary search tree is organized by a two-fork tree structure.

Binary Lookup tree node struct TreeNode {    int val;    TreeNode *left;    TreeNode *right;    TreeNode (int x): Val (x), left (null), right (null) {}};

"Nature"

Set X to two forks to find a node on the tree.

If Y is a node in the left subtree of x, then y->val is less than or equal to X->val.

If Y is a node in the right subtree of x, then y->val is greater than or equal to X->val.

"Output"

Depending on the nature of the binary lookup tree, we can output all the keywords in the tree in sorted order using the middle order recursive traversal algorithm.

Middle sequence traversal output binary lookup tree void inorder (treenode* root) {    if (root = NULL) {        return;    } If    inorder (root->left);    cout<<root->val<<endl;    Inorder (root->right);}

"Insert"

Insert void Treeinsert (treenode*& root,int val) {    //Create new Nodes    treenode* node = new TreeNode (val);    Empty tree    if (root = = NULL) {        root = node;        return;    } If    TreeNode *pre = NULL;    TreeNode *p = root;    Look for the insertion position    while (p) {        ///parent node        pre = P;        Descent along left subtree        if (Val < p->val) {            p = p->left;        } If        //descent along right subtree        else{            p = p->right;        } else    }//while    //left dial hand at node insert    if (Val < pre->val) {        pre->left = node;    } If    //right child node is inserted    else{        pre->right = node;    } Else

Treeinsert starts from the root node and drops along the tree.

The pointer p traces the path, and the pre always points to the parent node of P. After initializing the while loop is the two pointers falling along the tree, depending on the Val and p->val comparison results, you can decide to turn left or right.

Until P becomes null. This null occupies a position where we like to insert.

The feature is that the structure of the tree is usually not generated at once, but in the search process, when there are no keywords in the tree node equal to the given value when you insert them.

the newly inserted nodes must be a newly added leaf node . , and is finding the path when the lookup is unsuccessful on the last node visited on the left child or right child node.

"Find""Recursive algorithm"

Returns a pointer to the node that contains the keyword Val (if present), otherwise returns null

Find treenode* Treesearch (treenode* root,int val) {    if (root = NULL | | root->val = = val) {        return root;    } If    //left dial hand tree Find    if (Val < root->val) {        return treesearch (root->left,val);    } If    //right subtree find    else{        return Treesearch (root->right,val);    } Else

If root is an empty tree, the search fails and returns null directly.

If Root is not an empty tree:

For each node that encounters P, the Val and P->val are compared, and if the two keywords are the same, the lookup ends.

If Val is less than P->val, then continue to find the left subtree of P, and if Val is greater than or equal to P->val, continue to find the right subtree of p.

"Non-recursive algorithm"

Non-recursive lookup treenode* TreeSearch2 (treenode* root,int val) {    if (root = NULL | | root->val = val) {        return root;    }//if    TreeNode *p = root;    while (P && val! = p->val) {        //left dial hand tree Find        if (Val < p->val) {            p = p->left;        } If        //Right subtree lookup        else{            p = p->right;        } else    }//while    return p;}

"Maximum element"

To find the element with the largest keyword in the binary tree, just start from the root node and look down the right pointer of each node until NULL is encountered.

Maximum element treenode* Treemaxnum (TreeNode *root) {    TreeNode *p = root;    while (p->right) {        p = p->right;    }    while return p;}

"Minimum Element"

To find the element with the smallest keyword in the binary tree, just start with the root node and look down the left pointer of each node until NULL is encountered.

Minimum element treenode* treeminnum (TreeNode *root) {    TreeNode *p = root;    while (p->left) {        p = p->left;    }    while return p;}







[Algorithm] binary search tree

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.