Lecture 12 TREES II CS2110 Spring 2018

Similar documents
Announcements TREES II. Comparing Data Structures. Binary Search Trees. Red-Black Trees. Red-Black Trees 3/13/18

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

syntax tree - * * * * * *

TREES Lecture 12 CS2110 Spring 2018

TREES Lecture 12 CS2110 Spring 2019

TREES Lecture 12 CS2110 Fall 2016

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

TREES Lecture 10 CS2110 Spring2014

TREES. Trees - Introduction

Data Structures and Algorithms

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Binary Trees, Binary Search Trees

MIDTERM WEEK - 9. Question 1 : Implement a MyQueue class which implements a queue using two stacks.

An Introduction to Trees

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Week 2. CS 400 Programming III. Read: Module 2 readings before lecture

Stacks, Queues and Hierarchical Collections

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

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

3137 Data Structures and Algorithms in C++

CS24 Week 8 Lecture 1

Tree traversals and binary trees

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

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

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

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

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CMPT 225. Red black trees

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

12 Abstract Data Types

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Programming II (CS300)

Total Score /1 /20 /41 /15 /23 Grader

Summer Final Exam Review Session August 5, 2009

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

Formal Languages and Automata Theory, SS Project (due Week 14)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 380 ALGORITHM DESIGN AND ANALYSIS

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Data Structures in Java

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

Recursive Data Structures and Grammars

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

CS W3134: Data Structures in Java

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Recitation 9. Prelim Review

ECE 242. Data Structures

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

Data Structures Question Bank Multiple Choice

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Largest Online Community of VU Students

Search Trees: BSTs and B-Trees

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

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

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.

Data Structures and Algorithms for Engineers

CSE373 Fall 2013, Midterm Examination October 18, 2013

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

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

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Problem with Scanning an Infix Expression

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

CS2383 Programming Assignment 3

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

CS102 Binary Search Trees

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

ITI Introduction to Computing II

Postfix (and prefix) notation

Major2 Examination Term: 162 Year: 2016/2017

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

CS171 Final Practice Exam

LECTURE 11 TREE TRAVERSALS

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

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

CS 216 Exam 1 Fall SOLUTION

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Transcription:

TREES II Lecture CS0 Spring 08

Announcements Prelim is Tonight, bring your student ID 5:30PM EXAM OLH55: netids starting aa to dh OLH55: netids starting di to ji PHL0: netids starting jj to ks (Plus students who switched from the 7:30 exam) 7:30PM EXAM (34 Students) OLH55: netids starting kt to rz OLH55: netids starting sa to wl PHL0: netids starting wm to zz (Plus students who switched from the 5:30 exam)

Comparing Data Structures 3 Data Structure add(val x) lookup(int i) Array 3 0 Linked List 3 0 Binary Tree BST O(n) O() O() O(n) search(val x) O(n) O(n) 3 O() O(n) O(n) 3 O(height) O(height) O(height)

Binary Search Trees 4 january april february march august april june may december august july september february december november october january

Red-Black Trees 5 Self-balancing BST Each node has one extra bit of information "color" Constraints on how nodes can be colored enforces approximate balance 0 3 5

Red-Black Trees 6 ) A red-black tree is a binary search tree. ) Every node is either red or black. 3) The root is black. 4) If a node is red, then its (non-null) children are black. 5) For each node, every path to a decendant null node contains the same number of black nodes.

RB Tree Quiz 7 Which of the following are red-black trees? A) B) C) D) 0 3 0 3 0 3 0 3 5 5 YES NO YES NO 6 4 6

Class for a RBNode 8 class RBNode<T> { private T value; private Color color; private RBNode<T> parent; private RBNode<T> left, right; Null if the node is the root of the tree. Either might be null if the subtree is empty. /** Constructor: one-node tree with value x */ public RBNode (T v, Color c) { value= d; color= c; } }...

Insert 9 Insert(RBTree t, int v){ Node p; Node n= t.root; while(n!= null){ p= n; if(v < n.value){n= n.left} else{n= n.right} } Node vnode= new Node(v, RED) if(p == NULL){ t.root= vnode; } else if(v < p.value){ p.left= vnode; vnode.parent= p; } else{ p.right= vnode; vnode.parent= p; } fixtree(t, vnode); } 0 3 p n 5 6 8

fixtree 0 3 4 3 4 5 3 5 5 3 5 6 6 4 6 Case : parent is black Case : parent is red uncle is black node on outside Case 3: parent is red uncle is black node on inside Case 4: parent is red uncle is red

Rotations p leftrotate n n rightrotate p 0 0

fixtree fixtree(rbtree t, RBNode n){ while(n.parent.color == RED){ // not Case if(n.parent.parent.right == n.parent){ Node uncle = n.parent.parent.left; if(uncle.color == BLACK) { // Case or 3 if(n.parent.left == n) { rightrotate(n);} //3 n.parent.color== BLACK; n.parent.parent.color= RED; leftrotate(n.parent.parent); } else { //uncle.color == RED // Case 4 n.parent.color= BLACK; uncle.color= BLACK; n.parent.parent.color= RED; n= n.parent.parent; } } else {...} // n.parent.parent.left == n.parent } t.root.color == BLACK;// fix root }

Search 3 Red-black trees are a special case of binary search trees Search works exactly the same as in any BST Time: O(height) 0 3 5 6

What is the max height? 4 Observation : Every binary tree must have a null node with depth log n +

What is the max height? 5 Observation : Every binary tree must have a null node with depth log n + n log(n+).584 3 4.3 5.584 6.807 7 3 8 3.69 9 3.3 0 3.49 3 34 5 6 7

What is the max height? 6 Observation : Every binary tree must have a null node with depth log n + Observation : In a red-black tree, the number of red nodes in a path from the root to a null node is less than or equal to the number of black nodes. 5 3

What is the max height? 7 Observation : Every binary tree must have a null node with depth log n + Observation : In a red-black tree, the number of red nodes in a path from the root to a null node is less than or equal to the number of black nodes. Observation 3: The maximum path length from the root to a null node is at most times the minimum path length from the root to a null node.

What is the max height? 8 Observation : Every binary tree must have a null node with depth log n + Observation : In a red-black tree, the number of red nodes in a path from the root to a null node is less than or equal to the number of black nodes. Observation 3: The maximum path length from the root to a null node is at most times the minimum path length from the root to a null node. h = max 4556 89:: path len min 4556 89:: path len log (n + ) h is O(log n)

Comparing Data Structures 9 Data Structure add(val x) lookup(int i) Array 3 0 Linked List 3 0 Binary Tree BST RB Tree O(n) O() O() O(n) search(val x) O(n) O(n) 3 O() O(n) O(n) 3 O(height) O(height) O(height) 3 O(log n) O(log n) O(log n)

Application of Trees: Syntax Trees 0 Most languages (natural and computer) have a recursive, hierarchical structure This structure is implicit in ordinary textual representation Recursive structure can be made explicit by representing sentences in the language as trees: Abstract Syntax Trees (ASTs) ASTs are easier to optimize, generate code from, etc. than textual representation A parser converts textual representations to AST

Applications of Trees: Syntax Trees parsing * ( + 0) A Java expression as a string. * - + 0 An expression as a tree.

Pre-order, Post-order, and In-order * - + 0 Pre-order traversal:. Visit the root. Visit the left subtree (in pre-order) 3. Visit the right subtree - * + 0

Pre-order, Post-order, and In-order 3 * - + 0 Pre-order traversal - * + 0 Post-order traversal. Visit the left subtree (in post-order). Visit the right subtree 3. Visit the root * 0 + -

Pre-order, Post-order, and In-order 4 * - + 0 Pre-order traversal Post-order traversal - * + 0 * 0 + - In-order traversal. Visit the left subtree (in-order). Visit the root 3. Visit the right subtree * - + 0

Pre-order, Post-order, and In-order 5 * - + 0 Pre-order traversal Post-order traversal - * + 0 * 0 + - In-order traversal ( * ) - ( + 0) To avoid ambiguity, add parentheses around subtrees that contain operators.

Printing contents of BST (In-Order Traversal) 6 Because of ordering rules for a BST, it s easy to print the items in alphabetical order Recursively print left subtree Print the node Recursively print right subtree /** Print BST t in alpha order */ private static void print(treenode<t> t) { if (t== null) return; print(t.left); System.out.print(t.value); print(t.right); }

In Defense of Postfix Notation 7 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. * 0 + -

In Defense of Postfix Notation 8 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. * 0 + -

In Defense of Postfix Notation 9 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. * 0 + -

In Defense of Postfix Notation 30 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. 0 + -

In Defense of Postfix Notation 3 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. 0 + -

In Defense of Postfix Notation 3 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. -

In Defense of Postfix Notation 33 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack.

In Defense of Postfix Notation 34 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. In about 974, Gries paid $300 for an HP calculator, which had some memory and used postfix notation! Still works. a.k.a. reverse Polish notation

In Defense of Prefix Notation 35 Function calls in most programming languages use prefix notation: like add(37, 5). Some languages (Lisp, Scheme, Racket) use prefix notation for everything to make the syntax simpler. (define (fib n) (if (<= n ) (+ (fib (- n ) (fib (- n )))))

Iterator/Iterable 36 There's a pair of Java interfaces designed to make data structures easy to traverse You could modify a tree to implement iterable, implement an (in-order, post-order, etc.) iterator and then use a for each loop to traverse the tree! In recitation this week, you will modify your linked list from A3 to implement iterable