CS 106X Lecture 18: Trees

Similar documents
CS24 Week 8 Lecture 1

Binary Trees

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

Programming II (CS300)

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Binary Trees, Binary Search Trees

BBM 201 Data structures

Why Do We Need Trees?

CSE 214 Computer Science II Introduction to Tree

TREES. Trees - Introduction

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

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

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

CSC148 Week 6. Larry Zhang

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

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

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

Chapter 20: Binary Trees

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

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

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.

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

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

Programming Abstractions

CMSC 341 Leftist Heaps

CS 206 Introduction to Computer Science II

11 TREES DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Trees. Eric McCreath

Programming Abstractions

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

LECTURE 11 TREE TRAVERSALS

CMSC 341 Lecture 15 Leftist Heaps

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

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

CMSC 341 Lecture 15 Leftist Heaps

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

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

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Binary Trees. Height 1

Trees. Carlos Moreno uwaterloo.ca EIT

INF2220: algorithms and data structures Series 1

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

Programming II (CS300)

Data Structures and Algorithms

TREES Lecture 12 CS2110 Fall 2016

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

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

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

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

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

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

Trees. CSE 373 Data Structures

Principles of Computer Science

Trees and Graphs. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Topic 14. The BinaryTree ADT

Trees (part 2) CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

TREES Lecture 10 CS2110 Spring2014

Trees. Truong Tuan Anh CSE-HCMUT

Algorithms. Deleting from Red-Black Trees B-Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Trees Algorhyme by Radia Perlman

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

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

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

B-Trees. CS321 Spring 2014 Steve Cutchin

Module 6: Binary Trees

Data Structures And Algorithms

Friday Four Square! 4:15PM, Outside Gates

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Trees, Binary Trees, and Binary Search Trees

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Trees. Trees. CSE 2011 Winter 2007

Data Structures and Algorithms

CSE 214 Computer Science II Heaps and Priority Queues

TREES Lecture 12 CS2110 Spring 2019

Binary Trees. Examples:

Building Java Programs

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

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

BINARY SEARCH TREES. Problem Solving with Computers-II

CSCI Trees. Mark Redekopp David Kempe

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

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

TREES Lecture 12 CS2110 Spring 2018

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

Module 8: Binary trees

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CMSC 341 Lecture 10 Binary Search Trees

Data Structure - Binary Tree 1 -

B-Trees and External Memory

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

CS 206 Introduction to Computer Science II

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

B-Trees and External Memory

Transcription:

CS 106X Lecture 18: Trees Wednesday, February 22, 2017 Programming Abstractions (Accelerated) Winter 2017 Stanford University Computer Science Department Lecturer: Chris Gregg reading: Programming Abstractions in C++, Section 16.1

Today's Topics Logistics Midterm Tomorrow! Midterm will cover up to and including Linked Lists Introduction to Trees

Trees We have already seen trees in the class in the form of decision trees! cart art crt cat car rt at ar rt ct cr at ct ca ar cr ca t r t a r a t r t c r c t a t c a c r a r c a c

Trees You've coded trees for recursive assignments!

Trees Can Describe Hierarchies world China USA Honduras Hunan Shanghai Tegucigalpa California Ohio Changsha SF LA Kent

Trees Can Describe Websites (HTML)

Trees Can Describe Programs * This is a figure in an academic paper written by a recent CS106 student!

Trees are inherently recursive What is a Tree (in Computer Science)? A tree is a collection of nodes, which can be empty. If it is not empty, there is a root node, r, and zero or more non-empty subtrees, T1, T2,, Tk, whose roots are connected by a directed edge from r. A A is the root B C D E F G H I J K L M N O P

What is a Tree (in Computer Science)? Tree Terminology A tree is a collection of nodes, which can be empty. If it is not empty, there is a root node, r, and zero or more non-empty subtrees, T1, T2,, Tk, whose roots are connected by a directed edge from r. A A is the root B is a child of A B C D E F F is a child of A and a parent of K,L,M G H I J K L M N Note: there are N nodes and N-1 edges O P

What is a Tree (in Computer Science)? Tree Terminology A tree is a collection of nodes, which can be empty. If it is not empty, there is a root node, r, and zero or more non-empty subtrees, T1, T2,, Tk, whose roots are connected by a directed edge from r. A A is the root B C D E F G Nodes with no children are called leaves H I O J P K L M N

What is a Tree (in Computer Science)? Tree Terminology A tree is a collection of nodes, which can be empty. If it is not empty, there is a root node, r, and zero or more non-empty subtrees, T1, T2,, Tk, whose roots are connected by a directed edge from r. A A is the root B C D E F G Nodes with the same parent are siblings. H I O J P K L M N

Tree Terminology We can define a path from a parent to its children. A The path A-E-J-O has a length of three (the number of edges) E F G I J K L M N O P

Tree Terminology The depth of a node is the length from the root. The depth of node J is 2. The depth of the root is 0. The height of a node is the longest path from the node to a leaf. The height of node F is 1. The height of all leaves is 0. I A E O J P F K L M G N

Tree Terminology The height of a tree is the height of the root (in this case, the height of the tree is 3. A E F G I J K L M N O P

Tree Terminology Trees can have only one parent, and cannot have cycles

Tree Terminology Trees can have only one parent, and cannot have cycles S N S N T A A F O

Tree Terminology Trees can have only one parent, and cannot have cycles S N S A N T A Node A has two parents Node A has two parents F O

Tree Terminology Trees can have only one parent, and cannot have cycles S S T A T A N N

Tree Terminology Trees can have only one parent, and cannot have cycles S S T A T A N not a tree: the red edges make a cycle N

How can we build trees programmatically?

How can we build trees programmatically? Binary Tree: value

How can we build trees programmatically? Binary Tree: value Linked List value

How can we build trees programmatically? Binary Tree: value Linked List value

How can we build trees programmatically? Binary Tree: value value value Linked List value

How can we build trees programmatically? Binary Tree: value value value Linked List value

The Most Important Slide Binary Tree: struct Tree { string value; Tree *left; Tree *right; }; value

We Can Have Ternary Trees (or any number, n) Ternary Tree: struct Tree { string value; Tree *left; Tree *middle; Tree *right; }; value

We Can Have Ternary Trees (or any number, n) N-ary Tree: struct Tree { string value; Vector<Tree *> children; }; value...

Trees can be defined as either structs or classes struct Tree { string value; Tree * left; Tree * right; }; class Tree { private: string value; Vector<Tree *> children; };

Let's write some code to "traverse" the tree struct Tree { string value; Tree * left; Tree * right; }; There are multiple ways to traverse the nodes in a binary tree: 1.Pre-order 2.In-order 3.Post-order 4.Level-order this is written a correctly sentence.

Let's write some code to "traverse" the tree struct Tree { string value; Tree * left; Tree * right; }; There are multiple ways to traverse the nodes in a binary tree: 1.Pre-order 2.In-order 3.Post-order 4.Level-order 1.Do something 2.Go left 3.Go right this is written a correctly sentence.

Let's write some code to "traverse" the tree struct Tree { string value; Tree * left; Tree * right; }; There are multiple ways to traverse the nodes in a binary tree: 1.Pre-order 2.In-order 3.Post-order 4.Level-order 1.Go left 2.Do something 3.Go right this is written a correctly sentence.

Let's write some code to "traverse" the tree struct Tree { string value; Tree * left; Tree * right; }; There are multiple ways to traverse the nodes in a binary tree: 1.Pre-order 2.In-order 3.Post-order 4.Level-order 1.Go left 2.Go right 3.Do something this is written a correctly sentence.

Let's write some code to "traverse" the tree struct Tree { string value; Tree * left; Tree * right; }; There are multiple ways to traverse the nodes in a binary tree: 1.Pre-order 2.In-order 3.Post-order 4.Level-order is this written a correctly sentence. Hmm...can we do this recursively? We want to print the levels: 0, 1, 2 from left-to-right order

Let's write some code to "traverse" the tree struct Tree { string value; Tree * left; Tree * right; }; There are multiple ways to traverse the nodes in a binary tree: 1.Pre-order 2.In-order 3.Post-order 4.Level-order Not easy recursively...let's use a queue! 1. Enqueue root 2. While queue is not empty: a. dequeue node b. do something with node c. enqueue left child of node if it exists d. enqueue right child of node if it exists is this written a correctly sentence. should look familiar...word ladder?

Let's write some code struct Tree { string value; Tree * left; Tree * right; }; void preorder(tree * tree) { if(tree == NULL) return; cout<< tree->value <<" "; preorder(tree->left); preorder(tree->right); } void inorder(tree * tree) { if(tree == NULL) return; inorder(tree->left); cout<< tree->value <<" "; inorder(tree->right); } void postorder(tree * tree) { if(tree == NULL) return; postorder(tree->left); postorder(tree->right); cout<< tree->value << " "; } is this written a correctly sentence. void levelorder(tree *tree) { Queue<Tree *>treequeue; treequeue.enqueue(tree); while (!treequeue.isempty()) { Tree *node = treequeue.dequeue(); cout << node->value << " "; } } if (node->left!= NULL) { treequeue.enqueue(node->left); } if (node->right!= NULL) { treequeue.enqueue(node->right); }

References and Advanced Reading References: https://en.wikipedia.org/wiki/tree_(data_structure) http://pages.cs.wisc.edu/~vernon/cs367/notes/8.trees.html Advanced Reading: http://www.cs.cmu.edu/~adamchik/15-121/lectures/trees/trees.html Great set of tree-type questions: http://cslibrary.stanford.edu/110/binarytrees.html