CS : Data Structures

Similar documents
CS : Data Structures

CS : Data Structures

CS : Data Structures Michael Schatz. Oct Lecture 15. Graphs

CS : Data Structures Michael Schatz. Oct Lecture 18. Midterm review 2

Programming II (CS300)

Chapter 20: Binary Trees

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. Hierarchical data structures: AVL tree, Bayer tree, Heap

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

March 20/2003 Jayakanth Srinivasan,

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

CS24 Week 8 Lecture 1

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Binary Trees

Data Structures and Algorithms

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

TREES. Trees - Introduction

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

COMP 250 Fall binary trees Oct. 27, 2017

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

BBM 201 Data structures

Binary Trees, Binary Search Trees

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Data Structures - Binary Trees 1

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

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

Trees. Truong Tuan Anh CSE-HCMUT

CS : Data Structures

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Binary Tree Applications

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Tree Structures. A hierarchical data structure whose point of entry is the root node

CS 350 : Data Structures B-Trees

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

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

Data Structures and Algorithms for Engineers

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

INF2220: algorithms and data structures Series 1

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

CS350: Data Structures B-Trees

CS : Data Structures

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

Trees. Trees. CSE 2011 Winter 2007

CE 221 Data Structures and Algorithms

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

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Introduction to Computers and Programming. Concept Question

Friday Four Square! 4:15PM, Outside Gates

Algorithms and Data Structures

CS : Data Structures

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

Analysis of Algorithms

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Associate Professor Dr. Raed Ibraheem Hamed

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

Programming II (CS300)

CSE 230 Intermediate Programming in C and C++ Binary Tree

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

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

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

INF2220: algorithms and data structures Series 1

Binary Search Trees. See Section 11.1 of the text.

CMSC th Lecture: Graph Theory: Trees.

Self-Balancing Search Trees. Chapter 11

CS Transform-and-Conquer

tree nonlinear Examples

Trees Algorhyme by Radia Perlman

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

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

Topic 14. The BinaryTree ADT

Programming II (CS300)

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

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

Advanced Tree Data Structures

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Why Do We Need Trees?

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

ITEC2620 Introduction to Data Structures

COMP : Trees. COMP20012 Trees 219

CSC148 Week 6. Larry Zhang

CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, THE TREE TRAVERSALS, B TREES WEEK - 7

COMP 250 Fall Solution - Homework #4

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

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Garbage Collection: recycling unused memory

Data Structures Question Bank Multiple Choice

Cpt S 122 Data Structures. Data Structures Trees

CS200: Balanced Search Trees

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

CS 206 Introduction to Computer Science II

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Trees. CSE 373 Data Structures

Transcription:

CS 600.226: Data Structures Michael Schatz Oct 12, 2016 Lecture 17: Trees

Assignment 5: Due Sunday Oct 9 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently written!

Midterm Review

Part 2: Trees

Trees are all around us J

Types of Trees Unordered Binary tree Linear List 3-ary Tree (k-ary tree has k children) Single root node (no parent) Each internal node has at most 1 parent Node may have 0 or more children

Not Trees Node 4 has 2 parents Forms a (self) cycle 2 root nodes: Forest Single root node (no parent) Each internal node has at most 1 parent Node may have 0 or more children

Special Trees Height of root = 0 Total Height = 3 Full Binary Tree Every node has 0 or 2 children Complete Binary Tree Every level full, except potentially the bottom level What is the maximum number of leaf nodes in a complete binary tree? What is the maximum number of nodes in a complete binary tree? 2 h+1-1 What fraction of the nodes in a complete binary tree are leaves? about half 2 h

Balancing Trees Balanced Binary Tree Minimum possible height Unbalanced Tree Non-minimum height What is the height of a balanced binary tree? What is the height of a balanced 3-ary tree? What is the height of a k-ary tree? How much taller is a binary tree than a k-ary tree? What is the maximum height of an unbalanced tree? lg n log 3 n log k n lg n/log k n = lg k n-1 L

Tree Interface public interface Tree<T>{ Position <T> insertroot(t t ) throws TreeNotEmptyException; Why insertroot? Position <T> insertchild(position <T> p, T t) throws InvalidPositionException; boolean empty(); Position <T> root() throws TreeEmptyException; Position <T>[] children(position <T> p) Why children()? throws InvalidPositionException, LeafException; Position <T> parent(position<t> p) throws InvalidPositionException ; boolean leaf (Position <T> p) throws InvalidPositionException ; What should parent(root()) return? } T remove(position<t> p) What should remove(root()) do? throws InvalidPositionException, NotALeafException;

Tree Traversals + 1 * 2 3

Tree Traversals + 1 * 2 3

Tree Traversals + 1 * 2 3 Note here we visit children from left to right, but could go right to left Notice we visit internal nodes 3 times: 1: stepping down from parent 2: after visiting first child 3: after visiting second child Different algs work at different times 1: preorder 2: inorder 3: postorder

Tree Traversals + 1 * 2 3 Note here we visit children from left to right, but could go right to left Notice we visit internal nodes 3 times: 1: stepping down from parent 2: after visiting first child 3: after visiting second child Different algs work at different times 1: preorder + 1 * 2 3 2: inorder 3: postorder

Tree Traversals + 1 * 2 3 Note here we visit children from left to right, but could go right to left Notice we visit internal nodes 3 times: 1: stepping down from parent 2: after visiting first child 3: after visiting second child Different algs work at different times 1: preorder + 1 * 2 3 2: inorder 1 + 2 * 3 3: postorder

Tree Traversals + 1 * 2 3 Note here we visit children from left to right, but could go right to left Notice we visit internal nodes 3 times: 1: stepping down from parent 2: after visiting first child 3: after visiting second child Different algs work at different times 1: preorder + 1 * 2 3 2: inorder 1 + 2 * 3 3: postorder 1 2 3 * +

Traversal Implementations How to traverse? B A C D basictraversal(node n): // just entered from top basictraversal(n.left) // just got back from left basictraversal(n.middle) // just got back from middle basictraversal(n.right) // just got back from right return // leave to top E F G H I J K L

InOrder Traversals How to inorder print? B A C InOrderTraversal(Node n): if n is not null InOrderTraversal(n.left) print(n) InOrderTraversal(n.right) D E F G H I J K L

PostOrder Traversals How to postorder print? A PostOrderTraversal(Node n): for c in x.children: PostOrderTraversal(c) print(n) B C D E F G H I J K L

PreOrder Traversals [A] [D,C,B] [D,C,F,E] [D,C,F] [D,C,J] [D,C] [D] [I, H, G] B A C D How to preorder print? PreOrderTraversal(Node n): Stack s s.push(n) while (!s.empty()): Node x = s.pop() print(x) for c in x.children: s.push(c) E F G H I J K L

Next Steps 1. Reflect on the magic and power of Trees! 2. Take a break from homework J

Welcome to CS 600.226 http://www.cs.jhu.edu/~cs226/ Questions?