Download presentation
Presentation is loading. Please wait.
Published byAntonia McLaughlin Modified over 8 years ago
1
Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1
2
Binary Search Trees Data Structures and Programming Techniques 2
3
Definition A binary search tree is a binary tree in which the nodes are labeled with elements of a set. Binary search trees have the following property. For each node N: Keys in left subtree of N < Key K in node N < Keys in right subtree of N This condition is called the binary search tree property. Data Structures and Programming Techniques 3
4
Assumption We will assume that no two nodes in a binary search tree have equal keys. The assumption can be removed but then relevant algorithms will be a little bit more complicated. Data Structures and Programming Techniques 4
5
Example Data Structures and Programming Techniques 5 10 5 14 7 12 18 15
6
Example (for the same set) Data Structures and Programming Techniques 6 15 14 18 712 10 5
7
Example ORY ZRH JFK BRU MEX ARN DUS ORD NRT GLA GCM Data Structures and Programming Techniques 7
8
Searching for a Key To search for a key K in a binary search tree T, we compare K to the key K r of the root of T. If K==K r, the search terminates successfully. If K<K r, the search continues in the left subtree of T. If K>K r, the search continues in the right subtree of T. If T is the empty tree, the search returns NULL. Data Structures and Programming Techniques 8
9
Type Definitions for Binary Search Trees /* This is the file BinarySearchTreeTypes.h */ typedef int TreeEntry; typedef int KeyType; /* the types TreeEntry and KeyType depend on the application */ typedef struct TreeNodeTag { TreeEntry entry; struct TreeNodeTag *left; struct TreeNodeTag *right; } TreeNode; Data Structures and Programming Techniques 9
10
Searching for a Key /* TreeSearch: search for target starting at node root. Pre: The tree to which root points has been created. Post: The function returns a pointer to a tree node that matches target or NULL if the target is not in the tree. */ TreeNode *TreeSearch(TreeNode *root, KeyType target) { if (root) if (target entry) root=TreeSearch(root->left, target); else if (target > root->entry) root=TreeSearch(root->right, target); return root; } Data Structures and Programming Techniques 10
11
Inserting a Key To insert a key K in a binary search tree T, we compare K to the key K r of the root of T. If K==K r, the key is already present in T and the algorithm stops. If K<K r, the algorithm continues in the left subtree of T. If K>K r, the algorithm continues in the right subtree of T. If T is the empty tree, then key K is inserted there. Data Structures and Programming Techniques 11
12
Inserting a Key /* InsertTree: insert a new node in the tree. Pre: The binary search tree to which root points has been created. The parameter newnode points to a node that has been created and contains a key in its entry. Post: The node newnode has been inserted into the tree in such a way that the properties of a binary search tree are preserved. */ TreeNode *InsertTree(TreeNode *root, TreeNode *newnode) { if (!root){ root=newnode; root->left=root->right=NULL; } else if (newnode->entry entry) root->left=InsertTree(root->left, newnode); else root->right=InsertTree(root->right, newnode); return root; } Data Structures and Programming Techniques 12
13
Example Let us insert keys e, b, d, f, a, g, c into an initially empty tree in the order given. Data Structures and Programming Techniques 13
14
Insert e e Data Structures and Programming Techniques 14
15
Insert b e b Data Structures and Programming Techniques 15
16
Insert d e b d Data Structures and Programming Techniques 16
17
Insert f e b d f Data Structures and Programming Techniques 17
18
Insert a e b d f a Data Structures and Programming Techniques 18
19
Insert g e b d f a g Data Structures and Programming Techniques 19
20
Insert c e b d f a g c Data Structures and Programming Techniques 20
21
Inserting in the Natural Order If we insert the previous keys in their natural order a, b, c, d, e, f, g then the tree constructed is a chain. Data Structures and Programming Techniques 21
22
Insert a, b, c, d, e, f, g b a g c f e d Data Structures and Programming Techniques 22
23
Inserting in the Natural Order (cont’d) As we will see below, chains result in inefficient searching. So we should never insert keys in their natural order in a binary search tree. Similar things hold if keys are in reverse order or if they are nearly ordered. Data Structures and Programming Techniques 23
24
Example e b d f a g c Data Structures and Programming Techniques 24 Let us traverse this tree in inorder. What do you notice?
25
Inorder Traversal of Binary Search Trees If we traverse a binary search tree using the inorder traversal, then the keys of the nodes come out sorted in their natural order. This gives rise to a sorting algorithm called TreeSort: insert the keys one by one in a binary search tree, then traverse the tree inorder. Data Structures and Programming Techniques 25
26
Deletion from a Binary Search Tree An algorithm for deleting a key K from a binary search tree T is the following. If K is in a leaf node then delete it and replace the link to the deleted node by NULL. If K is not in a leaf node but has only one subtree then delete it and adjust the link from its parent to point to its subtree. If K is in a node with both a left and right subtree then attach the right subtree in place of the deleted node, and then hang the left subtree onto an appropriate node of the right subtree. Data Structures and Programming Techniques 26
27
Pictorially: Deletion of a Leaf x Delete x Data Structures and Programming Techniques 27
28
Pictorially: Empty Right Subtree x Delete x y y Data Structures and Programming Techniques 28
29
Pictorially: Empty Left Subtree x Delete x y y Data Structures and Programming Techniques 29
30
Pictorially: Neither Subtree is Empty x Delete x y z z y Data Structures and Programming Techniques 30
31
Deleting a Node /* DeleteNodeTree: delete a new node from the tree. Pre: The parameter p is the address of an actual node (not a copy) in a binary search tree, and p is not NULL. Post: The node p has been deleted from the binary search tree and the resulting smaller tree has the properties required of a binary search tree. */ void DeleteNodeTree(TreeNode **p) { TreeNode *r=*p, *q; /* used to find place for left subtree */ if (r==NULL) printf("Attempt to delete a nonexistent node from binary search tree\n"); else if (r->right==NULL){ *p=r->left; /* Reattach left subtree */ free(r); } else if (r->left==NULL){ *p=r->right; /* Reattach right subtree */ free(r); } else { /* Neither subtree is empty */ for (q=r->right; q->left; q=q->left) ; /* find leftmost node of right subtree */ q->left=r->left; /* Reattach left subtree */ *p=r->right; /* Reattach right subtree */ free(r); } Data Structures and Programming Techniques 31
32
Deleting a Node Given a Key /* DeleteKeyTree: delete a node with key target from the tree. Pre: root is the root of a binary search tree with a node containing key equal to target. Post: The node with key equal to target has been deleted and returned. The resulting tree has the properties required of a binary search tree. Uses: DeleteKeyTree recursively, DeleteNodeTree */ void DeleteKeyTree(TreeNode **root, TreeNode **keyposition, KeyType target) { if (*root==NULL) printf("Attempt to delete a key not present in the binary search tree\n"); else if (target == (*root)->entry){ *keyposition=*root; DeleteNodeTree(root); } else if (target entry) DeleteKeyTree(&(*root)->left, keyposition, target); else DeleteKeyTree(&(*root)->right, keyposition, target); } Data Structures and Programming Techniques 32
33
Example: Delete z r b a x y z y x r b a Data Structures and Programming Techniques 33
34
Example: Delete x r b a x y z z y r b a Data Structures and Programming Techniques 34
35
Example: Delete r r b a x y z z y x b a Data Structures and Programming Techniques 35
36
Example: Delete r r c b a z y x z y x c b a Data Structures and Programming Techniques 36
37
Discussion The algorithm for deleting a node may result in an increase to the height of the tree and make subsequent operations inefficient. Can we find a better algorithm? Data Structures and Programming Techniques 37
38
Better Algorithm for Deletion If the key K to be deleted is in an interior node N with two children, then we can find the lowest-valued key K’ in the descendants of the right child and replace K by K’. This key is in a node N’ which is the successor of N under the inorder traversal of the tree. This node can be found by starting at the right child of N and then following left child pointers until we find a node with a NULL left child. Of course, we also need to remove node N’ from the tree. This can be done easily since this node has at most one (right) child. Data Structures and Programming Techniques 38
39
Better Algorithm for Deletion (cont’d) Notice that the highest-valued key among the descendants of the left child would do as well. This key is in a node N’’ which is the predecessor of N under the inorder traversal of the tree. Data Structures and Programming Techniques 39
40
Example: Delete 10 Data Structures and Programming Techniques 40 10 5 14 7 1218 15 13 The lowest-valued key among the descendants of 14 is 12. This key will replace 10 In the tree and its current node will be removed.
41
Example: Result Data Structures and Programming Techniques 41 12 5 14 13 18 15 7
42
Example: Delete 10 Data Structures and Programming Techniques 42 10 5 14 7 1218 15 13 Alternatively, we can replace 10 with the highest-valued key among the descendants of its left child. This is the key 7.
43
Example: Alternative Result Data Structures and Programming Techniques 43 7 5 14 12 18 15 13
44
Complexity Analysis Data Structures and Programming Techniques 44
45
Best Case Examples Data Structures and Programming Techniques 45
46
Best Case Examples (cont’d) Data Structures and Programming Techniques 46
47
Best Case Examples (cont’d) Data Structures and Programming Techniques 47
48
Complexity Analysis (cont’d) Data Structures and Programming Techniques 48
49
Worst Case Example (Left-linear Tree) Data Structures and Programming Techniques 49
50
Worst Case Example (Zig-zag) Data Structures and Programming Techniques 50
51
Worst Case Example (Right-linear) Data Structures and Programming Techniques 51
52
Complexity Analysis (cont’d) Data Structures and Programming Techniques 52
53
Complexity Analysis (cont’d) The complexity of insertion in a binary search tree is the same as the complexity of search given that it makes the same comparisons plus changing a few pointers. The complexity of deletion is also similar for the more clever algorithm that we gave. Data Structures and Programming Techniques 53
54
Discussion Data Structures and Programming Techniques 54
55
Example 5 3 2 4 6 7 Data Structures and Programming Techniques 55
56
Inserting Key 1 5 3 2 4 6 7 1 Data Structures and Programming Techniques 56
57
Rebalancing 4 2 1 3 5 6 7 Data Structures and Programming Techniques 57
58
Question Data Structures and Programming Techniques 58
59
Readings T. A. Standish. Data Structures, Algorithms and Software Principles in C. – Chapter 9. Section 9.7 R. Kruse, C. L. Tondo and B. Leung. Data Structures, Algorithms and Program Design in C. – Chapter 9. Section 9.2 Ν. Μισυρλής. Δομές Δεδομένων με C. – Κεφ. 8 Data Structures and Programming Techniques 59
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.