Binary Search Trees
description
Transcript of Binary Search Trees
![Page 1: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/1.jpg)
1
Binary Search Trees
Chapter 12
![Page 2: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/2.jpg)
2
![Page 3: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/3.jpg)
3
Search trees -1
• Search trees are data structures that support many dynamic-set operations, including SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, and DELETE.
![Page 4: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/4.jpg)
4
Search trees - 2
• Basic operations on a binary search tree take time proportional to the height of the tree. – For a complete binary tree with n nodes, such
operations run in (lg n) worst-case time.– If the tree is a linear chain of n nodes, however,
the same operations take (n) worst-case time.
![Page 5: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/5.jpg)
5
What is a binary tree?
• Property 1: each node can have up to two successor nodes (children)– The predecessor node of a node is called its
parent– The "beginning" node is called the root (no
parent)– A node without children is called a leaf
• Property 2: a unique path exists from the root to every other node
![Page 6: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/6.jpg)
6
What is a binary search tree?
• a tree can be represented by a linked data structure in which each node is an object.
![Page 7: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/7.jpg)
7
key field - 1
• In addition to a key field and satellite data, each node contains fields 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 field contains the value NIL. The root node is the only node in the tree whose parent field is NIL.
![Page 8: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/8.jpg)
8
key field -2
• The keys in a binary search tree are always stored in such a way as to satisfy the binary-search-tree property:
![Page 9: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/9.jpg)
9
![Page 10: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/10.jpg)
10
Inorder, preorder, and postorder
• The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. This algorithm is so named because the key of the root of a subtree is printed between the values in its left subtree and those in its right subtree.
• Similarly, a preorder tree walk prints the root before the values in either subtree, and
• A postorder tree walk prints the root after the values in its subtrees.
![Page 11: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/11.jpg)
11
![Page 12: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/12.jpg)
12
![Page 13: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/13.jpg)
13
![Page 14: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/14.jpg)
14
![Page 15: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/15.jpg)
15
![Page 16: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/16.jpg)
16
![Page 17: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/17.jpg)
17
INORDER-TREE-WALK procedure
• Procedure to print all the elements in a binary search tree T , we call INORDER-TREE-WALK(root[T ]).
![Page 18: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/18.jpg)
18
![Page 19: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/19.jpg)
19
Querying a binary search tree
![Page 20: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/20.jpg)
20
To search for the key 13 in the tree, we follow the path 15 → 6 → 7 → 13 from the root.
Pointer x
![Page 21: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/21.jpg)
21
Minimum and maximum
![Page 22: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/22.jpg)
22
Successor and predecessor
• Given a node in a binary search tree, it is sometimes important to be able to find its successor in the sorted order determined by an in-order tree walk. If all keys are distinct, the successor of a node x is the node with the smallest key greater than key[x].
![Page 23: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/23.jpg)
23
![Page 24: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/24.jpg)
24
![Page 25: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/25.jpg)
25
![Page 26: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/26.jpg)
26
- the successor of the node with key 15 in Figure 12.2 is the node with key 17- the successor of the node with key 13 is the node with key 15.
- If the right subtree of node x is nonempty, then the successor of x is just the leftmost node in the right subtree,- If the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x.
![Page 27: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/27.jpg)
27
Insertion and deletion
![Page 28: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/28.jpg)
28
Deletion
![Page 29: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/29.jpg)
29
![Page 30: Binary Search Trees](https://reader036.fdocuments.us/reader036/viewer/2022070405/56814039550346895daba7ab/html5/thumbnails/30.jpg)
30
Randomly built binary search trees
Self study