CS24 Week 8 Lecture 1

Similar documents
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Binary Trees

Programming II (CS300)

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

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

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

TREES. Trees - Introduction

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

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

LECTURE 11 TREE TRAVERSALS

Trees Algorhyme by Radia Perlman

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Binary Trees, Binary Search Trees

Trees. Truong Tuan Anh CSE-HCMUT

Name CPTR246 Spring '17 (100 total points) Exam 3

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

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

Algorithms. Deleting from Red-Black Trees B-Trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1


CmpSci 187: Programming with Data Structures Spring 2015

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

CS 106X Lecture 18: Trees

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

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

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

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

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

Friday Four Square! 4:15PM, Outside Gates

Stacks, Queues and Hierarchical Collections

BBM 201 Data structures

CS350: Data Structures Red-Black Trees

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

Programming Abstractions

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Search Trees - 1 Venkatanatha Sarma Y

Garbage Collection: recycling unused memory

Associate Professor Dr. Raed Ibraheem Hamed

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

Trees. Eric McCreath

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

TREES Lecture 12 CS2110 Spring 2018

Why Do We Need Trees?

Chapter 20: Binary Trees

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

CS171 Final Practice Exam

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

CS 151. Binary Trees. Friday, October 5, 12

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

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

TREES Lecture 12 CS2110 Fall 2016

SFU CMPT Lecture: Week 9

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Tree traversals and binary trees

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Multi-Way Search Tree

Self-Balancing Search Trees. Chapter 11

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

CMPT 225. Binary Search Trees

Data Structures and Algorithms

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Trees and Tree Traversal

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

Trees. CSE 373 Data Structures

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

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

Topic 14. The BinaryTree ADT

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

tree nonlinear Examples

CS302 - Data Structures using C++

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

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

24-Oct-18. Lecture No.08. Trace of insert. node 17, 9, 14, 5. p->setright( node );

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Data Structures Question Bank Multiple Choice

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

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Harald Gall, Prof. Dr.

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

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Data Structure Advanced

Trees. Tree Structure Binary Tree Tree Traversals

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

CSC148H Week 8. Sadia Sharmin. July 12, /29

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

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

INF2220: algorithms and data structures Series 1

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

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

TREES Lecture 10 CS2110 Spring2014

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

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Data Structures And Algorithms

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Transcription:

CS24 Week 8 Lecture 1 Kyle Dewey

Overview Tree terminology Tree traversals Implementation (if time)

Terminology

Node The most basic component of a tree - the squares

Edge The connections between nodes - the arrows

Parent / Child A parent is the predecessor of a node A child is the successor of a node Not all nodes have parents Not all nodes have children (parent of 3 and 12) (child of ) (child of )

Leaf / Terminal Node A node without any children

Leaf / Terminal Node A node without any children (leaf) (leaf)

Internal Node A node with at least one child

Internal Node A node with at least one child (internal node)

Root Node Node without any parent Often drawn as the topmost node

Height and Depth Height: The number of edges on the longest path from a node to a leaf Depth: the number of edges between a node and the root node

Height and Depth http://stackoverflow.com/questions/2603692/what-is-the-difference-between-tree-depth-and-height

Level All the nodes of a tree which have the same depth

Level All the nodes of a tree which have the same depth level 0 level 1 level 2

k-ary Tree A tree where each node can have between 0 and k children What is k for a binary tree?

k-ary Tree A tree where each node can have between 0 and k children What is k for a binary tree? - 2

Full k-ary Tree All nodes have either 0 or k children

Complete k-ary Tree Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right 0 4

Complete k-ary Tree Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right ok 0 4

Complete k-ary Tree Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right 0 10

Complete k-ary Tree Like a full k-ary tree, except the last level is permitted to be missing nodes, but only on the right not ok 0 10

Balanced Tree For all nodes, the height of the left and right subtrees differ by no more than one

Balanced Tree For all nodes, the height of the left and right subtrees differ by no more than one ok

Balanced Tree For all nodes, the height of the left and right subtrees differ by no more than one 0 15

Balanced Tree For all nodes, the height of the left and right subtrees differ by no more than one ok 0 15

Balanced Tree For all nodes, the height of the left and right subtrees differ by no more than one 12 10 15

Balanced Tree For all nodes, the height of the left and right subtrees differ by no more than one not ok 12 10 15

Subtree Nearly synonymous with node We recursively defined the tree to be either a node with an element and two children, or an empty tree (NULL) Generally refers to some subcomponent of a larger tree, including recursive subcomponents

Subtree Example Can refer to 3 and its children Cannot just refer to 3

Traversals

Traversals For many tree-related problems, the order in which nodes are processed can have a huge impact Two basic kinds: breadth-first search and depth-first search

Breath-First Search (BFS) Tree is traversed as if nodes were words on a page (top to bottom, left to right)

Breath-First Search (BFS) Tree is traversed as if nodes were words on a page (top to bottom, left to right) 1 2 3 4 5 6

Implementing BFS Question: how might we implement BFS? Hint: you ll need a data structure you ve implemented before 1 2 3 4 5 6

Implementing BFS Idea: put nodes on a queue Visit nodes according to the queue order When we are done with a node, put its children onto the end of the queue

Implementing BFS root Queue: <<empty>>

Implementing BFS root Put root on the queue first (this is the node, not just the Queue: number)

Implementing BFS root Now dequeue Queue:

Implementing BFS root 1 Now dequeue Queue:

Implementing BFS root 1 Now dequeue Queue: <<empty>>

Implementing BFS root 1 Now put on the child nodes Queue: <<empty>>

Implementing BFS root 1 Now put on the child nodes Queue: 3, 12

Implementing BFS root 1 Repeat Queue: 3, 12

Implementing BFS root 1 2 Queue: 3, 12

Implementing BFS root 1 2 Queue: 12

Implementing BFS root 1 2 Queue: 12, 0, 4

Implementing BFS root 1 2 Queue: 12, 0, 4

Implementing BFS root 1 2 3 Queue: 12, 0, 4

Implementing BFS root 1 2 3 Queue: 0, 4

Implementing BFS root 1 2 3 Queue: 0, 4, 10, 15

Implementing BFS root 1 2 3 Queue: 0, 4, 10, 15

Implementing BFS root 1 2 3 4 Queue: 0, 4, 10, 15

Implementing BFS root 1 2 3 4 Queue: 4, 10, 15

Implementing BFS root 1 2 3 4 5 Queue: 4, 10, 15

Implementing BFS root 1 2 3 4 5 Queue: 10, 15

Implementing BFS root 1 2 3 4 5 6 Queue: 10, 15

Implementing BFS root 1 2 3 4 5 6 Queue: 15

Implementing BFS root 1 2 3 4 5 6 Queue: 15

Implementing BFS root 1 2 3 4 5 6 Queue: <<empty>>

Depth-First Search (DFS) Favor going down towards the left first 1 2 5 3 4 6

Implementing DFS Question: how might we implement DFS? Hint: you ll need a data structure you ve implemented before 1 2 5 3 4 6

Implementing DFS Idea: put nodes on a stack Visit nodes according to the stack order When we are done with a node, push its children onto the top of the stack

Implementing DFS root Stack: <<empty>>

Implementing DFS root Stack:

Implementing DFS root Stack:

Implementing DFS root 1 Stack:

Implementing DFS root 1 Stack: <<empty>>

Implementing DFS root 1 Stack: 3, 12

Implementing DFS root 1 Stack: 3, 12

Implementing DFS root 1 2 Stack: 3, 12

Implementing DFS root 1 2 Stack: 12

Implementing DFS root 1 2 Stack: 0, 4, 12

Implementing DFS root 1 2 Stack: 0, 4, 12

Implementing DFS root 1 2 3 Stack: 0, 4, 12

Implementing DFS root 1 2 3 Stack: 4, 12

Implementing DFS root 1 2 3 4 Stack: 4, 12

Implementing DFS root 1 2 3 4 Stack: 12

Implementing DFS root 1 2 5 3 4 Stack: 12

Implementing DFS root 1 2 5 3 4 Stack: <<empty>>

Implementing DFS root 1 2 5 3 4 Stack: 10, 15

Implementing DFS root 1 2 5 3 4 Stack: 10, 15

Implementing DFS root 1 2 5 3 4 6 Stack: 10, 15

Implementing DFS root 1 2 5 3 4 6 Stack: 15

Implementing DFS root 1 2 5 3 4 6 Stack: 15

Implementing DFS root 1 2 5 3 4 6 Stack: <<empty>>

On Using Stacks We can cut out the explicit stack by using the call stack implicitly via recursion void traverse(node* current) { if (current!= NULL) { traverse(current->getleft()); traverse(current->getright()); } }

Specific Kinds of DFS Traversals Depending on when we process the current node, there are three general kinds of DFS traversals: Pre-order: process current first In-order: process current between left and right Post-order: process current after left and right

Pre-Order Traversal void traverse(node* current) { if (current!= NULL) { process(current); traverse(current->getleft()); traverse(current->getright()); } }

In-Order Traversal void traverse(node* current) { if (current!= NULL) { traverse(current->getleft()); process(current); traverse(current->getright()); } }

Post-Order Traversal void traverse(node* current) { if (current!= NULL) { traverse(current->getleft()); traverse(current->getright()); process(current); } }

Using Traversals Say we want to print out the contents of a binary search tree in sorted order What kind of traversal should we use?

Using Traversals Say we want to print out the contents of a binary search tree in sorted order What kind of traversal should we use? - inorder

Using Traversals Say we want to delete a binary search tree Which traversal is best?

Using Traversals Say we want to delete a binary search tree Which traversal is best? - post-order