Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Trees Types and Operations
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees, Binary Trees, and Binary Search Trees COMP171.
BST Data Structure A BST node contains: A BST contains
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
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.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
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:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Binary Search Tree Qamar Abbas.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
CC 215 Data Structures Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
CSE 373 Data Structures Lecture 7
CS200: Algorithms Analysis
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees CSE 373 Data Structures.
Chapter 12: Binary Search Trees
Tree.
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Presentation transcript:

Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

 When we store ordered data in an array, we have a very efficient search algorithm, the binary search.  However, we have very inefficient insertion and deletion algorithms that require shifting data in the array.

 To provide for efficient insertions and deletions, we developed the linked list.  The problem with linked lists, however, is that their search algorithms are sequential searches, which are very inefficient.  Can’t we get the best of both worlds (i.e., efficient search, insertion, and deletion algorithms)?

 The binary search tree is our answer!  The binary search tree is a binary tree with the following properties: ◦ All items (keys) in the left subtree are less than the root’s key. ◦ All items (keys) in the right subtree are greater than the root’s key. ◦ Each subtree is, itself, a binary search tree.

◦ Assume each node of a binary tree stores a data item ◦ Assume data items are of some type that be ordered and all items are distinct. No two items have the same value. ◦ A binary search tree is a binary tree such that ◦ for every node X in the tree:  the values of all the items in its left subtree are smaller than the value of the item in X  the values of all items in its right subtree are greater than the value of the item in X.

Here you can see the basic structure of a binary search tree.

 A binary search tree is organized as the name suggests  A tree can be represented by a linked data structure where each node is an object  In addition to a key field, each node contains fields left, and right, that correspond to its left child and right child LeftRightKey LeftRightKeyLeftRightKey LeftRightKey

 If a child or parent is missing, the appropriate field contains the value NULL.  The root is only node in the tree, whose parent field is NULL.  Top of the tree is known as ‘root’ and the exposed nodes at bottom are known as ‘leafs’.

A binary search tree Not a binary search tree, but a binary tree

- a & b are full and BST -a, b, & d are complete -c and e are skewed

Here we see examples of binary trees that are not binary search trees … why? a) 22 > 17 b) 11 < 17 c) 11 > 6 d) 22 > 17

 Note: we have written this definition in a way that ensures that no two entries in a binary search tree can have equal keys.  Although it is possible to modify the definition to allow entries with duplicate keys, it makes the algorithms somewhat more complicated.  If duplicates need to be accounted for, they can be stored in another data structure (e.g., list).

The search tree property does not say what happens with the elements with the same key. In our examples, all the keys will be distinct but in practice it is important to cover the case of multiple elements with the same key (duplicate keys). The duplicate keys can all be kept in the left subtree, or all in the right subtree. It doesn’t matter which we choose, but it matters that the choice is the same for the whole implementation. Another issue: with duplicate keys, it is important to have extra operations in the interface: getAll, and removeAll

 Binary trees offer short paths from root  A node has up to two children  Data is organized by value  Operations: ◦ Insertion ◦ Search ◦ Traversal ◦ Deletion

 Find Minimum  Find the item that has the minimum value in the tree  Find Maximum  Find the item that has the maximum value in the tree  Print  Print the values of all items in the tree using a traversal strategy that is appropriate for the application  Successor  Predecessor

 The first value inserted goes at the root  Every node inserted becomes a leaf  Descend left or right depending on value

6 28 NULL t Tree root node 5 NULL New Node t t Inserting Item 5 to the Tree

 The BST property allows us to print out all keys in BST in sorted order using a simple recursive algorithm. InOrder_Tree_Walk(x) { if x≠ NULL InOrder_Tree_Walk(left[x]) print key[x] InOrder_Tree_Walk(right[x]) } InOrder_Tree_Walk(x) { if x≠ NULL InOrder_Tree_Walk(left[x]) print key[x] InOrder_Tree_Walk(right[x]) }

, 7, 16, 20, 37, 38, 43

BST-inorder-traverse (node) 1. If node has left child BST-inorder-traverse (Left(node)) 2. Apply operation to node (e.g. cout) 3. If node has right child BST-inorder-traverse (Right(node))  Order for tree above: 1, 2, 3, 5, 6, 7  Application: Display tree in ascending order

BST-preorder-traverse (node) 1. Apply operation to node e.g cout 2. If node has left child BST-preorder-traverse (Left(node)) 3. If node has right child BST-preorder-traverse (Right(node)) Order for tree above: 3, 1, 2, 6, 5, 7 Application: Copy BST

BST-postorder-traverse (node) 1. If node has left child BST-postorder-traverse (Left(node)) 2. If node has right child BST-postorder-traverse (Right(node)) 3. Apply operation to node e.g cout Order for tree above: 2, 1, 5, 7, 6, 3 Application: Deallocate BST

 Want to search for key stored in BST Functions:  Tree search  Minimum  Maximum  Successor  Predecessor

 Start at the root  Is target = value at current node? ◦ We’re done  Is target < value at current node? ◦ Yes: search left subtree ◦ No: search right subtree

Search Starts from the Root

 When given a pointer to the root of a tree and a key, Tree-Search will return a pointer to the node with key k if one exists (x = root). Tree-Search (x, k) { if x=NULL or k=key[ x] return x if k<key [x] return Tree-Search( left[ x], k) else return Tree-Search( right[ x], k) }

 This function follows the BST property, if value of k is smaller than that of x, then it should be in left sub tree of x, else it should be in right sub tree of x

 The minimum element of BST is the left most node of left sub tree.  Therefore the minimum can always be found by tracking the left child pointers until an empty sub tree is reached.  If there is no left sub tree then minimum key is at root( i.e. key[ x]) Tree-Minimum(x) { while( left[ x] != NULL) x = left [x] return x }

 The maximum element of BST is the right most node of right sub tree.  Therefore the maximum can always be found by tracking the right child pointers until an empty sub tree is reached. Tree-Maximum( x) { while( right[ x] != NULL) x = right [x] return x }

 The successor of a node x, is node y, that has the smallest key greater than that of x. ◦ If x has a right subtree, then successor(x) is the left most element in that sub tree. ◦ If x has no right sub tree, then successor(x) is the lowest ancestor of x (above x on the path to the root) that has x in its left sub tree.

The successor of node with key 15 is node with key 17. The successor of node with key 7 is node with key 9. The successor of node with key 13 is node with key 15.

 The predecessor is the node that has the largest key smaller than that of x ◦ If x has a left sub tree, then the predecessor must be the right most element of the left sub tree. ◦ If x has no left subtree, then predecessor (x) is the lowest ancestor of x (above x on the path to the root) that has x in its right subtree.

The predecessor of node with key 6 is node with key 4. The predecessor of node with key 15 is node with key 13. The predecessor of node with key 17 is node with key 15.

 If z has no children, we modify its parent to replace z with NULL as child.  If z has one child, we splice out z by making a new link between its child and its parent.  If node has two children, we splice out z’s successor y, which has no left child and replace the contents of z with the contents of y.

Three cases for remove(x): x occurs at a leaf: simply remove the node containing x removeElement(45) >

x occurs at a node with one child v : remove the node containing x and make v a direct child of x ’s parent (if any) removeElement(20) >

x occurs at a node with two children: first replace x with smallest value in right subtree of x. This value occurs at a node with no left child. So we can delete this node using one of the two previous cases removeElement(15) > Now remove this<---

Deletion of node 2. before after

template void BinarySearchTree :: remove( const Comparable & x, BinaryNode * & t ) const { if( t == NULL ) return; // Item not found; do nothing if( x element ) remove( x, t->left ); else if( t->element < x ) remove( x, t->right ); else if( t->left != NULL && t->right != NULL ) // Two children { t->element = findMin( t->right )->element; remove( t->element, t->right ); } else { BinaryNode *oldNode = t; t = ( t->left != NULL ) ? t->left : t->right; delete oldNode; }