BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Similar documents
TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Trees. (Trees) Data Structures and Programming Spring / 28

TREES. Trees - Introduction

Programming II (CS300)

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Binary Trees, Binary Search Trees

8. Binary Search Tree

Spring 2018 Mentoring 8: March 14, Binary Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Algorithms. AVL Tree

CS 171: Introduction to Computer Science II. Binary Search Trees

TREES Lecture 12 CS2110 Spring 2019

Binary Search Trees. Analysis of Algorithms

Lecture 23: Binary Search Trees

Programming Languages and Techniques (CIS120)

CS 261 Data Structures. AVL Trees

CS 350 : Data Structures B-Trees

CS350: Data Structures B-Trees

CS350: Data Structures Red-Black Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Data Structures and Algorithms

CS102 Binary Search Trees

Multi-Way Search Tree

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Binary search trees (chapters )

Binary Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Multi-way Search Trees

Data Structures and Algorithms

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

Binary Search Tree (3A) Young Won Lim 6/2/18

3137 Data Structures and Algorithms in C++

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Balanced Binary Search Trees. Victor Gao

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

A set of nodes (or vertices) with a single starting point

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Transform & Conquer. Presorting

Balanced Trees. Nate Foster Spring 2018

CS 350 : Data Structures Binary Search Trees

ITI Introduction to Computing II

9. Heap : Priority Queue

Programming Languages and Techniques (CIS120)

CISC 235: Topic 4. Balanced Binary Search Trees

CIS265/ Trees Red-Black Trees. Some of the following material is from:

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Binary Search Trees Treesort

Search Trees: BSTs and B-Trees

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

ITI Introduction to Computing II

Balanced Search Trees. CS 3110 Fall 2010

If you took your exam home last time, I will still regrade it if you want.

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Data Structures in Java

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Balanced Search Trees

Binary Search Trees 1

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Trees. Eric McCreath

12 July, Red-Black Trees. Red-Black Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Fall, 2015 Prof. Jungkeun Park

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Trees Slow Insertion in an Ordered Array If you re going to be doing a lot of insertions and deletions, an ordered array is a bad choice.

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

Analysis of Algorithms

CS350: Data Structures Binary Search Trees

We have the pointers reference the next node in an inorder traversal; called threads

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Maps; Binary Search Trees

CS200: Balanced Search Trees

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Red-black trees (19.5), B-trees (19.8), trees

Binary Search Trees (10.1) Dictionary ADT (9.5.1)

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSC148 Week 7. Larry Zhang

CSCI Trees. Mark Redekopp David Kempe

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Binary Search Tree (3A) Young Won Lim 6/6/18

Binary Search Tree. Revised based on textbook author s notes.

Binary Search Tree (3A) Young Won Lim 6/4/18

Friday Four Square! 4:15PM, Outside Gates

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Transcription:

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1

administrivia 2

-assignment 7 due tonight at midnight -asking for regrades through assignment 5 and midterm must be complete by Friday 3

last time 4

5

-trees are a linked data structure with a hierarchical formation -any node is a subtree of some larger tree -except the very top node! -there is a strict parent-to-child relationship among nodes -links only go from parent to child a -there is exactly one path from the root to any other node b c d 6

root parent nodes children Leaf nodes 7

a b c d e subtree rooted at node c (leaf nodes are trees too!) f g h i subtree rooted at node d 8

public static void DFT(BinaryNode N) { if(n == null) return; System.out.println(N.data); DFT(N.left); a } DFT(N.right); b c what does this print out? a b d f e c d e f 9

-pre-order: use N // eg. print N DFT(N.left); DFT(N.right); a -in-order: DFT(N.left); use N // eg. print N DFT(N.right); -post-order: DFT(N.left); DFT(N.right); use N // eg. print N d b f e c note: Nodes are still traversed in the same order, but used (printed) in a different order 10

how to compute the height of a binary tree? int height(node n) { if(n == null) return -1; } else return max(height(n.left), height(n.right)) + 1; complexity? 11

evaluating an expression tree uses which of the following traversal orders? A) pre-order B) in-order C) post-order + - * 3 / 7 ^ 15 11 2 4 12

what is the value of this expression tree? A) 9 B) -1 C) -5 * - 3 5 2 13

what order are the nodes printed using pre-order traversal? A) d b a c e B) a b c d e C) a c b e d d b e a c 14

15

today 16

-binary search trees -insertion -performance -deletion 17

binary search trees (BSTs) 18

-a binary search tree is a binary tree with a restriction on the ordering of nodes -all items in the left subtree of a node are less than the item in the node -all items in the right subtree of a node are greater than or equal to the item in the node -BSTs allow for fast searching of nodes 19

looking for h i is h less than or greater than i? d j b f a c e h g 20

looking for h d is h less than or greater than d? b f a c e h g 21

looking for h f is h less than or greater than f? e h g 22

looking for h h return true g 23

-NOTE: at each step we eliminate approximately half of the tree -what does this imply about the search complexity? -what if the item is not found in the tree? how do we know when to stop and return false? 24

is this a BST? dog A) yes B) no cat fish alpaca elephant zebra bee unicorn bird 25

p is this a BST? A) yes B) no k y i n t z g j r u c v a d 26

insertion 27

we want to insert g i is g less than or greater than i? d j b f a c e h 28

we want to insert g i is g less than or greater than d? d j b f a c e h 29

we want to insert g i d j b f is g less than or greater than f? a c e h 30

we want to insert g i d j b f a c e h is g less than or greater than h? 31

we want to insert g i d j b f a c e h g code demo 32

performance 33

-big-o complexity of BST searching -we disregard one subtree at each step -roughly half of the remaining nodes! -O(log N) -what is the big-o complexity of BST insertion? -searching and insertion are both O(log N) 34

-O(log N) performance requires eliminating half of the possibilities on each step i -are all left and right subtrees the same size? b d f j a c e h g 35

-is this a valid BST? i -what is the big-o complexity of searching and insertion for this tree? -the order in which nodes are inserted determines the structure of the tree -these nodes were inserted in descending order a b d code demo 36

insertion & searching -average case: O(log N) -inserted in random order -worst case:o(n) -inserted in ascending or descending order -best case: O(log N) -how does this compare to a sorted array? 37

BST vs array -arrays can have O(log N) searching performance as well -how? -what is the cost of inserting into an array? -we can put it at the end and resort: O(log N) -or insert it in the right spot: O(N) BST wins for insertion! yay! 38

balanced vs unbalanced all operations depend on how well-balanced the tree is 1 2 O(log N) balanced 4 3 4 O(N) unbalanced 2 6 5 1 3 5 7 6 7

deletion 40

-since we must maintain the properties of a tree structure, deletion is more complicated than with an array or linked-list -there are three different cases: 1.deleting a leaf node 2.deleting a node with one child subtree 3.deleting a node with two children subtrees -first step of deletion is to find the node to delete -just a regular BST search -BUT, stop at the parent of the node to be deleted 41

case 1: deleting a leaf node DELETE NODE 6 20 -stop at parent node (5) -set parent s reference to null 9 27 current 5 16 2 6 11 19 current.right = null; 10 17

case 1: deleting a leaf node DELETE NODE 6 20 -stop at parent node (5) -set parent s reference to null 9 27 current 5 16 2 11 19 current.right = null; 10 17

case 2: delete node with 1 child DELETE NODE 19 20 -stop at parent node (16) -set parent s reference to node s child -multiple cases depending on which side the child and grandchild are on! 5 9 27 16 current current.right = current.right.left; 2 6 11 10 17 19

case 2: delete node with 1 child DELETE NODE 19 20 -stop at parent node (16) -set parent s reference to node s child -multiple cases depending on which side the child and grandchild are on! 5 9 27 16 current 2 6 11 17 current.right = current.right.left; 10

case 3: delete node with 2 children current 20 DELETE NODE 9 -stop at parent node (20) -replace the node with smallest item in its right subtree(10) 5 9 27 16 2 6 11 19 10 17

case 3: delete node with 2 children current 20 DELETE NODE 9 -stop at parent node (20) -replace the node with smallest item in its right subtree(10) -perform a delete on on successor (10) 5 10 27 16 2 6 11 19 10 17

case 3: delete node with 2 children current 20 DELETE NODE 9 -stop at parent node (20) -replace the node with smallest item in its right subtree(10) -perform a delete on successor (10) (guaranteed not to have a left child! case 1 or 2 ) 10 27 5 16 2 6 11 19 17

case 3: delete node with 2 children Replacing the deleted node with the smallest child in right subtree guarantees the BST structure why? current 20 10 27 The smallest item in the right subtree is greater than any item in the left subtree, and smaller than any item in the new right subtree. 5 2 6 11 16 19 17

deletion performance what is the cost of deleting a node from a BST? -first, find the node we want to delete: O(log N) -cost of: -case 1 (delete leaf): set a single reference to null: O(1) -case 2 (delete node with 1 child): bypass a reference: O(1) -case 3 (delete node with 2 children): O(log N) Find the successor: delete the duplicate successor: O(1) 50

some other useful properties -how can we find the smallest node? -start at the root, go as far left as possible -O(log N) on a balanced tree -how can we find the largest node? -start at the root, go as far right as possible -O(log N) on a balanced tree -how can we print nodes in ascending order? -in-order traversal 51

quick review 52

d which of the following trees is the result of adding c to this bst? b e a A) B) C) d d d b e b e b e a c a c a c 53

what will 5 s left child be after deleting 2? A) 3 B) 6 C) null 9 5 16 2 6 11 19 3 12 54

what node will replace 9 after deleting 9? A) 6 B) 10 C) 13 D) 19 9 5 16 2 6 12 19 3 10 13 55 11

what node will replace 5 after deleting 5? A) 2 B) 3 C) 6 D) 12 9 5 16 2 6 11 19 3 12 56

next time 57

-reading -chapter 14 in book -homework -assignment 7 due tonight -assignment 8 out later today 58