Topic 18 Binary Search Trees

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
1 Trees most slides taken from Mike Scott, UT Austin.
Department of Computer Science University of Maryland, College Park
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
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.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Topic 17 Introduction to Trees
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
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.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
1 Introduction to trees Instructor: Dimitrios Kosmopoulos.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
CSE 373 Binary search trees; tree height and balance
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
BST Trees
Week 6 - Wednesday CS221.
Binary Search Tree Chapter 10.
Trees.
COMP 103 Binary Search Trees.
Chapter 16 Tree Implementations
Binary Search Trees -Monty Python and The Holy Grail
Binary Search Trees.
Tree data structure.
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
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.
Topic 19 Binary Search Trees
Binary Search Tree AVL Tree
Tree data structure.
Search Sorted Array: Binary Search Linked List: Linear Search
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Binary Search Trees A special case of a Binary Tree
Non-Linear Structures
CS202 - Fundamental Structures of Computer Science II
CSC 143 Binary Search Trees.
Data Structures II AP Computer Science
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Topic 18 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies." -Monty Python and The Holy Grail CS 307 Fundamentals of Computer Science

The Problem with Linked Lists Accessing a item from a linked list takes O(N) time for an arbitrary element Binary trees can improve upon this and reduce access to O( log N ) time for the average case Expands on the binary search technique and allows insertions and deletions Worst case degenerates to O(N) but this can be avoided by using balanced trees (AVL, Red-Black) CS 307 Fundamentals of Computer Science

Binary Search Trees A binary tree is a tree where each node has at most two children, referred to as the left and right child A binary search tree is a binary tree in which every node's left subtree holds values less than the node's value, and every right subtree holds values greater than the node's value. A new node is added as a leaf. root parent 17 < > 11 19 right child left child CS 307 Fundamentals of Computer Science

Attendance Question 1 After adding N distinct elements in random order to a Binary Search Tree what is the expected height of the tree? O(N1/2) O(logN) O(N) O(NlogN) O(N2) CS 307 Fundamentals of Computer Science

Implementation of Binary Node public class BSTNode { private Comparable myData; private BSTNode myLeft; private BSTNode myRightC; public BinaryNode(Comparable item) { myData = item; } public Object getValue() { return myData; } public BinaryNode getLeft() { return myLeft; } public BinaryNode getRight() { return myRight; } public void setLeft(BSTNode b) { myLeft = b; } // setRight not shown } CS 307 Fundamentals of Computer Science

Sample Insertion 100, 164, 130, 189, 244, 42, 141, 231, 20, 153 (from HotBits: www.fourmilab.ch/hotbits/) If you insert 1000 random numbers into a BST using the naïve algorithm what is the expected height of the tree? (Number of links from root to deepest leaf.) CS 307 Fundamentals of Computer Science

Worst Case Performance In the worst case a BST can degenerate into a singly linked list. Performance goes to O(N) 2 3 5 7 11 13 17 CS 307 Fundamentals of Computer Science

More on Implementation Many ways to implement BSTs Using nodes is just one and even then many options and choices public class BinarySearchTree { private TreeNode root; private int size; public BinarySearchTree() { root = null; size = 0; } CS 307 Fundamentals of Computer Science

Add an Element, Recursive CS 307 Fundamentals of Computer Science

Add an Element, Iterative CS 307 Fundamentals of Computer Science

Attendance Question 2 What is the best case and worst case Big O to add N elements to a binary search tree? Best Worst O(N) O(N) O(NlogN) O(NlogN) O(N) O(NlogN) O(NlogN) O(N2) O(N2) O(N2) CS 307 Fundamentals of Computer Science

Performance of Binary Trees For the three core operations (add, access, remove) a binary search tree (BST) has an average case performance of O(log N) Even when using the naïve insertion / removal algorithms no checks to maintain balance balance achieved based on the randomness of the data inserted CS 307 Fundamentals of Computer Science

Remove an Element Three cases node is a leaf, 0 children (easy) node has 1 child (easy) node has 2 children (interesting) CS 307 Fundamentals of Computer Science

Properties of a BST The minimum value is in the left most node The maximum value is in the right most node useful when removing an element from the BST An inorder traversal of a BST provides the elements of the BST in ascending order CS 307 Fundamentals of Computer Science

Using Polymorphism Examples of dynamic data structures have relied on null terminated ends. Use null to show end of list, no children Alternative form use structural recursion and polymorphism CS 307 Fundamentals of Computer Science

BST Interface public interface BST { public int size(); public boolean contains(Comparable obj); public boolean add(Comparable obj); } CS 307 Fundamentals of Computer Science

EmptyBST public class EmptyBST implements BST { private static EmptyBST theOne = new EmptyBST(); private EmptyBST(){} public static EmptyBST getEmptyBST(){ return theOne; } public NEBST add(Comparable obj) { return new NEBST(obj); } public boolean contains(Comparable obj) { return false; } public int size() { return 0; } } CS 307 Fundamentals of Computer Science

Non Empty BST – Part 1 public class NEBST implements BST { private Comparable data; private BST left; private BST right; public NEBST(Comparable d){ data = d; right = EmptyBST.getEmptyBST(); left = EmptyBST.getEmptyBST(); } public BST add(Comparable obj) { int val = obj.compareTo( data ); if( val < 0 ) left = left.add( obj ); else if( val > 0 ) right = right.add( obj ); return this; CS 307 Fundamentals of Computer Science

Non Empty BST – Part 2 public boolean contains(Comparable obj){ int val = obj.compareTo(data); if( val == 0 ) return true; else if (val < 0) return left.contains(obj); else return right.contains(obj); } public int size() { return 1 + left.size() + right.size(); CS 307 Fundamentals of Computer Science