Chapter 7. Binary Search Trees

Slides:



Advertisements
Similar presentations
Lecture 22, Revision 1 Lecture notes Java code – CodeFromLectures folder* Example class sheets – 4 of these plus solutions Extra examples (quicksort) Lab.
Advertisements

1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
A Binary Search Tree Binary Search Trees.
Chapter 10-A Trees Modified
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 CMSC 341 Introduction to Trees Textbook sections:
(c) University of Washington20-1 CSC 143 Java Trees.
CS 201 Data Structures and Algorithms
Trees Chapter 15.
Data Structure and Algorithms
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees Lecture 12 CS2110 – Fall 2017.
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CS212: Data Structures and Algorithms
Chapter 7. Trees & Binary Trees
Trees.
Binary Search Trees.
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSC 143 Java Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Chapter 7. Binary Search Trees Lecture 14 Chapter 7. Binary Search Trees Set ADT Introduction to trees/binary trees traversals Binary search trees -BST implementation of set ADT ADS2 Lecture 14

Sets A set is a collection of distinct objects Mathematical notation: {1,5,3} Operations: Membership  : 3  {1,3,5} true, 3  {1,2,6} false (say 3  {1,2,6}) Union : {1,3,5}  {2,5} = {1,2,3,5} {1,3,5}  {5} = {1,3,5} Subtraction - : {1,3,5} - {2,5} = {1,3} {1,3,5} - {4} = {1,3,5} Intersection : {1,3,5}  {2,5} = {5} Special case, the empty set: F = { } More examples of sets? See board

Set ADT A (very) simplified version of the java.util Set interface public interface Set<E> extends Iterable<E>{ /**Adds specified element to set if not already present*/ public boolean add(E e); /**Removes all of the elements from this set*/ public void clear(); /**Returns true if set contains specified element*/ public boolean contains(E e); /**Returns true if set contains no elements*/ public boolean isEmpty(); /**Removes specified element from this set, if present*/ public boolean remove(E e); /**Returns number of elements in set (cardinality)*/ public int size(); } ADS2 Lecture 14

Array and linked list implementations of set Simple (extendible) array implementation Sorted (extendible) array implementation Linked list implementation Let n=size of array and s=current size of set. Complexities are: See Sets folder from moodle Array Sorted Array Linked list add O(1) O(log n) contains O(n) O(s) remove ADS2 Lecture 14

Implementations of set contd. Using a sorted array, contains and remove operations are faster, thanks to binary search But then we have to fix the max size of the set (or extend array), and this is expensive spacewise There is another option which allows us to use a technique similar to binary search on a dynamic data structure…. A binary search tree… (a special sort of binary tree) ADS2 Lecture 14

Complexity of set operations using binary search tree If we implement the set as a binary search tree the time complexities are: Array Sorted Array Linked list Binary search tree add O(1) O(log n) O(log s) contains O(n) O(s) remove Will come back to set ADT later on. For now will look at BT (or BST) in own right. ADS2 Lecture 14

Trees A tree is either empty or is a collection of nodes consisting of a root node r and Zero or more subtrees T1,…,Tk each of whose roots are connected via a directed edge to r. The root of each subtree is said to be a child for r, and r is the parent of each subree. Every non-root node has a unique parent. r Note: In practice we omit arrows, assume all lines pointing down T2 T3 Tk T1 . . . See board for examples ADS2 Lecture 14

What do we mean by a path in a tree? From recursive definition of tree see that if there is a (directed) edge from node ni to nj, then ni is the parent of nj and nj is the child of ni For two nodes n1 and nk, a path from n1 to nk is a sequence of nodes n1,n2, … , nk such that ni is the parent of ni+1 for 1  i  k For any path, the length is the no. of edges on the path For any node n, the depth of n is the length of the path from n to the root Depth of a tree is the depth of its deepest node. If we call the node containing x, ‘x’ then path from e to a is e,c,b,a depth of a is 3 depth of tree is 3 ADS2 Lecture 14

Binary Trees A binary tree is a tree in which each node has A reference to a left node A value A reference to a right node A binary tree is either empty or Contains a single node r (the root) whose left and right subtrees are binary trees The tree is accessed via a reference to the root. The nodes with both child references null are the leaves. recursive definition! Examples, see board Remember: to be a tree need unique path from root to every node Binary: every node has at most 2 children. ADS2 Lecture 14

Balance A typical binary tree: This tree is quite well balanced. An extreme unbalanced tree might have no right pointers - would look more like a linked list. Generally tree-based algorithms work most efficiently on balanced trees. A binary tree in which no value occurs in more than one node is injective. We deal only with injective binary trees. ADS2 Lecture 14

Node of binary tree Binary tree node: A question: How would we represent the nodes of a tree (not binary)? We don’t know how many children each node has Binary tree node: public class BTNode<E>{ private E element; private BTNode<E> left; private BTNode<E> right; /**Creates a node with null references*/ public BTNode(){ this(null,null,null); } /** Creates node with the given element, L and R nodes*/ public BTNode(E e, BTNode<E> l,BTNode<E> r){ element = e; left=l; right=r; plus getters and setters ADS2 Lecture 14

Definitions p a node of a binary tree p.left - node of a binary tree - the left subtree p.right - node of a binary tree - the right subtree if p.left=q - p is the parent of q and q is the left child of p if p.right=q - p is the parent of q and q is the right child of p. p Left subtree of p Right subtree of p ADS2 Lecture 14

Traversals A traversal of a binary tree is a sequence of nodes of the tree. Special traversals: 1. Inorder traversal - defined recursively The inorder traversal of an empty tree is the empty sequence The inorder traversal of a non-empty tree is: the inorder traversal of the left subtree (a sequence) the root value (a sequence with one member) the inorder traversal of the right subtree (a sequence) ADS2 Lecture 14

Example (inorder traversal) Inorder traversal is a b c d e f g h i j Examples for you, see board. There will be some additional examples (of all types of traversal) on the examples sheet (some from previous exam papers..) ADS2 Lecture 14

Traversals contd. 2. Preorder traversal – defined recursively The preorder traversal of an empty tree is the empty sequence The preorder traversal of a non-empty tree is: the root value (a sequence with one member) the preorder traversal of the left subtree (a sequence) the preorder traversal of the right subtree (a sequence) Preorder traversal is e c b a d i g f h j Examples for you, see board. ADS2 Lecture 14

Traversals contd. Postorder traversal defined recursively: The postorder traversal of an empty tree is the empty sequence The postorder traversal of a non-empty tree is: the postorder traversal of the left subtree (a sequence) the postorder traversal of the right subtree (a sequence) the root value (a sequence with one member) Postorder traversal is a b d c f h g j i e Examples for you, see board. ADS2 Lecture 14

Inorder traversal in pseudocode Algorithm Inorder(B): Input:Binary Tree B with root r Output: String representation of inorder traversal of B if B is not empty then BL  left subtree of B BR  right subtree of B return Inorder(BL) + r + Inorder(BR) else return "" We can implement this using recursion in Java, just like we did for singly linked lists. ADS2 Lecture 14

Inorder traversal in Java, using recursion Approach 1 (Similar to Linked list approach) Assume following instance variables and constructors in BinaryTree class: public class BinaryTree<E extends Comparable<E>> { private BTNode<E> root; private int size; public BinaryTree(){ root=null; size=0; } /**constructor to create new tree with specific node as root*/ public BinaryTree(BTNode<E> p){ root=p; //haven’t defined size - come back to this ADS2 Lecture 14

Inorder traversal in Java contd. /**create string representing inorder traversal of tree*/ public String toString(){ if (root==null) return ""; else{ BSTree<E> tempTreeL=new BinaryTree<E>(root.getLeft()); BSTree<E> tempTreeR=new BinaryTree<E>(root.getRight()); return tempTreeL + " " + root.getElement()+ " " + tempTreeR; } The non-recursive version takes about 50 of lines of code. This approach almost works. But the new tree we have defined using constructor has no size value defined (so not really a binary tree). Would either have to remove the size instance variable (but we want size() operation to be fast) or calculate size of binary tree starting from given node whenever we do recursion: expensive. ADS2 Lecture 14

Inorder traversal in Java contd. So instead we implement recursion without using constructor, but have to refer to the root of our tree in all our (recursive) methods. Would also use this approach on linked lists if size instance variable included. To avoid having to refer to nodes in external methods, can (like you did in your lab exercise) add an additional method that has no parameter but calls the other one with the root (see next slide). ADS2 Lecture 14

Inorder traversal in Java contd. Approach 2 (we use this one) –assume same instance variables, and first constructor as before (but not second, won’t need it now). /**create string from node p using inorder traversal*/ public String toString(BTNode<E> p){ if (p==null) return ""; else return (toString(p.getLeft())+ " " + p.getElement()+ " " + p.getElement() + " " + toString(p.getRight())); } /** create string for whole tree*/ public String toString(){ return this.toString(root); now need root node passed as parameter Could define an iterator using inorder traversal*, or one of the other two traversals… Actually, you will be doing this in your next lab exercise (Assessment 2) ADS2 Lecture 14

Binary search trees A binary search tree is a binary tree whose inorder traversal is in sorted order As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree There are many more (binary) search trees that have inorder tranersal a b c d e f g h i j See board. ADS2 Lecture 14

Implementing search in a binary search tree Can implement binary search in O(log #s) time on average Takes longer if the tree is badly balanced For every node X, value in all nodes in left subtree of X are less than (or equal to)* value in X, and value of all nodes in right subtree are greater than (or equal to)* value in X Algorithm is simple: if x < root then search left subtree if x > root then search right subtree When the tree is balanced the path length to the leaves is log #s *but we only want injective BSTs, so not “equal to” Example, see board ADS2 Lecture 14

Search in binary search tree, pseudocode Algorithm Contains(B,e,p): Input:Binary Tree B with root p, and element e Output: whether B contains e if B is not empty then if root.element=e then return true else if root.element> e then l  left child of p return Contains(B,e,l) r  right child of p return Contains(B,e,r) else return false ADS2 Lecture 14

Examples from last week: b c d e (i) inorder: e,d,c,b,a preorder: a,b,c,d,e postorder: e,d,c,b,a a b e d c (ii) inorder: e,c,b,d,a preorder: a,b,c,e,d postorder: e,c,d,b,a ADS2 Lecture 14

(iii) a d e f h g b c inorder: d,c,e,g,f,h,b,a preorder: a,b,c,d,e,f,g,h postorder: d,g,h,f,e,c,b,a c e g f a b d inorder: b,a,d,c,f,e,g preorder: a,b,c,d,e,f,g postorder: b,d,f,g,e,c,a (iv) ADS2 Lecture 14