CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
Trees, Binary Trees, and Binary Search Trees COMP171.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
1 CSE 326: Data Structures Part Four: Trees Henry Kautz Autumn 2002.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
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.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
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:
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
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.
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,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
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.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
Binary Search Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CSE 332: Data Abstractions Binary Search Trees
Binary Trees, Binary Search Trees
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
CSE 326: Data Structures Lecture #8 Balanced Dendrology
Binary SearchTrees [CLRS] – Chap 12.
CSE 326: Data Structures Lecture #7 Binary Search Trees
Trees in Data Structures
Binary Trees, Binary Search Trees
Presentation transcript:

CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001

Binary Search TreesCSE 326 Autumn Binary Trees Binary tree is  a root  left subtree (maybe empty)  right subtree (maybe empty) Properties  max # of leaves:  max # of nodes:  average depth for N nodes: Representation: A B DE C F HG JI Data right pointer left pointer

Binary Search TreesCSE 326 Autumn Binary Tree Representation A right child left child A B DE C F B right child left child C right child left child D right child left child E right child left child F right child left child

Binary Search TreesCSE 326 Autumn Dictionary ADT Dictionary operations  create  destroy  insert  find  delete Stores values associated with user- specified keys  values may be any (homogeneous) type  keys may be any (homogeneous) comparable type Adrien Roller-blade demon Hannah C++ guru Dave Older than dirt … insert find(Adrien) Adrien Roller-blade demon Donald l33t haxtor

Binary Search TreesCSE 326 Autumn Dictionary ADT: Used Everywhere  Arrays  Sets  Dictionaries  Router tables  Page tables  Symbol tables  C++ structures  … Anywhere we need to find things fast based on a key

Binary Search TreesCSE 326 Autumn Search ADT Dictionary operations  create  destroy  insert  find  delete Stores only the keys  keys may be any (homogenous) comparable  quickly tests for membership Simplified dictionary, useful for examples (e.g. CSE 326) Adrien Hannah Dave … insert find(Adrien) Adrien Donald

Binary Search TreesCSE 326 Autumn Dictionary Data Structure: Requirements  Fast insertion  runtime:  Fast searching  runtime:  Fast deletion  runtime:

Binary Search TreesCSE 326 Autumn Naïve Implementations unsorted array sorted array linked list insertO(n)find + O(n)O(1) findO(n)O(log n)O(n) deletefind + O(1) (mark-as-deleted) find + O(1) (mark-as-deleted) find + O(1)

Binary Search TreesCSE 326 Autumn Binary Search Tree Dictionary Data Structure Binary tree property  each node has  2 children  result:  storage is small  operations are simple  average depth is small Search tree property  all keys in left subtree smaller than root’s key  all keys in right subtree larger than root’s key  result:  easy to find any given key  Insert/delete by changing links

Binary Search TreesCSE 326 Autumn Example and Counter-Example BINARY SEARCH TREE NOT A BINARY SEARCH TREE 7 15

Binary Search TreesCSE 326 Autumn Complete Binary Search Tree Complete binary search tree (aka binary heap):  Links are completely filled, except possibly bottom level, which is filled left-to-right

Binary Search TreesCSE 326 Autumn In-Order Traversal visit left subtree visit node visit right subtree What does this guarantee with a BST? In order listing: 2  5  7  9  10  15  17  20  30

Binary Search TreesCSE 326 Autumn Recursive Find Node * find(Comparable key, Node * t) { if (t == NULL) return t; else if (key key) return find(key, t->left); else if (key > t->key) return find(key, t->right); else return t; } Runtime: Best-worse case? Worst-worse case? f(depth)?

Binary Search TreesCSE 326 Autumn Iterative Find Node * find(Comparable key, Node * t) { while (t != NULL && t->key != key) { if (key key) t = t->left; else t = t->right; } return t; }

Binary Search TreesCSE 326 Autumn Insert void insert(Comparable x, Node * t) { if ( t == NULL ) { t = new Node(x); } else if (x key) { insert( x, t->left ); } else if (x > t->key) { insert( x, t->right ); } else { // duplicate // handling is app-dependent } Concept:  Proceed down tree as in Find  If new key not found, then insert a new node at last spot traversed

Binary Search TreesCSE 326 Autumn BuildTree for BSTs  Suppose the data 1, 2, 3, 4, 5, 6, 7, 8, 9 is inserted into an initially empty BST:  in order  in reverse order  median first, then left median, right median, etc.

Binary Search TreesCSE 326 Autumn Analysis of BuildTree Worst case is O(n 2 ) … + n = O(n 2 ) Average case assuming all orderings equally likely: O(n log n)  averaging over all insert sequences (not over all binary trees)  equivalently: average depth of a node is log n  proof: see Introduction to Algorithms, Cormen, Leiserson, & Rivest

Binary Search TreesCSE 326 Autumn BST Bonus: FindMin, FindMax  Find minimum  Find maximum

Binary Search TreesCSE 326 Autumn Successor Node Next larger node in this node’s subtree How many children can the successor of a node have? Node * succ(Node * t) { if (t->right == NULL) return NULL; else return min(t->right); }

Binary Search TreesCSE 326 Autumn Predecessor Node Next smaller node in this node’s subtree Node * pred(Node * t) { if (t->left == NULL) return NULL; else return max(t->left); }

Binary Search TreesCSE 326 Autumn Deletion Why might deletion be harder than insertion?

Binary Search TreesCSE 326 Autumn Lazy Deletion Instead of physically deleting nodes, just mark them as deleted + simpler + physical deletions done in batches + some adds just flip deleted flag - extra memory for deleted flag - many lazy deletions slow finds - some operations may have to be modified (e.g., min and max)

Binary Search TreesCSE 326 Autumn Lazy Deletion Delete(17) Delete(15) Delete(5) Find(9) Find(16) Insert(5) Find(17)

Binary Search TreesCSE 326 Autumn Deletion - Leaf Case Delete(17)

Binary Search TreesCSE 326 Autumn Deletion - One Child Case Delete(15)

Binary Search TreesCSE 326 Autumn Deletion - Two Child Case Delete(5) Replace node with descendant whose value is guaranteed to be between left and right subtrees: the successor Could we have used predecessor instead?

Binary Search TreesCSE 326 Autumn Delete Code void delete(Comparable key, Node *& root) { Node *& handle(find(key, root)); Node * toDelete = handle; if (handle != NULL) { if (handle->left == NULL) { // Leaf or one child handle = handle->right; delete toDelete; } else if (handle->right == NULL) { // One child handle = handle->left; delete toDelete; } else { // Two children successor = succ(root); handle->data = successor->data; delete(successor->data, handle->right); }

Binary Search TreesCSE 326 Autumn Thinking about Binary Search Trees Observations  Each operation views two new elements at a time  Elements (even siblings) may be scattered in memory  Binary search trees are fast if they’re shallow Realities  For large data sets, disk accesses dominate runtime  Some deep and some shallow BSTs exist for any data

Binary Search TreesCSE 326 Autumn Beauty is Only  (log n) Deep Binary Search Trees are fast if they’re shallow:  perfectly complete  complete – possibly missing some “fringe” (leaves)  any other good cases? What matters?  Problems occur when one branch is much longer than another  i.e. when tree is out of balance

Binary Search TreesCSE 326 Autumn Dictionary Implementations BST’s looking good for shallow trees, i.e. if Depth is small (log n); otherwise as bad as a linked list! unsorted array sorted array linked list BST insertO(n)find + O(n)O(1)O(Depth) findO(n)O(log n)O(n)O(Depth) deletefind + O(1) (mark-as-deleted) find + O(1) (mark-as-deleted) find + O(1)O(Depth)

Binary Search TreesCSE 326 Autumn Digression: Tail Recursion  Tail recursion: when the tail (final operation) of a function recursively calls the function  Why is tail recursion especially bad with a linked list?  Why might it be a lot better with a tree? Why might it not?

Binary Search TreesCSE 326 Autumn Making Trees Efficient: Possible Solutions Keep BSTs shallow by maintaining “balance”  AVL trees … also exploit most-recently-used (mru) info  Splay trees Reduce disk access by increasing branching factor  B-trees