Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

Chapter 12 Binary Search Trees
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Comp 245 Data Structures Trees. Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one.
© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group Introduction to Computer Science 2 Binary.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
BST Data Structure A BST node contains: A BST contains
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Starting at Binary Trees
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Binary Search Trees Chapter 7 Objectives
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Lecture 7 Algorithm Analysis
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Data Structures and Programming Techniques
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1

Binary Search Trees Data Structures and Programming Techniques 2

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

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

Example Data Structures and Programming Techniques

Example (for the same set) Data Structures and Programming Techniques

Example ORY ZRH JFK BRU MEX ARN DUS ORD NRT GLA GCM Data Structures and Programming Techniques 7

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

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

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

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

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

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

Insert e e Data Structures and Programming Techniques 14

Insert b e b Data Structures and Programming Techniques 15

Insert d e b d Data Structures and Programming Techniques 16

Insert f e b d f Data Structures and Programming Techniques 17

Insert a e b d f a Data Structures and Programming Techniques 18

Insert g e b d f a g Data Structures and Programming Techniques 19

Insert c e b d f a g c Data Structures and Programming Techniques 20

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

Insert a, b, c, d, e, f, g b a g c f e d Data Structures and Programming Techniques 22

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

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?

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

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

Pictorially: Deletion of a Leaf x Delete x Data Structures and Programming Techniques 27

Pictorially: Empty Right Subtree x Delete x y y Data Structures and Programming Techniques 28

Pictorially: Empty Left Subtree x Delete x y y Data Structures and Programming Techniques 29

Pictorially: Neither Subtree is Empty x Delete x y z z y Data Structures and Programming Techniques 30

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

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

Example: Delete z r b a x y z y x r b a Data Structures and Programming Techniques 33

Example: Delete x r b a x y z z y r b a Data Structures and Programming Techniques 34

Example: Delete r r b a x y z z y x b a Data Structures and Programming Techniques 35

Example: Delete r r c b a z y x z y x c b a Data Structures and Programming Techniques 36

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

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

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

Example: Delete 10 Data Structures and Programming Techniques 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.

Example: Result Data Structures and Programming Techniques

Example: Delete 10 Data Structures and Programming Techniques Alternatively, we can replace 10 with the highest-valued key among the descendants of its left child. This is the key 7.

Example: Alternative Result Data Structures and Programming Techniques

Complexity Analysis Data Structures and Programming Techniques 44

Best Case Examples Data Structures and Programming Techniques 45

Best Case Examples (cont’d) Data Structures and Programming Techniques 46

Best Case Examples (cont’d) Data Structures and Programming Techniques 47

Complexity Analysis (cont’d) Data Structures and Programming Techniques 48

Worst Case Example (Left-linear Tree) Data Structures and Programming Techniques 49

Worst Case Example (Zig-zag) Data Structures and Programming Techniques 50

Worst Case Example (Right-linear) Data Structures and Programming Techniques 51

Complexity Analysis (cont’d) Data Structures and Programming Techniques 52

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

Discussion Data Structures and Programming Techniques 54

Example Data Structures and Programming Techniques 55

Inserting Key Data Structures and Programming Techniques 56

Rebalancing Data Structures and Programming Techniques 57

Question Data Structures and Programming Techniques 58

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