Binary search trees. What’s on the menu? ADT DictionaryBST & Algorithms SimulatorComplexity.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
A balanced life is a prefect life.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
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:
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
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.
Binary Search Trees (BST)
I hope that you get along well with your teammate. If not… You can always change. A quick answer about teams.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
CS261 Data Structures Binary Search Trees Concepts.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
AA Trees.
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Cinda Heeren / Geoffrey Tien
Binary Search Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Presentation transcript:

Binary search trees

What’s on the menu? ADT DictionaryBST & Algorithms SimulatorComplexity

Abstract Data Type: Dictionary We’ve seen two ADTs, or behaviours. ListStack add(Object o) get(int i) remove(int i) push(Object o) peek() pop() Let’s look at a 3rd ADT. Dictionary Binary Search Trees

Abstract Data Type: Dictionary We’ve seen two ADTs, or behaviours.Let’s look at a 3rd ADT. Dictionary What’s a dictionary? A set of keys, each associated with a value. key value Binary Search Trees

Abstract Data Type: Dictionary public interface Dictionary{ void add(int key, Object value); void reassign(int key, Object value); Object remove(int key); Object lookup(int key); } Binary Search Trees add a new key associated with a value change the value associated with the key remove the key (hence the value) and return the value return the value associated with the key

Binary Search Trees Abstract Data Type: Dictionary public void example(Dictionary D){ D.add(3, « cheers »); D.add(5, « salamati »); System.out.println( _____D.lookup(5)); D.reassign(5, « slàinte »); System.out.println( _____D.lookup(5)); D.remove(3); } Dictionary D {3, cheers} {5, salamati}{5, slàinte} > salamati > slàinte

Binary Search Trees An implementation: Binary Search Tree A binary search tree (BST) is a tree in which a node can have at most two children and the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller Not a BST: 6 is greater than 4 (and 5) ! In representation, we usually only show the keys. Remember that they are binded to a value.

Binary Search Trees Algorithm: add(int key, Object data) The Rule: the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller add(9, « blabla »); 9 < 12 9 > 8 9 < add(14, « blabla »); 14 > < 16 14

Binary Search Trees Algorithm: add(int key, Object data) The Rule: the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller. private void add(Node current, int key, Object data){ if(key>current.getKey()){ if(current.getRightChild()==null){ Node n = new Node(key, data); current.setRightChild(n); }else add(current.getRightChild(),key, data); }else{ if(current.getLeftChild()==null){ Node n = new Node(key, data); current.setLeftChild(n); }else add(current.getLeftChild(),key, data); } the key I want to add is higher than the current one, I go in the right subtree no right subtree? Then I make a Node and I’m done. otherwise, go deeper in the tree similarly, if the key is less or equal then go in the left subtree

Binary Search Trees Algorithm: add(int key, Object data) The Rule: the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller. private void add(Node current, int key, Object data){ if(key>current.getKey()){ if(current.getRightChild()==null){ Node n = new Node(key, data); current.setRightChild(n); }else add(current.getRightChild(),key, data); }else{ if(current.getLeftChild()==null){ Node n = new Node(key, data); current.setLeftChild(n); }else add(current.getLeftChild(),key, data); } public void add(int key, Object data){ if(root==null) root = new Node(key,data); else add(root,key,data); } In purple, the general case. In blue, when we stop. Below, how we start. The algorithm that the user calls is public. The ‘real’ one private.

Binary Search Trees Algorithm: lookup(int key) The Rule: the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller. Write the algorithm that returns the data bound to a key. Steps you should be thinking of: what do I do in the general case? when do I stop? how do I start? Functions: getRightChild()getLeftChild() getKey() getdata() If the key is greater than the current, go in the right subtree; otherwise, go left. No node: null. Found the key: return the data. No root: null. Otherwise, start on the root.

Binary Search Trees Algorithm: lookup(int key) The Rule: the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller. public Object lookup(int key){ if(root==null) return null; return getNode(root, key); } private Object getNode(Node current, int key){ if(current==null) return null; if(current.getKey()==key) return current.getData(); if(key>current.getKey()) return getNode(current.getRightChild(), key); return getNode(current.getLeftChild(), key); } start general stop

Binary Search Trees Algorithm: lookup(int key) The Rule: the keys into the right subtree of a node c are all greater than the key of c, while the keys in the left subtree are all smaller. public Object lookup(int key){ if(root==null) return null; return getNode(root, key); } private Object getNode(Node current, int key){ if(current==null) return null; if(current.getKey()==key) return current.getData(); if(key>current.getKey()) return getNode(current.getRightChild(), key); return getNode(current.getLeftChild(), key); } 9 1 lookup(5); > null lookup(1);

Binary Search Trees Algorithm: findMin(Node current) Starting from a given node, return the node with the smallest key Strategy: go in the left subtree as long as you can. If you follow the algorithm, it always goes forward in only one direction, like a LinkedList. So it’s not truly recursive, and it can be written in an iterative fashion.

Binary Search Trees Algorithm: findMin(Node current) Starting from a given node, return the node with the smallest key. Strategy: go in the left subtree as long as you can. private Node findMin( Node t ) { if( t != null ) while( t.getLeftChild() != null) t = t.getLeftChild(); return t; }

Binary Search Trees Algorithm: remove(int key) Case 0: You are removing a node with 0 children (i.e. a leaf). stolen from John Edgar Example: removing 30. Strategy: go to the parent (here 23), and set the node to null.

Binary Search Trees Algorithm: remove(int key) Case 1: You are removing a node with only one child. stolen from John Edgar Example: removing 79. Strategy: go to the parent (here 63), and replace the node by its subtree

Binary Search Trees Algorithm: remove(int key) Case 2: You are removing a node with two children. stolen from John Edgar Strategy: replace the node by the minimum in its right subtree temp If we want to remove 32, we need to replace it by one of its children.

Binary Search Trees protected Node remove(int key, Node current){ if(current==null) return null; if(key < current.getKey()) current.setLeftChild(remove(key, current.getLeftChild())); else if(key > current.getKey()) current.setRightChild(remove(key,current.getRightChild())); else if(current.getLeftChild() != null && current.getRightChild() != null ) { Node rightMin = findMin(current.getRightChild()); current.setData(rightMin.getData()); current.setKey(rightMin.getKey()); current.setRightChild(removeMin(current.getRightChild())); } else if(current.getLeftChild()==null) current = current.getRightChild(); else current = current.getLeftChild(); return current; } Algorithm: remove(int key) locate the node If there are 2 children then replace by the minimum in the right subtree, and delete this minimum. 1 child: replace by the subtree 0 child: replace by the subtree (which is null…)

Binary Search Trees Complexity If I’m looking for something, how long can it take at most to find it? The size of the longest path (root to leaf) is the height h of the tree. Since I know exactly where I’m going, it takes me at most O(h). Add/Remove/Lookup are in O(h). If we’re lucky, the tree is well balanced: every level is full. Level 1: 1 node. Level 2: 2 nodes. Level 3: 4 nodes. Level 4: 8 nodes. Level n: 2^n nodes. h = log n 2

Binary Search Trees Complexity If I’m looking for something, how long can it take at most to find it? The size of the longest path (root to leaf) is the height h of the tree. Since I know exactly where I’m going, it takes me at most O(h). Add/Remove/Lookup are in O(h). If we’re lucky, the tree is well balanced: every level is full. h = log n If we’re unlucky, the tree is totally unbalanced, looking like a LinkedList: h = n.

Binary Search Trees Complexity Add/Remove/Lookup are in O(h). → Balanced tree: Add/Remove/Lookup are in O(log n) → Unbalanced tree: Add/Remove/Lookup are in O(n) It is important to keep the tree balanced in order to have the algorithms efficient. We will see different ways to achieve it. 2 If you’re simply asked about the complexity, say O(h). If you have to be more detailed, then distinguish balanced and unbalanced cases.