Lecture 9: Intro to Trees

Slides:



Advertisements
Similar presentations
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Advertisements

Trees Types and Operations
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Building Java Programs Binary Search Trees; TreeSet.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
Binary Search Trees (BST)
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.
"Teachers open the door, but you must enter by yourself. "
Lecture 19: Binary Trees reading: 17.1 – 17.3
Part-D1 Binary Search Trees
AA Trees.
Data Structures – LECTURE Balanced trees
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSE 373 Binary search trees; tree height and balance
Search Trees.
Building Java Programs
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
Binary Search Trees.
UNIT III TREES.
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Introduction Applications Balance Factor Rotations Deletion Example
Lecture 22 Binary Search Trees Chapter 10 of textbook
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Tree data structure.
i206: Lecture 13: Recursion, continued Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Building Java Programs
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
AVL Trees CENG 213 Data Structures.
Data Structures and Algorithms
Tree data structure.
Data Structures and Algorithms
Data Structures and Algorithms
AVL Trees: AVL Trees: Balanced binary search tree
Search Sorted Array: Binary Search Linked List: Linear Search
B-Tree Insertions, Intro to Heaps
CSE 373: Data Structures and Algorithms
"Teachers open the door, but you must enter by yourself. "
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Lecture 21: Binary Search Trees; TreeSet
CSE 373 Data Structures and Algorithms
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
AVL Tree Chapter 6 (cont’).
Lecture 10: BST and AVL Trees
Trees Trees.
1 Lecture 13 CS2013.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Lecture 9: Intro to Trees CSE 373: Data Structures and Algorithms CSE 373 19 SP - Kasey Champion

Warm Up + C T(n/2) T(n/2) + C + C Extra Credit: 1. Write a recurrence for this piece of code (assume each node has exactly or 2 children) private IntTreeNode doublePositivesHelper(IntTreeNode node) {     if (node != null) {         if (node.data > 0) {             node.data *= 2;         }         node.left = doublePositivesHelper(node.left);         node.right = doublePositivesHelper(node.right);         return node;     } else {         return null;     } } + C T(n/2) T(n/2) + C + C Extra Credit: Go to PollEv.com/champk Text CHAMPK to 22333 to join session, text “1” or “2” to select your answer 𝑇 𝑛 = 𝐶 1 𝑤ℎ𝑒𝑛 𝑛<1 𝐶 2 + 2𝑇 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 CSE 373 SP 19 - Kasey Champion

Warm Up Continued 𝑇 𝑛 = 𝐶 1 𝑤ℎ𝑒𝑛 𝑛<1 𝐶 2 + 2𝑇 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 𝑇 𝑛 = 𝐶 1 𝑤ℎ𝑒𝑛 𝑛<1 𝐶 2 + 2𝑇 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Level (i) Number of Nodes Input to Node Work per Node 1 2 3 base Input = n 𝑤𝑜𝑟𝑘= 𝐶 2 𝑇 𝑛 2 +𝑇 𝑛 2 + 𝐶 2 𝑇 𝑛 𝑇 𝑛 2 2 +𝑇 𝑛 2 2 +𝐶2 𝑇 𝑛 2 2 +𝑇 𝑛 2 2 +𝐶2 input = n/2 𝑤𝑜𝑟𝑘=𝐶2 input = n/2 𝑤𝑜𝑟𝑘=𝐶2 𝑇 𝑛 2 𝑇 𝑛 2 … … … … … … … … 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 𝐶1 CSE 373 19 sp - Kasey Champion

Warm Up Continued 𝑇 𝑛 = 𝐶 1 𝑤ℎ𝑒𝑛 𝑛<1 𝐶 2 + 2𝑇 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 𝑇 𝑛 = 𝐶 1 𝑤ℎ𝑒𝑛 𝑛<1 𝐶 2 + 2𝑇 𝑛 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Level (i) Number of Nodes Input to Node Work per Node 2 0 =1 𝑛 2 0 =𝑛 𝑐2 1 2 1 =2 𝑛 2 1 = 𝑛 2 2 2 2 =4 𝑛 2 2 = 𝑛 4 3 2 3 =8 𝑛 2 3 = 𝑛 8 base ⇒ 2 log2𝑛 +1 𝐶1 How many nodes on each branch level? How much work for each branch node? How much work per branch level? How many branch levels? How much work for each leaf node? How many leaf nodes? 2 𝑖 𝑐2 2 𝑖 𝐶2 𝑛 2 𝑖 <1⇒ i=log 2 𝑛 Combining it all together… 𝐶1 𝑇 𝑛 = 𝑖=0 log 2 𝑛 2 𝑖 𝐶 2 +2𝑛𝐶1 2 log 2 𝑛 +1 =2𝑛 CSE 373 19 sp - Kasey Champion

Trees CSE 373 SP 18 - Kasey Champion

Storing Sorted Items in an Array get() – O(logn) put() – O(n) remove() – O(n) Can we do better with insertions and removals? CSE 373 SP 18 - Kasey Champion

Review: Trees! A tree is a collection of nodes Each node has at most 1 parent and 0 or more children Root node: the single node with no parent, “top” of the tree Branch node: a node with one or more children Leaf node: a node with no children Edge: a pointer from one node to another Subtree: a node and all it descendants Height: the number of edges contained in the longest path from root node to some leaf node 1 2 5 3 6 7 4 8 CSE 373 SP 18 - Kasey Champion

Tree Height 2 Minutes What is the height of the following trees? overallRoot overallRoot overallRoot 1 7 null 2 5 7 Height = 2 Height = 0 Height = -1 or NA CSE 373 SP 18 - Kasey Champion

Traversals traversal: An examination of the elements of a tree. A pattern used in many tree algorithms and methods Common orderings for traversals: pre-order: process root node, then its left/right subtrees 17 41 29 6 9 81 40 in-order: process left subtree, then root node, then right 29 41 6 17 81 9 40 post-order: process left/right subtrees, then root node 29 6 41 81 40 9 17 Traversal Trick: Sailboat method Trace a path around the tree. As you pass a node on the proper side, process it. pre-order: left side in-order: bottom post-order: right side 40 81 9 41 17 6 29 overallRoot CSE 373 SP 17 – Zora Fung

Binary Search Trees A binary search tree is a binary tree that contains comparable items such that for every node, all children to the left contain smaller data and all children to the right contain larger data. 10 9 15 7 12 18 8 17 CSE 373 SP 18 - Kasey Champion

Implement Dictionary Binary Search Trees allow us to: 10 “foo” quickly find what we’re looking for add and remove values easily Dictionary Operations: Runtime in terms of height, “h” get() – O(h) put() – O(h) remove() – O(h) What do you replace the node with? Largest in left sub tree or smallest in right sub tree 7 “bar” 12 “baz” 5 “fo” 9 “sho” 15 “sup” 1 “burp” 8 “poo” 13 “boo” CSE 373 SP 18 - Kasey Champion

Height in terms of Nodes For “balanced” trees h ≈ logc(n) where c is the maximum number of children Balanced binary trees h ≈ log2(n) Balanced trinary tree h ≈ log3(n) Thus for balanced trees operations take Θ(logc(n)) CSE 373 SP 18 - Kasey Champion

Unbalanced Trees Is this a valid Binary Search Tree? Yes, but… 10 Is this a valid Binary Search Tree? Yes, but… We call this a degenerate tree For trees, depending on how balanced they are, Operations at worst can be O(n) and at best can be O(logn) How are degenerate trees formed? insert(10) insert(9) insert(7) insert(5) 9 7 5 CSE 373 SP 18 - Kasey Champion

Measuring Balance 2 Minutes Measuring balance: For each node, compare the heights of its two sub trees Balanced when the difference in height between sub trees is no greater than 1 8 10 8 7 7 9 15 7 Balanced Balanced Balanced 12 18 Unbalanced CSE 373 SP 18 - Kasey Champion

Meet AVL Trees AVL Trees must satisfy the following properties: binary trees: all nodes must have between 0 and 2 children binary search tree: for all nodes, all keys in the left subtree must be smaller and all keys in the right subtree must be larger than the root node balanced: for all nodes, there can be no more than a difference of 1 in the height of the left subtree from the right. Math.abs(height(left subtree) – height(right subtree)) ≤ 1 AVL stands for Adelson-Velsky and Landis (the inventors of the data structure) CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 7 Is it… Binary BST Balanced? yes yes 4 10 3 5 9 12 2 6 8 11 13 14 CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 2 Minutes 6 Is it… Binary BST Balanced? yes 8 no Height = 0 Height = 2 1 4 7 12 3 5 10 13 9 11 CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 2 Minutes 8 Is it… Binary BST Balanced? yes no 6 11 yes 2 7 15 -1 5 9 9 > 8 CSE 373 SP 18 - Kasey Champion

Implementing an AVL tree dictionary Dictionary Operations: get() – same as BST containsKey() – same as BST put() - ??? remove() - ??? Add the node to keep BST, fix AVL property if necessary Replace the node to keep BST, fix AVL property if necessary Unbalanced! 1 2 2 1 3 3 CSE 373 SP 18 - Kasey Champion

CSE 373 SP 18 - Kasey Champion

Warm Up CSE 373 SP 18 - Kasey Champion

Meet AVL Trees AVL Trees must satisfy the following properties: binary trees: all nodes must have between 0 and 2 children binary search tree: for all nodes, all keys in the left subtree must be smaller and all keys in the right subtree must be larger than the root node balanced: for all nodes, there can be no more than a difference of 1 in the height of the left subtree from the right. Math.abs(height(left subtree) – height(right subtree)) ≤ 1 AVL stands for Adelson-Velsky and Landis (the inventors of the data structure) CSE 373 SP 18 - Kasey Champion

Measuring Balance Measuring balance: For each node, compare the heights of its two sub trees Balanced when the difference in height between sub trees is no greater than 1 8 10 8 7 7 9 15 7 Balanced Balanced Balanced 12 18 Unbalanced CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 7 Is it… Binary BST Balanced? yes yes 4 10 3 5 9 12 2 6 8 11 13 14 CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 2 Minutes 6 Is it… Binary BST Balanced? yes 8 no Height = 0 Height = 2 1 4 7 12 3 5 10 13 9 11 CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 2 Minutes 8 Is it… Binary BST Balanced? yes no 6 11 yes 2 7 15 -1 5 9 9 > 8 CSE 373 SP 18 - Kasey Champion

Implementing an AVL tree dictionary Dictionary Operations: get() – same as BST containsKey() – same as BST put() - ??? remove() - ??? Add the node to keep BST, fix AVL property if necessary Replace the node to keep BST, fix AVL property if necessary Unbalanced! 1 2 2 1 3 3 CSE 373 SP 18 - Kasey Champion

Rotations! Balanced! Unbalanced! a b a Insert ‘c’ X b X b Z a Y Z Y Z CSE 373 SP 18 - Kasey Champion

Rotate Left parent’s right becomes child’s left, child’s left becomes its parent Balanced! Unbalanced! a b a Insert ‘c’ X b X b Z a Y Z Y Z X Y c c CSE 373 SP 18 - Kasey Champion

Rotate Right parent’s left becomes child’s right, child’s right becomes its parent 4 3 15 height put(16); 2 2 3 8 22 Unbalanced! 1 1 2 4 10 19 24 1 3 6 17 20 16 CSE 373 SP 18 - Kasey Champion

Rotate Right parent’s left becomes child’s right, child’s right becomes its parent 15 3 height put(16); 2 2 8 19 1 1 1 10 17 22 4 3 6 16 20 24 CSE 373 SP 18 - Kasey Champion

So much can go wrong Unbalanced! Unbalanced! Unbalanced! 3 1 1 3 3 1 Rotate Left Rotate Right 2 2 2 Parent’s right becomes child’s left Child’s left becomes its parent Parent’s left becomes child’s right Child’s right becomes its parent CSE 373 SP 18 - Kasey Champion

Two AVL Cases Kink Case Line Case Solve with 2 rotations 3 1 1 3 2 2 3 1 1 3 2 2 Rotate Right Parent’s left becomes child’s right Child’s right becomes its parent Rotate Left Parent’s right becomes child’s left Child’s left becomes its parent Right Kink Resolution Rotate subtree left Rotate root tree right Left Kink Resolution Rotate subtree right Rotate root tree left CSE 373 SP 18 - Kasey Champion

Double Rotations 1 Unbalanced! a a a Insert ‘c’ X e W d W e d Z Y e d CSE 373 SP 18 - Kasey Champion

Double Rotations 2 d Unbalanced! a e a W d Y X Z W Y e X Z c c CSE 373 SP 18 - Kasey Champion