You are searching about Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries, today we will share with you article about Binary search tree – Wikipedia was compiled and edited by our team from many sources on the internet. Hope this article on the topic Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries is useful to you.
Binary search tree – Wikipedia
Binary search tree  

Type  tree  
Invented  1960  
Invented by  P.F. Windley, A.D. Booth, A.J.T. Colin, and T.N. Hibbard  
Time complexity in big O notation  

Fig. 1: A binary search tree of size 9 and depth 3, with 8 at the root. The leaves are not drawn.
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node’s left subtree and less than the ones in its right subtree. The time complexity of operations on the binary search tree is directly proportional to the height of the tree.
Binary search trees allow binary search for fast lookup, addition, and removal of data items. Since the nodes in a BST are laid out so that each comparison skips about half of the remaining tree, the lookup performance is proportional to that of binary logarithm. BSTs were devised in the 1960s for the problem of efficient storage of labeled data and are attributed to Conway BernersLee and David Wheeler.
The performance of a binary search tree is dependent on the order of insertion of the nodes into the tree since arbitrary insertions may lead to degeneracy; several variations of the binary search tree can be built with guaranteed worstcase performance. The basic operations include: search, traversal, insert and delete. BSTs with guaranteed worstcase complexities perform better than an unsorted array, which would require linear search time.
The complexity analysis of BST shows that, on average, the insert, delete and search takes
$$for
$$nodes. In the worst case, they degrade to that of a singly linked list:
$$. To address the boundless increase of the tree height with arbitrary insertions and deletions, selfbalancing variants of BSTs are introduced to bound the worst lookup complexity to that of the binary logarithm. AVL trees were the first selfbalancing binary search trees, invented in 1962 by Georgy AdelsonVelsky and Evgenii Landis.
Binary search trees can be used to implement abstract data types such as dynamic sets, lookup tables and priority queues, and used in sorting algorithms such as tree sort.
History
The binary search tree algorithm was discovered independently by several researchers, including P.F. Windley, Andrew Donald Booth, Andrew Colin, Thomas N. Hibbard. The algorithm is attributed to Conway BernersLee and David Wheeler, who used it for storing labeled data in magnetic tapes in 1960. One of the earliest and popular binary search tree algorithm is that of Hibbard.
The time complexities of a binary search tree increases boundlessly with the tree height if the nodes are inserted in an arbitrary order, therefore selfbalancing binary search trees were introduced to bound the height of the tree to
$$. Various heightbalanced binary search trees were introduced to confine the tree height, such as AVL trees, Treaps, and red–black trees.
The AVL tree was invented by Georgy AdelsonVelsky and Evgenii Landis in 1962 for the efficient organization of information. It was the first selfbalancing binary search tree to be invented.
Overview
A binary search tree is a rooted binary tree in which the nodes are arranged in total order in which the nodes with keys greater than any particular node is stored on the right subtrees and the ones with equal to or less than are stored on the left subtree satisfying the binary search property.
Binary search trees are also efficacious in sortings and search algorithms. However, the search complexity of a BST depends upon the order in which the nodes are inserted and deleted; since in worst case, successive operations in the binary search tree may lead to degeneracy and form a singly linked list (or “unbalanced tree”) like structure, thus has the same worstcase complexity as a linked list.
Binary search trees are also a fundamental data structure used in construction of abstract data structures such as sets, multisets, and associative arrays.
Operations
Searching
Searching in a binary search tree for a specific key can be programmed recursively or iteratively.
Searching begins by examining the root node. If the tree is
$$, 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
$$. If the searched key is not found after a
$$subtree is reached, then the key is not present in the tree.
Recursive search
The following pseudocode implements the BST search procedure through recursion.
RecursiveTreeSearch(x, key) if x = NIL or key = x.key then return x if key < x.key then return RecursiveTreeSearch(x.left, key) else return RecursiveTreeSearch(x.right, key) end if 
The recursive procedure continues until a
$$or the
$$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 efficient.
IterativeTreeSearch(x, key) while x ≠ NIL and key ≠ x.key then 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
$$where
$$is the height of the tree. However, the worst case for BST search is
$$where
$$is the total number of nodes in the BST, because an unbalanced BST may degenerate to a linked list. However, if the BST is heightbalanced the height is
$$.
Successor and predecessor
For certain operations, given a node
$$, finding the successor or predecessor of
$$is crucial. Assuming all the keys of the BST are distinct, the successor of a node
$$in BST is the node with the smallest key greater than
$$‘s key. On the other hand, the predecessor of a node
$$in BST is the node with the largest key smaller than
$$‘s key. Following is pseudocode for finding the successor and predecessor of a node
$$in BST.
BSTSuccessor(x) if x.right ≠ NIL then return BSTMinimum(x.right) end if y := x.parent while y ≠ NIL and x = y.right then x := y y := y.parent repeat return y 
BSTPredecessor(x) if x.left ≠ NIL then return BSTMaximum(x.left) end if y := x.parent while y ≠ NIL and x = y.left then 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.
BSTMaximum(x) while x.right ≠ NIL then x := x.right repeat return x 
BSTMinimum(x) while x.left ≠ NIL then x := x.left repeat return x 
Insertion
Operations such as insertion and deletion cause the BST representation to change dynamically. The data structure must be modified in such a way that the properties of BST continue to hold. New nodes are inserted as leaf nodes in the BST. Following is an iterative implementation of the insertion operation.
1 BSTInsert(T, z) 2 y := NIL 3 x := T.root 4 while x ≠ NIL do 5 y := x 6 if z.key < x.key then 7 x := x.left 8 else 9 x := x.right 10 end if 11 repeat 12 z.parent := y 13 if y = NIL then 14 T.root := z 15 else if z.key < y.key then 16 y.left := z 17 else 18 y.right := z 19 end if 
The procedure maintains a “trailing pointer”
$$as a parent of
$$. After initialization on line 2, the while loop along lines 411 causes the pointers to be updated. If
$$is
$$, the BST is empty, thus
$$is inserted as the root node of the binary search tree
$$, if it is not
$$, insertion proceeds by comparing the keys to that of
$$on the lines 1519 and the node is inserted accordingly.
Deletion
Fig. 2: Binary search tree special cases deletion illustration.
Deletion of a node, say
$$, from a binary search tree
$$should abide three cases:
 If
$$
z
\displaystyle \textz
is a leaf node, the parent node’s pointer to
$$
z
\displaystyle \textz
gets replaced with
$$
nil
\displaystyle \textnil
and consequently
$$
z
\displaystyle \textz
gets removed from the tree.
 If
$$
z
\displaystyle \textz
has a single child node, the child gets elevated as either left or right child of
$$
z
\displaystyle \textz
‘s parent depending on the position of
$$
z
\displaystyle \textz
within the BST, as shown in fig. 2 part (a) and part (b), and as a result,
$$
z
\displaystyle \textz
gets removed from the tree.
 If
$$
z
\displaystyle \textz
has both a left and right child, the successor of
$$
z
\displaystyle \textz
(let it be
$$
y
\displaystyle \texty
) takes the position of
$$
z
\displaystyle \textz
in the tree. This depends on the position of
$$
y
\displaystyle \texty
within the BST:
 If
$$
y
\displaystyle \texty
is
$$
z
\displaystyle \textz
‘s immediate right child,
$$
y
\displaystyle \texty
gets elevated and
$$
y
\displaystyle \texty
‘s left child made point to
$$
z
\displaystyle \textz
‘s initial left subtree, as shown in fig. 2 part (c).
 If
$$
y
\displaystyle \texty
is not the immediate right child of
$$
z
\displaystyle \textz
, deletion proceeds by replacing the position of
$$
y
\displaystyle \texty
by its right child, and
$$
y
\displaystyle \texty
takes the position of
$$
z
\displaystyle \textz
in the BST, as shown in fig. 2 part (d).
 If
The following pseudocode implements the deletion operation in a binary search tree.
1 BSTDelete(T, z) 2 if z.left = NIL then 3 ShiftNodes(T, z, z.right) 4 else if z.right = NIL then 5 ShiftNodes(T, z, z.left) 6 else 7 y := TreeSuccessor(z) 8 if y.parent ≠ z then 9 ShiftNodes(T, y, y.right) 10 y.right := z.right 11 y.right.parent := y 12 end if 13 ShiftNodes(T, z, y) 14 y.left := z.left 15 y.left.parent := y 16 end if 
1 ShiftNodes(T, u, v) 2 if u.parent = NIL then 3 T.root := v 4 else if u = u.parent.left then 5 u.parent.left := v 5 else 6 u.parent.right := v 7 end if 8 if v ≠ NIL then 9 v.parent := u.parent 10 end if 
The
$$procedure deals with the 3 special cases mentioned above. Lines 23 deal with case 1; lines 45 deal with case 2 and lines 616 for case 3. The helper function
$$is used within the deletion algorithm for the purpose of replacing the node
$$with
$$in the binary search tree
$$. This procedure handles the deletion (and substitution) of
$$from the BST.
Traversal
A BST can be traversed through three basic algorithms: inorder, preorder, and postorder tree walks.
 Inorder tree walk: Nodes from the left subtree get visited first, followed by the root node and right subtree.
 Preorder tree walk: The root node gets visited first, followed by left and right subtrees.
 Postorder tree walk: Nodes from the left subtree get visited first, followed by the right subtree, and finally the root.
Following is a recursive implementation of the tree walks.
InorderTreeWalk(x) if x ≠ NIL then InorderTreeWalk(x.left) visit node InorderTreeWalk(x.right) end if 
PreorderTreeWalk(x) if x ≠ NIL then visit node PreorderTreeWalk(x.left) PreorderTreeWalk(x.right) end if 
PostorderTreeWalk(x) if x ≠ NIL then PostorderTreeWalk(x.left) PostorderTreeWalk(x.right) visit node end if 
Balanced binary search trees
Without rebalancing, insertions or deletions in a binary search tree may lead to degeneration, resulting in a height
$$of the tree (where
$$is number of items in a tree), so that the lookup performance is deteriorated to that of a linear search. Keeping the search tree balanced and height bounded by
$$is a key to the usefulness of the binary search tree. This can be achieved by “selfbalancing” mechanisms during the updation operations to the tree designed to maintain the tree height to the binary logarithmic complexity.
Heightbalanced trees
A tree is heightbalanced if the heights of the left subtree and right subtree are guaranteed to be related by a constant factor. This property was introduced by the AVL tree and continued by the RedBlack tree. The heights of all the nodes on the path from the root to the modified leaf node have to be observed and possibly corrected on every insert and delete operation to the tree.
Weightbalanced trees
In a weightbalanced tree, the criterion of a balanced tree is the number of leaves of the subtrees. The weights of the left and right subtrees differ at most by
$$. However, the difference is bound by a ratio
$$of the weights, since a strong balance condition of
$$cannot be maintained with
$$rebalancing work during insert and delete operations. The
$$weightbalanced trees gives an entire family of balance conditions, where each left and right subtrees have each at least a fraction of
$$of the total weight of the subtree.
Types
There are several selfbalanced binary search trees, including Ttree, treap, redblack tree, Btree, 2–3 tree, and Splay tree.
Examples of applications
Sort
Binary search trees are used in sorting algorithms such as tree sort, where all the elements are inserted at once and the tree is traversed at an inorder fashion. BSTs are also used in quicksort.
Priority queue operations
Binary search trees are used in implementing priority queues, using the node’s key as priorities. Adding new elements to the queue follows the regular BST insertion operation but the removal operation depends on the type of priority queue:
 If it is an ascending order priority queue, removal of an element with the lowest priority is done through leftward traversal of the BST.
 If it is a descending order priority queue, removal of an element with the highest priority is done through rightward traversal of the BST.
See also
 Search tree
 Joinbased tree algorithms
 Optimal binary search tree
References
Further reading
 This article incorporates public domain material from Paul E. Black. “Binary Search Tree”. Dictionary of Algorithms and Data Structures. NIST.
 Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). “12: Binary search trees, 15.5: Optimal binary search trees”. Introduction to Algorithms (2nd ed.). MIT Press. pp. 253–272, 356–363. ISBN 0262032937.
 Jarc, Duane J. (3 December 2005). “Binary Tree Traversals”. Interactive Data Structure Visualizations. University of Maryland. Archived from the original on 27 February 2014.
 Knuth, Donald (1997). “6.2.2: Binary Tree Searching”. The Art of Computer Programming. Vol. 3: “Sorting and Searching” (3rd ed.). AddisonWesley. pp. 426–458. ISBN 0201896850.
 Long, Sean. “Binary Search Tree” (PPT). Data Structures and Algorithms VisualizationA PowerPoint Slides Based Approach. SUNY Oneonta.
 Parlante, Nick (2001). “Binary Trees”. CS Education Library. Stanford University. Archived from the original on 20220130.
External links
 Ben Pfaff: An Introduction to Binary Search Trees and Balanced Trees. (PDF; 1675 kB) 2004.
 Binary Tree Visualizer (JavaScript animation of various BTbased data structures)
 v
 t
 e
(dynamic sets/associative arrays)
 2–3
 2–3–4
 AA
 (a,b)
 AVL
 B
 B+
 B*
 B^{x}
 (Optimal) Binary search
 Dancing
 HTree
 Interval
 Order statistic
 (Leftleaning) Red–black
 Scapegoat
 Splay
 T
 Treap
 UB
 Weightbalanced
 Binary
 Binomial
 Brodal
 Fibonacci
 Leftist
 Pairing
 Skew
 van Emde Boas
 Weak
 Ctrie
 Ctrie (compressed ADT)
 Hash
 Radix
 Suffix
 Ternary search
 Xfast
 Yfast
 Ball
 BK
 BSP
 Cartesian
 Hilbert R
 kd (implicit kd)
 M
 Metric
 MVP
 Octree
 PH
 Priority R
 Quad
 R
 R+
 R*
 Segment
 VP
 X
 Cover
 Exponential
 Fenwick
 Finger
 Fractal tree index
 Fusion
 Hash calendar
 iDistance
 Kary
 Leftchild rightsibling
 Link/cut
 Logstructured merge
 Merkle
 PQ
 Range
 SPQR
 Top
Wellknown data structures



Types 

Abstract 

Arrays 

Linked 

Trees 

Graphs 


Video about Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries
UNC: Data Structures – F22 – Lecture 11 – Linked List + Binary Search Tree implementations
Question about Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries
If you have any questions about Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries, please let us know, all your questions or suggestions will help us improve in the following articles!
The article Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries was compiled by me and my team from many sources. If you find the article Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries helpful to you, please support the team Like or Share!
Rate Articles Binary search tree – Wikipedia
Rate: 45 stars
Ratings: 1844
Views: 23359656
Search keywords Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries
0:00 Review of Lecture 10
8:11 Head vs Tail recursion
17:24 Control flow and object creation
26:42 Deletion in the Linked List
36:53 Implementing a Binary Search Tree (BST)
46:30 Insert and Search in BST
1:02:05 Deletions in BST
Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries
way Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries
tutorial Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries
Write A Function That Takes A Binary Search Tree As Input And Produces A Linked List Of The Entries free
Source: en.wikipedia.org