Binary search trees (chapters )

Similar documents
Binary search trees (chapters )

TREES. Trees - Introduction

Algorithms. AVL Tree

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

Binary heaps (chapters ) Leftist heaps

Binary Trees, Binary Search Trees

Trees. A tree is a directed graph with the property

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

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

Self-Balancing Search Trees. Chapter 11

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

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

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Balanced Binary Search Trees

Programming II (CS300)

Analysis of Algorithms

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

COMP : Trees. COMP20012 Trees 219

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Data Structures Lesson 7

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

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

Data Structures and Algorithms for Engineers

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Binary Trees

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

COMP171. AVL-Trees (Part 1)

Balanced Search Trees. CS 3110 Fall 2010

Unit III - Tree TREES

Chapter 12 Advanced Data Structures

Data Structures and Algorithms

CS Transform-and-Conquer

AVL trees and rotations

Advanced Tree Data Structures

CSCI2100B Data Structures Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CISC 235: Topic 4. Balanced Binary Search Trees

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

CS 261 Data Structures. AVL Trees

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Friday Four Square! 4:15PM, Outside Gates

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CS350: Data Structures AVL Trees

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

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

CS350: Data Structures B-Trees

Lecture 13: AVL Trees and Binary Heaps

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

CS 206 Introduction to Computer Science II

INF2220: algorithms and data structures Series 1

Programming II (CS300)

CS102 Binary Search Trees

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

Algorithms. Deleting from Red-Black Trees B-Trees

CSCI Trees. Mark Redekopp David Kempe

Binary Search Trees. Analysis of Algorithms

Data Structures in Java

Trees. A tree is a data structure consisting of data nodes connected to each other with pointers: Leaf. Vocabulary

Programming II (CS300)

Garbage Collection: recycling unused memory

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

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

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Trees. Eric McCreath

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

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

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

Binary Search Trees 1

CS 350 : Data Structures B-Trees

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

Lesson 21: AVL Trees. Rotation

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

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

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

AVL Trees. See Section 19.4of the text, p

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

Search Trees: BSTs and B-Trees

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

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

Trees. Prof. Dr. Debora Weber-Wulff

AVL trees and rotations

Section 4 SOLUTION: AVL Trees & B-Trees

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Cpt S 122 Data Structures. Data Structures Trees

Search Structures. Kyungran Kang

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

CS350: Data Structures Red-Black Trees

UNIT III BALANCED SEARCH TREES AND INDEXING

Search Trees - 1 Venkatanatha Sarma Y

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

CSC 421: Algorithm Design Analysis. Spring 2013

LECTURE 18 AVL TREES

Advanced Set Representation Methods

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

Transcription:

Binary search trees (chapters 18.1 18.3)

Binary search trees In a binary search tree (BST), every node is greater than all its left descendants, and less than all its right descendants (recall that this is an invariant) owl hamster penguin gorilla lemur wolf ape

Searching in a BST Finding an element in a BST is easy, because by looking at the root you can tell which subtree the element is in lemur must be in left subtree of owl hamster owl penguin lemur must be in right subtree of hamster gorilla lemur wolf ape

Searching in a binary search tree To search for target in a BST: If the target matches the root node's data, we've found it If the target is less than the root node's data, recursively search the left subtree If the target is greater than the root node's data, recursively search the right subtree If the tree is empty, fail A BST can be used to implement a set, or a map from keys to values

Inserting into a BST To insert a value into a BST: Start by searching for the value But when you get to null (the empty tree), make a node for the value and place it there

Deleting a node with one child Deleting is, which has one child, in we connect in to is's parent jack

Deleting from a BST To delete a value from a BST: Find the node and its parent If it has no children, just remove it from the tree (by disconnecting it from its parent) If it has one child, replace the node with its child (by making the node's parent point at the child) If it has two children...?

Deleting a node with two children Replace the deleted value with the biggest value from its left subtree (or the smallest from the right subtree) [why this one?] Delete house by replacing it with horn Remove horn

Deleting a node with two children Find the node to delete Find the biggest value in the left subtree and put that value in the deleted node Using the biggest value preserves the invariant (check you understand why) Biggest node = rightmost node Finally, delete the biggest value from the left subtree This node can't have two children (no right child), so deleting it is much easier

Deleting a node with two children Deleting rat, we replace it with priest; now we have to delete priest which has a child, morn

Complexity of BST operations All our operations are O(height of tree) This means O(log n) if the tree is balanced, but O(n) if it's unbalanced (like the tree on the right) how might we get this tree? Balanced BSTs add an extra invariant that makes sure the tree is balanced then all operations are O(log n)

Summary of BSTs Binary trees with BST invariant Can be used to implement sets and maps lookup: can easily find a value in the tree insert: perform a lookup, then put the new value at the place where the lookup would terminate delete: find the value, then several cases depending on how many children the node has Complexity: all operations O(height of tree) that is, O(log n) if tree is balanced, O(n) if unbalanced inserting random data tends to give balanced trees, sequential data gives unbalanced ones

Tree traversal Traversing a tree means visiting all its nodes in some order A traversal is a particular order that we visit the nodes in Four common traversals: preorder, inorder, postorder, level-order For each traversal, you can define an iterator that traverses the nodes in that order (see 17.4)

Preorder traversal Visit root node, then left child, then right 1 owl 2 hamster 6 penguin 3 gorilla 5 lemur 7 wolf 4 ape

Postorder traversal Visit left child, then right, then root node 7 owl 4 hamster 6 penguin 2 gorilla 3 lemur 5 wolf 1 ape

Inorder traversal Visit left child, then root node, then right 5 owl 3 hamster 6 penguin 2 gorilla 4 lemur 7 wolf 1 ape

Level-order traversal Visit nodes left to right, top to bottom 1 owl 2 hamster 3 penguin 4 gorilla 5 lemur 6 wolf 7 ape

In-order traversal printing void inorder(node<e> node) { if (node == null) return; inorder(node.left); System.out.println(node.value); inorder(node.value); } But nicer to define an iterator! Iterator<Node<E>> inorder(node<e> node); Level-order traversal is slightly trickier, and uses a queue see 17.4.4

Sorting a binary search tree If we do an inorder traversal of a BST, we get its elements in sorted order! 5 owl 3 hamster 6 penguin 2 gorilla 4 lemur 7 wolf 1 ape

AVL trees (chapter 18.4)

Balanced BSTs: the problem The BST operations take O(height of tree), so for unbalanced trees can take O(n) time

Balanced BSTs: the solution Take BSTs and add an extra invariant that makes sure that the tree is balanced Height of tree must be O(log n) Then all operations will take O(log n) time One possible idea for an invariant: Height of left child = height of right child (for all nodes in the tree) Tree would be sort of perfectly balanced What's wrong with this idea?

A too restrictive invariant Perfect balance is too restrictive! Number of nodes can only be 1, 3, 7, 15, 31,... owl hamster penguin gorilla lemur panda wolf

AVL trees a less restrictive invariant The AVL tree is the first balanced BST discovered (from 1962) it's named after Adelson-Velsky and Landis It's a BST with the following invariant: The difference in heights between the left and right children of any node is at most 1 This makes the tree's height O(log n), so it's balanced

AVL trees We call the quantity right height left height of a node its balance Thus the AVL invariant is: the balance of every node is -1,, or 1 Whenever a node gets out of balance, we fix it with so-called tree rotations (next) (Implementation: store the balance of each node as a field in the node, and remember to update it when updating the tree)

Rotation Rotation rearranges a BST by moving a different node to the root, without changing the BST's contents (pic from Wikipedia)

Rotation We can use rotations to adjust the relative height of the left and right branches of a tree Height of 3 Height of 4

AVL insertion Start by doing a BST insertion This might break the AVL (balance) invariant Then go upwards from the newly-inserted node, looking for nodes that break the invariant (unbalanced nodes) Whenever you find one, rotate it Then continue upwards in the tree There are several cases depending on how the node is unbalanced

Case 1: a left-left tree 5 25 a b The purple represents an insertion that has increased the height of tree a to k+1 c Each pink triangle represents an AVL tree with height k

Case 1: a left-left tree Height k+2 5 Height k 25 c a b The tree as a whole has a balance of -2: invariant broken!

Case 1: a left-left tree 25 5 This is called a left-left tree because both the root and the left child are deeper on the left c a b To fix it we do a right rotation

Balancing a left-left tree, afterwards Height k+1 25 Height k+1 5 a b c

Balancing a left-left tree, afterwards 25 5 Invariant restored! Notice that now the insertion didn't change the height of the tree a b c

Case 2: a right-right tree 25 5 Mirror image of left-left tree Can be fixed with left rotation a b c

Case 3: a left-right tree Height k+2 5 Height k 25 c a b The tree as a whole has a balance of -2: invariant broken!

Case 3: a left-right tree 5 25 c a b We can't fix this with one rotation Let's look at b's subtrees b L and b R

Case 3: a left-right tree 5 25 4 c a b L b R Rotate 25-subtree to the left

Case 3: a left-right tree Height k+2 5 Height k Height k+1 4 25 b R c a b L We now have a left-left tree! So we can fix it by rotating the whole tree to the right

Case 3: a left-right tree 4 Balanced! Notice it works whichever of b L and b R has the extra height 25 5 b L b R a c

Case 4: a right-left tree 25 Mirror image of left-right tree 5 a b c

Four sorts of unbalanced trees Left-left (root's balance is -2, left child's balance ) Rotate the whole tree to the right Left-right (root's balance is -2, left child's balance > ) First rotate the left child to the left Then rotate the whole tree to the right Right-left (root's balance is 2, right child's balance < ) First rotate the right child to the right Then rotate the whole tree to the left Right-right (root's balance is 2, right child's balance ) Rotate the whole tree to the left

The four cases Left Right Case 2 5-1 D 3 Right Left Case -2 3 A 1 5 A D (picture from Wikipedia) B 4 C B 4 C Left Left Case Right Right Case 2-2 5 3 4 1/ D A -1/ 4 3 C B 5 A B C D Balanced Balanced -1/ 1/ 4 4 3 5 3 5 A B C D A B C D

A bigger example (slides from Peter Ljunglöf) Let's build an AVL tree for the words in The quick brown fox jumps over the lazy dog 43

The quick brown The +2 quick -1 brown The overall tree is rightheavy (Right-Left) parent balance = +2 right child balance = -1 44

The quick brown The +2 quick -1 brown 1. Rotate right around the child 45

The quick brown The +2 brown +1 quick 1. Rotate right around the child 46

The quick brown The +2 brown +1 quick 1. Rotate right around the child 2. Rotate left around the parent 47

The quick brown brown The quick 1. Rotate right around the child 2. Rotate left around the parent 48

The quick brown fox The brown quick Insert fox 49

The quick brown fox The brown +1 quick -1 Insert fox fox 5

The quick brown fox jumps The brown +1 quick -1 Insert jumps fox 51

The quick brown fox jumps The brown +2 quick -2 Insert jumps fox +1 jumps 52

The quick brown fox jumps brown +2 The quick -2 fox +1 jumps The tree is now leftheavy about quick (Left- Right case) 53

The quick brown fox jumps brown +2 The quick -2 fox +1 jumps 1. Rotate left around the child 54

The quick brown fox jumps brown +2 The quick -2 jumps -1 fox 1. Rotate left around the child 55

The quick brown fox jumps brown +2 The quick -2 jumps -1 fox 1. Rotate left around the child 2. Rotate right around the parent 56

The quick brown fox jumps brown +1 The jumps fox quick 1. Rotate left around the child 2. Rotate right around the parent 57

The quick brown fox jumps over The brown +1 jumps Insert over fox quick 58

The quick brown fox jumps over The brown +2 jumps +1 Insert over fox quick -1 over 59

The quick brown fox jumps over brown +2 The jumps +1 fox quick -1 over We now have a Right- Right imbalance 6

The quick brown fox jumps over brown +2 The jumps +1 fox quick -1 over 1. Rotate left around the parent 61

The quick brown fox jumps over jumps brown quick -1 The fox over 1. Rotate left around the parent 62

The quick brown fox jumps over the brown jumps quick -1 Insert the The fox over 63

The quick brown fox jumps over the brown jumps quick Insert the The fox over the 64

e quick brown fox jumps over the lazy brown jumps quick Insert lazy The fox over the 65

e quick brown fox jumps over the lazy brown jumps +1 quick -1 Insert lazy The fox over -1 the lazy 66

quick brown fox jumps over the lazy dog brown jumps +1 quick -1 Insert dog The fox over -1 the lazy 67

quick brown fox jumps over the lazy dog! brown +1 jumps quick -1 Insert dog The fox -1 over -1 the dog lazy 68

AVL trees A balanced BST that maintains balance by rotating the tree Insertion: insert as in a BST and move upwards from the inserted node, rotating unbalanced nodes Deletion (in book if you're interested): delete as in a BST and move upwards from the node that disappeared, rotating unbalanced nodes Worst-case (it turns out) 1.44log n, typical log n comparisons for any operation very balanced. This means lookups are quick. Insertion and deletion can be slower than in a naïve BST, because you have to do a bit of work to repair the invariant Look in Haskell compendium (course website) for implementation