CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
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.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
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:
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 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.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
Instructor: Lilian de Greef Quarter: Summer 2017
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
CSE373: Data Structures & Algorithms More Heaps; Dictionaries; Binary Search Trees Riley Porter Winter 2016 Winter 2017.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms
Binary Search Trees.
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
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 &
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1

Announcements HW2 due start of class Wednesday 16 th April TA sessions next week – Tuesday: More help with homework 2 – Thursday: Binary Search Trees and AVL Trees Spring 2014CSE373: Data Structures & Algorithms2

Previously on CSE 373 – Dictionary ADT stores (key, value) pairs find, insert, delete – Trees Terminology Binary Trees Spring 2014CSE373: Data Structures & Algorithms3

Reminder: Tree terminology Spring 2014CSE373: Data Structures & Algorithms4 A E B DF C G IH LJMKN Node / Vertex Edges Root Leaves Left subtree Right subtree

Example Tree Calculations Spring 2014CSE373: Data Structures & Algorithms5 A E B DF C G IH LJMKN A Recall: Height of a tree is the maximum number of edges from the root to a leaf. Height = 0 A B Height = 1 What is the height of this tree? What is the depth of node G? What is the depth of node L? Depth = 2 Depth = 4 Height = 4

Binary Trees Spring 2014CSE373: Data Structures & Algorithms6 Binary tree: Each node has at most 2 children (branching factor 2) Binary tree is A root (with data) A left subtree (may be empty) A right subtree (may be empty) Special Cases

Tree Traversals A traversal is an order for visiting all the nodes of a tree Pre-order:root, left subtree, right subtree In-order:left subtree, root, right subtree Post-order:left subtree, right subtree, root + * 24 5 (an expression tree) Spring CSE373: Data Structures & Algorithms + * * *5+

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D B

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ ✓✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D B E

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ ✓✓ ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D B E A

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ ✓✓ ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D B E A

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ ✓✓ ✓ ✓ ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D B E A F C

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Spring CSE373: Data Structures & Algorithms ✓ ✓✓ ✓ ✓ ✓✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ D B E A F C G

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } Sometimes order doesn’t matter Example: sum all elements Sometimes order matters Example: evaluate an expression tree A B DE C FG Spring CSE373: Data Structures & Algorithms A B DE C FG ✓ ✓✓ ✓ ✓ ✓✓

Binary Search Tree (BST) Data Structure Structure property (binary tree) –Each node has  2 children –Result: keeps operations simple Order property –All keys in left subtree smaller than node’s key –All keys in right subtree larger than node’s key –Result: easy to find any given key Spring CSE373: Data Structures & Algorithms A binary search tree is a type of binary tree (but not all binary trees are binary search trees!)

Are these BSTs? Spring CSE373: Data Structures & Algorithms Activity!

Find in BST, Recursive Data find(Key key, Node root){ if(root == null) return null; if(key < root.key) return find(key,root.left); if(key > root.key) return find(key,root.right); return root.data; } Spring CSE373: Data Structures & Algorithms Worst case running time is O(n). - Happens if the tree is very lopsided (e.g. list) What is the running time?

Find in BST, Iterative Data find(Key key, Node root){ while(root != null && root.key != key) { if(key < root.key) root = root.left; else(key > root.key) root = root.right; } if(root == null) return null; return root.data; } Spring CSE373: Data Structures & Algorithms Worst case running time is O(n). - Happens if the tree is very lopsided (e.g. list)

Bonus: Other BST “Finding” Operations FindMin : Find minimum node –Left-most node FindMax : Find maximum node –Right-most node Spring CSE373: Data Structures & Algorithms

Insert in BST insert(13) insert(8) insert(31) (New) insertions happen only at leaves – easy! Spring CSE373: Data Structures & Algorithms Again… worst case running time is O(n), which may happen if the tree is not balanced.

Deletion in BST Why might deletion be harder than insertion? 10 Spring CSE373: Data Structures & Algorithms Removing an item may disrupt the tree structure!

Deletion in BST Basic idea: find the node to be removed, then “fix” the tree so that it is still a binary search tree Three potential cases to fix: –Node has no children (leaf) –Node has one child –Node has two children Spring CSE373: Data Structures & Algorithms

Deletion – The Leaf Case delete(17) 10 Spring CSE373: Data Structures & Algorithms

Deletion – The One Child Case Spring CSE373: Data Structures & Algorithms delete(15)

Deletion – The One Child Case Spring CSE373: Data Structures & Algorithms delete(15)

Deletion – The Two Child Case What can we replace 5 with? 10 Spring CSE373: Data Structures & Algorithms delete(5) 4

Deletion – The Two Child Case Idea: Replace the deleted node with a value guaranteed to be between the two child subtrees Options: successor minimum node from right subtree findMin(node.right) predecessor maximum node from left subtree findMax(node.left) Now delete the original node containing successor or predecessor Spring CSE373: Data Structures & Algorithms

Deletion: The Two Child Case (example) Spring CSE373: Data Structures & Algorithms delete(23)

Deletion: The Two Child Case (example) Spring CSE373: Data Structures & Algorithms delete(23)

Deletion: The Two Child Case (example) Spring CSE373: Data Structures & Algorithms delete(23)

Deletion: The Two Child Case (example) Spring CSE373: Data Structures & Algorithms Success! delete(23)

Lazy Deletion Lazy deletion can work well for a BST –Simpler –Can do “real deletions” later as a batch –Some inserts can just “undelete” a tree node But –Can waste space and slow down find operations –Make some operations more complicated: e.g., findMin and findMax ? Spring CSE373: Data Structures & Algorithms

BuildTree for BST Let’s consider buildTree –Insert all, starting from an empty tree Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST –If inserted in given order, what is the tree? –What big-O runtime for this kind of sorted input? –Is inserting in the reverse order any better? O(n 2 ) Not a happy place Spring CSE373: Data Structures & Algorithms

BuildTree for BST Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them –median first, then left median, right median, etc. –5, 3, 7, 2, 1, 4, 8, 6, 9 –What tree does that give us? –What big-O runtime? O(n log n), definitely better Spring CSE373: Data Structures & Algorithms