COMP 103 Binary Search Trees.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Trees Types and Operations
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) 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.
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."
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Search Trees (BST)
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.
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.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
COMP 53 – Week Fourteen Trees.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Algorithms for Information Processing
Trees Chapter 11 (continued)
CSE 373 Binary search trees; tree height and balance
Multiway Search Trees Data may not fit into main memory
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
The Tree Data Structure
BST Trees
Cinda Heeren / Geoffrey Tien
Week 6 - Wednesday CS221.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Cse 373 April 14th – TreEs pt 2.
Binary Search Trees (I)
Binary Search Trees -Monty Python and The Holy Grail
i206: Lecture 13: Recursion, continued Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
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
Monday, April 16, 2018 Announcements… For Today…
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
CSE 373: Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
A Robust Data Structure
Lecture 12 CS203 1.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Self-Balancing Search Trees
CSE 373 Data Structures and Algorithms
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Trees.
1 Lecture 13 CS2013.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

COMP 103 Binary Search Trees

RECAP-TODAY RECAP Tree traversals TODAY: Binary Search Trees Using tree data structures for implementing Sets, Bags and Maps Contains, add Another sort! Remove READING Chapter 17, sections 1 to 3

Recap – traversals on trees Depth-first vs. breadth-first Breadth-first: an iterative algorithm, that uses a queue! Depth-first: pre-order vs. post-order naturally recursive algorithm but you can also do it iteratively, using a stack. if it’s a binary tree, can have in-order traversals it’s not so obvious how to do this iteratively though... with otherwise identical code!

Implementing Sets, Bags and Maps We’ve seen several ways to implement these collections: Add on set has to check whether it’s already there: O(log n) for sorted array and O(n) otherwise. Also finds place to insert for sorted array or list. Actual insertion is O(n) for sorted array and O(1) otherwise Data Structure Add Find Remove Unsorted Array Bag: O(1) Set: O(n) O(n) O(n) +O(1) Sorted Array O(log n) O(log n) + O(n) Unsorted Linked List O(n)+O(1) Sorted Linked List

Implementing Sets, Bags and Maps The problem: Finding in sorted arrays is fast – use binary search. Finding in linked lists is slow. Inserting/removing in sorted arrays is slow. Inserting/removing in linked lists is fast once you’ve found the right place, and done member check!! Can we get lookup speed of binary search and insert/remove speed of linked list ?  Note that binary search imposes a tree structure on top of the array.  Binary search tree a binary tree that mimics the binary search structure a linked tree structure that makes insertion/removal fast

Binary Search in a sorted array Imposes a tree structure on the array: Same steps as if we searched in this tree structure: Tree structure allows fast add and remove of linked structures Jack Jacob Jade Jas Jeremy John Joshua Julia Justin 1 2 3 4 5 6 7 8 Jeremy Jacob Jack Jade John Julia Joshua Jas Jill Justin

Binary Search Trees A binary tree with an ordering property: At every node: item in node > all items in left subtree (≥ if duplicates allowed) item in node < all items in right subtree The order in the tree makes searching efficient just as in sorted arrays. Note: in-order traversal lists items in ascending order! Jeremy Jade Joshua Jack Jasmine John Julia Justin Jacob

BSTs for Set (or Bag or Map) Implement a Set as an object containing a count and a reference to the root node of a binary tree of BSTNodes: public class BSTSet <E> extends AbstractSet { private int count; private BSTNode root; … private class BSTNode { public E item; public BSTNode left, right; } Cost of contains, add, remove? How to implement the operations? Optional – makes size() constant, but need to adjust as elements are added/removed. Omit if size() is seldom used. count root BSTSet Jeremy Jade Jack Jasmine John Julia Joshua Justin Jacob BSTNodes

worst case, and average case Cost of BST operations worst case, and average case If a BST contains n nodes, what is the cost of contains: find an item in the tree. add: find the place to put it and insert. remove: find the item and splice it out. count root O G U C K S X n nodes A E I M Q T V Z .. .. .. .. .. .. .. .. … .. .. .. ..

Algorithm / Pseudocode for contains, in BSTSet Follow path from root to place where it be if it was there. public boolean contains (E value) { set currentNode to root while currentNode is not null if value == item in currentNode return true; if value < item in currentNode set currentNode to left child else set currentNode to right child return false } Ex: Write a recursive version Ex: Adapt to return the node containing value. you found it... To be really general, use compareTo – see text book.

Algorithm for add, in BSTSet Follow path as in contains to find insertion point. Always a leaf! public boolean add (E value) { if root is null insert at root, and return true set parentNode to root; // Find the parent node to insert new node while true if value equals item in parentNode // value already in set return false; if value is less than item in parentNode // belongs on left if left child of parentNode is null insert as left child, and return true else set parentNode to left child of parentNode else // belongs on right if right child is null insert as right child, and return true else set parentNode to right child of parentNode special case

Algorithm for add, in BSTSet insert at root: set root to new node containing value, increment count insert as left child: set left child of parentNode to new node with value, insert as right child: set right child of parentNode to new node with value,

Adding items to a BSTSet Add new item: Jeremy Jade Jasmine Joshua Jack Jacob John Julia Justin count root Jeremy Jade Joshua Jack Jasmine John Julia Justin Jacob

Tree Sort To sort a list: What is the best, average, and worst cost? Insert each item into a BST. In-order traverse the BST, copying items back into list. What is the best, average, and worst cost? What if the tree is automatically kept balanced at all times? Sorted list is the worst for it! While insertion sort is the best for sorted list.

Balanced and Unbalanced BSTs O G C K S X U E M I T Q Z V A O M C K S X U E A I G T Q Z V A V U T X G C M K S O Z E I Q see the discussion of “rotations”, in the text book

Definitions: balanced trees, complete trees if all the leaves are at the same level? (“full”) if all the leaves are approximately the same depth? if all the leaves are in the bottom two levels complete tree: balanced (defn 3), and all leaves on bottom row are to the left. O G C K S X U E A M I Q T Z V

Balancing a Binary Search Tree One way: Balance the tree periodically by writing the elements to a list and rebuilding the tree. Write elements to list: What algorithm? Cost? Rebuild tree: When/how often should we do this? Sorted list is the worst for it! While insertion sort is the best for sorted list.

Balancing a Binary Search Tree Another idea: record the “size” of each subtree. When deleting, promote from the side with bigger size. And another: after inserting, check to see if size of one subtree exceeds the other by more than a specified amount, and if so move one of more elements from that side to the other. What should we use as the “size” of a subtree? Number of elements? Height? How much difference should we allow before moving elements? See discussion of rotations in text book. AVL trees: height balanced – heights of subtrees differ by at most one. Sorted list is the worst for it! While insertion sort is the best for sorted list.

Cost of Contains: Worst case cost: Average case cost: Balanced tree: what is the worst case? Average case cost: what is the average case? Balanced tree: worst case cost average case cost Unbalanced tree:

Cost of adding Add a node at the place we would find it as a new leaf of the tree special case: the root Cost: balanced, average unbalanced, worst case count root O G U C K S X A E I M Q T V Z