&12-2 Find binary search tree

Source: Internet
Author: User

#1, theorem

On a two-fork search tree with a height of H, the operation Search, MINIMUM, MAXIMUM, successor, and predecessor on the dynamic collection can be completed in O (h) time.

#2, pseudo-code

Search, iterative form of search, take the minimum value, take the maximum value, find the successor, find the precursor.

1 //x is a element of the tree, K is the key of element we want to search.2tree-SEARCH (x,k)3 ifX==null or k==X.key4     returnx;5 6 ifk<X.key7     returntree-SEARCH (x.left,k);8 Else9     returnTree_search (X.RIGHT,K);
Tree-search
1 Iterative_tree_search (x,k) 2  while X!=null and k!=x.key3     if k<x.key4         x=  X.left; 5     Else 6         x=x.right; 7 return x;
Iterative_tree_search
1 tree-MINIMUM (x)2 while x.left!=NULL3     x=x.left; 4 return x;
Tree-minimum
1 tree-MAXIMUM (x)2 while x.right!=NULL3     x= X.right; 4 return x;
Tree-maximum
1 tree-successor (x)2if x.right!=NULL3     return Tree_minimum (x.right); 4 y=x.p; 5  while Y!=null and x==y.right6     x=y; 7     y=y.p; 8 return y;
Tree-successor
1 terr-predecessor (x)2if x.left!=NULL3     return MAXIMUM (x.left); 4 y=x.p; 5  while Y!=null and x==y.right6     x=y; 7     y=y.p; 8 return y;
Terr-predecessor

&12-2 Find 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.