180 likes | 307 Views
CSCI2100B Binary Search Trees Jeffrey Yu@CUHK. Binary Search Trees. In this lecture note, we start our discussions from binary search trees (Section 5.7, Chapter 5 in the textbook). It is good for us to review many things we have discussed.
E N D
Binary Search Trees • In this lecture note, we start our discussions from binary search trees (Section 5.7, Chapter 5 in the textbook). • It is good for us to review many things we have discussed. • Then we highlight the needs of an advanced topic, AVL trees, in Chapter 10 in the textbook. • We don’t have time to discuss AVL in details. Binary Search Trees
Why Do We Talk About It Now? • Consider a problem of size . That is to deal with a set of elements. • If the time complexity of an algorithm is , it means that it needs to check all pairs to find a solution. • Consider the sorting problem. • We can do better with . • If the complexity is , it means that it needs to scan every element once. • Consider the searching problem. • We can do better with . Binary Search Trees
From To • When sorting, consider quickSort and mergeSort, they attempt to divide data into 2 smaller parts repeatedly. A binary tree. • When sorting, consider heapSort, it uses max heap, which in return uses a binary tree. • When searching, assuming data is sorted, it uses binary search. • So, all are related to the idea of binary tree. Binary Search Trees
Searching with a binary tree • Let’s consider searching without sorting first. • Can we have a binary tree for searching purposes? • Let’s call such a binary tree a binary search tree. • Question 1: If we can do so, can we do better when we add new elements, delete elements, and search an element, using the keys associated with elements? • Question 2:Is a binary tree we discussed a binary search tree? Binary Search Trees
A H 13 12 11 L 3 D B 10 7 E I F K 15 14 1 2 8 6 4 5 9 complete binary tree Recall Binary Trees • A binary tree is a finite set of nodes that is either empty or consists of aroot and two disjoint binary trees called the leftsubtree and the rightsubtree. • We show full binary trees, and complete binary trees. full binary tree Binary Search Trees
Trees, Binary-Trees, Max-Trees and Max-Heaps • Trees • The restrictions on the structure: “tree”. • The restrictions on values kept in nodes: none. • Binary trees • The restrictions on the structure: a node can have at most two children (left/right). • The restrictions on values kept in nodes: none. • Max trees • The restrictions on the structure: “tree”. • The restrictions on values kept in nodes: the key value in each node is no smaller than the key values in its children (if any). • Max heaps • The restrictions on the structure: a “complete” binary tree. • The restrictions on values kept in nodes: as that in a max tree. The operations include insertion, deletion, and traversals. Binary Search Trees
[1] 21 [2] 15 [3] 20 [4] [5] [6] 14 10 21 20 15 2 14 10 19 2 Limitations of Trees, Binary-Trees, Max-Trees and Max-Heaps • Consider searching an element in a tree/binary-tree/max-tree/max-heap. The time complexity of such search function is . • Heap is good for implementing priority queue where we want to locate the largest element efficiently. • Heap is not good for searching or deleting arbitrary elements – . Binary Search Trees
12 22 10 30 15 25 40 20 20 2 12 10 5 22 25 15 Binary Search Tree (BST) • A binary search tree is a binary tree which satisfies the following properties. • Every element has a unique key. • All keys in a nonempty left subtree must be smaller than the key in the root of the subtree. • All keys in a nonempty right subtree must be larger than the key in the root of the subtree. Binary Search Trees
BST: The data structures typedefstruct_node { int data; structu _node *left_child, *right_child; } tree_node; typedeftree_node*tree_pointer; Binary Search Trees
Iterative Search of BST • Search an element in a binary search tree using a while loop. tree_pointer search(tree_pointer tree, int key){ while (tree) { /* the root of a subtree is not empty */ if (key == tree->data) return tree; if (key < tree->data) tree = tree->left_child; else tree = tree->right_child; } return NULL;} • The average (also the minimum) height of a binary search tree with nodes is . • The average time complexity for searching an element in a binary search tree is . • Can we efficiently sort & output elements in an ascending order or descending? Binary Search Trees
Sorting using a BST Sorting in ascending order using a binary search tree: void sortAscending(tree_pointer root){ if (root) { sortAscending(root->left_child); print(“%d”, root->data); sortAscending(root->right_child); } Sorting in descending order using a binary search tree: void sortDescending(tree_pointer root) { if (root) { sortDescending(root->right_child); print(“%d”, root->data); sortDescending(root->left_child); } Binary Search Trees
5 80 40 5 30 40 2 5 30 2 40 2 35 80 30 Insert 35 Insert 80 Original Tree Insertion into a BST • Verify that the key is different from those of existing elements in the binary search tree. • If the search is unsuccessful, then we insert the element at the point the search terminated. Suppose the so-called point is a node in the binary search tree. Then, key can be inserted in either its left child node or its right child node depending on the key. Binary Search Trees
1 2 3 2 1 3 3 2 3 2 3 1 1 1 2 Insertion into a BST Insertion 3 keys, 1, 2, 3 with different insertion order. Create an empty binary search tree. Insert 1, 2 and 3. Create an empty binary search tree. Insert 1, 3 and 2. Create an empty binary search tree. Insert 2, 1 and 3. Create an empty binary search tree. Insert 2, 3 and 1. Create an empty binary search tree. Insert 3, 1 and 2. Create an empty binary search tree. Insert 3, 2 and 1. (A) (B) (C) (D) (E) Binary Search Trees
Insertion into a BST • Consider how to insert 40, 20, 60, 10, 50, 45, 30, 55, 70 and 52 into a binary search tree in order. Binary Search Trees
Deletion from a BST • The properties of binary search trees: • Where is the max/min element kept in a tree/subtree? Leftmost/Rightmost node? • Delete an element from a binary search tree in 3 cases: • delete a leaf node. • delete a non-leaf node with one child. • Put the single child in the place of the deleted node. • delete a non-leaf node with two children. • Find the smallest element from its right subtree, or • Find the largest element from its left subtree. • Put the chosen node in the place of the deleted node. Binary Search Trees
Deletion from a BST • Delete 10 from the above tree. • Delete 55 from the above tree. • Delete 60 from the above tree. Binary Search Trees
10 40 20 60 10 30 50 70 20 70 From BST to AVL Trees • Binary Search Trees: • Even though the properties of binary search trees help us to reduce the number of nodes to search. But it doesn't mean it can always be . Why? It depends on insertion order. • To insert 10, 20, 30, 40, 50, 60, 70. • To insert 40, 20, 10, 30, 60, 50, 70. • AVL (Adelson-Veskii & Landis) Trees • Like BST, AVL trees search a single key value. • In addition, AVL Trees try to minimize the search time by maintaining the binary search tree as complete as possible. • To reduce the height of a binary search tree. Binary Search Trees