Lecture 7 - Binary Search Trees (1)
Lecture 7 - Binary Search Trees (1)
LECTURE 7
BINARY SEARCH TREES
• Dynamic-set Operations • Chapter 12
Recap
• Dynamic data structures that use pointers:
• Stack
• PUSH(S, x): 𝑂𝑂(1)
• POP(S): 𝑂𝑂(1)
• STACK-EMPTY(S): 𝑂𝑂(1)
• Queue
• ENQUEUE(Q, x): 𝑂𝑂(1)
• DEQUEUE(Q): 𝑂𝑂(1)
• Linked List
• LIST-SEARCH(L, k): 𝑂𝑂(𝑛𝑛)
• LIST-INSERT(L, x): 𝑂𝑂(1)
• LIST-DELETE(L, x): 𝑂𝑂(𝑛𝑛)
• Rooted Tree
2
Introduction (1)
• The search tree data structure supports many dynamic-set operations, including
SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, and DELETE.
• Thus, we can use a search tree both as a dictionary and as a priority queue.
• What is a binary search tree (BST)?
• A BST is organized in a binary tree, where each node carries:
• A key,
• Attributes left, right, and p that point to the nodes corresponding to its left child, its right child,
and its parent, respectively.
• If a child or the parent is missing, the appropriate attribute contains the value NIL.
• The root node is the only node in the tree whose parent is NIL.
• Basic operations on a binary search tree take time proportional to the height of the
tree.
3
Introduction (2)
(a)
(b)
• For any node 𝑥𝑥, the keys in the left subtree of 𝑥𝑥 are at most 𝑥𝑥. 𝑘𝑘𝑘𝑘𝑘𝑘, and the keys in the right subtree of 𝑥𝑥 are at
least 𝑥𝑥. 𝑘𝑘𝑘𝑘𝑘𝑘.
• Different binary search trees can represent the same set of values. The worst-case running time for most
search-tree operations is proportional to the height of the tree.
• (a) A binary search tree on 6 nodes with height 2. (b) A less efficient binary search tree with height 4 that
contains the same keys.
4
Introduction (3)
• The keys in a binary search tree are always stored in such a way as to satisfy
the binary-search-tree property:
• The inorder tree walk prints the keys in each of the two BSTs on Slide 4 in
the order 2, 5, 5, 6, 7, 8.
• It takes Θ(𝑛𝑛) time to walk an 𝑛𝑛-node binary search tree, since after the
initial call, the procedure calls itself recursively exactly twice for each node
in the tree—once for its left child and once for its right child.
6
Querying a BST (1)
• We often need to search for a key stored in a BST.
• Besides the SEARCH operation, BSTs can support queries such as
MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR.
• We shall now examine these operations and show how to support each one
in time 𝑂𝑂(ℎ) on any BST of height ℎ.
• Searching:
• Given a pointer to the root of the tree and a key 𝑘𝑘, the TREE-SEARCH
procedure returns a pointer to a node with key 𝑘𝑘 if one exists;
otherwise, it returns NIL.
7
Querying a BST (2)
• To search for the key 13 in the tree, we follow the path 15 ⟶ 6 ⟶ 7 ⟶ 13 from the
root.
8
Querying a BST (3)
• Can we make the TREE-SEARCH procedure more effective?
• By rewriting the procedure in an iterative fashion by “unrolling” the
recursion into a while loop.
• The minimum key in the tree is 2, which is found by following left pointers from the root.
• The maximum key 20 is found by following right pointers from the root.
10
Querying a BST (5)
• Successor and Predecessor:
• Given a node in a BST, sometimes we
need to find its successor in the sorted
order determined by an inorder tree
walk.
• If all keys are distinct, the successor of
a node 𝑥𝑥 is the node with the smallest
key greater than 𝑥𝑥. 𝑘𝑘𝑘𝑘𝑘𝑘.
• The structure of a BST allows us to
determine the successor of a node
without ever comparing keys. The running time of TREE-SUCCESSOR
on a tree of height ℎ is 𝑂𝑂 ℎ .
11
Querying a BST (6)
• The successor of the node with key 15 is the node with key 17, since it is the minimum
key in the right subtree of 15.
• The node with key 13 has no right subtree, and thus its successor is its lowest
ancestor whose left child is also an ancestor. In this case, the node with key 15 is its
successor.
• Insertion:
• To insert a new value into a BST 𝑇𝑇, we use the procedure TREE-INSERT.
• The procedure takes a node 𝑧𝑧 for which 𝑧𝑧. 𝑘𝑘𝑘𝑘𝑘𝑘 = 𝑣𝑣, 𝑧𝑧. 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙 = NIL, and
𝑧𝑧. 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟 = NIL.
• It modifies 𝑇𝑇 and some of the attributes of 𝑧𝑧 in such a way that it inserts 𝑧𝑧
into an appropriate position in the tree. 13
Insertion & Deletion (2)
18
Insertion & Deletion (7)
• With the TRANSPLANT procedure in hand, here is the procedure that deletes
node 𝑧𝑧 from binary search tree 𝑇𝑇:
19