Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Binary search tree
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Searching=== Searching in a binary search tree for a specific key can be programmed [[recursion (computer science)|recursively]] or [[iteration#Computing|iteratively]]. Searching begins by examining the [[root node]]. If the tree is [[Null pointer|{{math|{{text|nil}}}}]], the key being searched for does not exist in the tree. Otherwise, if the key equals that of the root, the search is successful and the node is returned. If the key is less than that of the root, the search proceeds by examining the left subtree. Similarly, if the key is greater than that of the root, the search proceeds by examining the right subtree. This process is repeated until the key is found or the remaining subtree is <math>\text{nil}</math>. If the searched key is not found after a <math>\text{nil}</math> subtree is reached, then the key is not present in the tree.{{r|algo_cormen|pp=290-291}} ====Recursive search==== The following [[pseudocode]] implements the BST search procedure through [[recursion (computer science)|recursion]].<ref name="algo_cormen" />{{rp|290}} {| |- style="vertical-align:top" | Recursive-Tree-Search(x, key) '''if''' x = NIL '''or''' key = x.key '''then''' '''return''' x '''if''' key < x.key '''then''' '''return''' Recursive-Tree-Search(x.left, key) '''else''' '''return''' Recursive-Tree-Search(x.right, key) '''end if''' |} The recursive procedure continues until a <math>\text{nil}</math> or the <math>\text{key}</math> being searched for are encountered. ====Iterative search==== The recursive version of the search can be "unrolled" into a [[while loop]]. On most machines, the iterative version is found to be more [[Computer performance|efficient]].<ref name="algo_cormen" />{{rp|291}} {| |- style="vertical-align:top" | Iterative-Tree-Search(x, key) '''while''' x ≠ NIL '''and''' key ≠ x.key '''do''' '''if''' key < x.key '''then''' x := x.left '''else''' x := x.right '''end if''' '''repeat''' '''return''' x |} Since the search may proceed till some [[leaf node]], the running time complexity of BST search is <math>O(h)</math> where <math>h</math> is the [[Tree (data structure)#Terminology|height of the tree]]. However, the worst case for BST search is <math>O(n)</math> where <math>n</math> is the total number of nodes in the BST, because an unbalanced BST may degenerate to a linked list. However, if the BST is [[height-balanced tree|height-balanced]] the height is <math>O(\log n)</math>.<ref name="algo_cormen" />{{rp|290}} ====Successor and predecessor==== For certain operations, given a node <math>\text{x}</math>, finding the successor or predecessor of <math>\text{x}</math> is crucial. Assuming all the keys of a BST are distinct, the successor of a node <math>\text{x}</math> in a BST is the node with the smallest key greater than <math>\text{x}</math>'s key. On the other hand, the predecessor of a node <math>\text{x}</math> in a BST is the node with the largest key smaller than <math>\text{x}</math>'s key. The following pseudocode finds the successor and predecessor of a node <math>\text{x}</math> in a BST.<ref>{{cite web|url=https://ranger.uta.edu/~huang/teaching/CSE5311/CSE5311_Lecture10.pdf|archive-url=https://web.archive.org/web/20210413045057/http://ranger.uta.edu/~huang/teaching/CSE5311/CSE5311_Lecture10.pdf|archive-date=13 April 2021|page=12|publisher=[[University of Texas at Arlington]]|access-date=17 May 2021|url-status=live|title=Design and Analysis of Algorithms|author=Junzhou Huang}}</ref><ref>{{cite web |author=Ray |first=Ray |title=Binary Search Tree |url=https://cs.lmu.edu/~ray/notes/binarysearchtrees/ |access-date=17 May 2022 |publisher=[[Loyola Marymount University]], Department of Computer Science}}</ref><ref name="algo_cormen" />{{rp|292β293}} {| |- style="vertical-align:top" | BST-Successor(x) '''if''' x.right ≠ NIL '''then''' '''return''' BST-Minimum(x.right) '''end if''' y := x.parent '''while''' y ≠ NIL '''and''' x = y.right '''do''' x := y y := y.parent '''repeat''' '''return''' y | BST-Predecessor(x) '''if''' x.left ≠ NIL '''then''' '''return''' BST-Maximum(x.left) '''end if''' y := x.parent '''while''' y ≠ NIL '''and''' x = y.left '''do''' x := y y := y.parent '''repeat''' '''return''' y |} Operations such as finding a node in a BST whose key is the maximum or minimum are critical in certain operations, such as determining the successor and predecessor of nodes. Following is the pseudocode for the operations.<ref name="algo_cormen" />{{rp|291β292}} {| |- style="vertical-align:top" | BST-Maximum(x) '''while''' x.right ≠ NIL '''do''' x := x.right '''repeat''' '''return''' x | BST-Minimum(x) '''while''' x.left ≠ NIL '''do''' x := x.left '''repeat''' '''return''' x |}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Binary search tree
(section)
Add topic