Tree traversals and binary trees

Similar documents
Programming II (CS300)

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

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

Figure 1. A breadth-first traversal.

LECTURE 11 TREE TRAVERSALS

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

Binary Trees, Binary Search Trees

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

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

TREES. Trees - Introduction

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

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

Trees. CSE 373 Data Structures

CS24 Week 8 Lecture 1

Chapter 20: Binary Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

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

Algorithms. AVL Tree

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

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

Binary Trees

Garbage Collection: recycling unused memory

An Introduction to Trees

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

CS 206 Introduction to Computer Science II

Data Structures and Algorithms for Engineers

Recursive Data Structures and Grammars

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

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

Multi-Way Search Tree

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

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

March 20/2003 Jayakanth Srinivasan,

Why Do We Need Trees?

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

Trees. Eric McCreath

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

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

IX. Binary Trees (Chapter 10)

Binary Tree Applications

Programming II (CS300)

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

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

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

Topic 14. The BinaryTree ADT

Trees. Truong Tuan Anh CSE-HCMUT

Fall, 2015 Prof. Jungkeun Park

Algorithms. Deleting from Red-Black Trees B-Trees

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

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

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

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

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

Friday Four Square! 4:15PM, Outside Gates

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

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

Binary Search Trees. See Section 11.1 of the text.

CS 206 Introduction to Computer Science II

CE 221 Data Structures and Algorithms

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

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

tree nonlinear Examples

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

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

8. Binary Search Tree

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Data Structures and Algorithms

Stacks, Queues and Hierarchical Collections

Trees. Tree Structure Binary Tree Tree Traversals

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

[ DATA STRUCTURES ] Fig. (1) : A Tree

CSI33 Data Structures

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Building Java Programs

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Advanced Set Representation Methods

Self-Balancing Search Trees. Chapter 11

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

Binary Trees and Binary Search Trees

3 Trees: traversal and analysis of standard search trees

Trees: examples (Family trees)

Dictionaries. 2/17/2006 Dictionaries 1

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

Trees, Part 1: Unbalanced Trees


CS302 - Data Structures using C++

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Tree Data Structures CSC 221

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

Binary Trees Fall 2018 Margaret Reid-Miller

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

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

CSE 326: Data Structures Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CS350: Data Structures Binary Search Trees

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

STUDENT LESSON AB30 Binary Search Trees

Transcription:

Tree traversals and binary trees Comp Sci 1575 Data Structures

Valgrind Execute valgrind followed by any flags you might want, and then your typical way to launch at the command line in Linux. Assuming that you compiled hw.cpp into hw as an executable: v a l g r i n d l e a k check= f u l l v a l g r i n d l e a k check= f u l l. / hw < s a m p l e i n p u t. t x t. / hw Demo a couple examples (varnodec and varnodei)

Outline 1 2

Outline 1 2

Binary tree Set of nodes is either empty or consists of a node called the root together with two binary trees, called the left and right subtrees, which are disjoint from each other and from the root; disjoint means that they have no nodes in common. Each node has at most two children, which are referred to as the left child and the right child. No node can have more than two children. The depth of an average binary tree is considerably smaller than N, the average depth is O(sqrt(N)), and for a special type of binary tree, namely the binary search tree, the average value of the depth is O(log N).

Outline 1 2

Perfect binary tree Binary tree in which all interior nodes have two children and all leaves have the same depth or same level.

Full (left) and complete (right) binary trees Full binary tree Tree in which every node in the tree has either 0 or 2 children. Each node in a full binary tree is either (1) an internal node with exactly two non-empty children (2) a leaf. Complete binary tree Has a restricted shape obtained by starting at the root and filling the tree by levels from left to right. Every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. The bottom level has its nodes filled in from the left side. Can be efficiently represented using an array.

Full and complete binary trees

Full and complete binary trees

Balanced binary trees Has the minimum possible maximum height (a.k.a. depth) for the leaf nodes, because for any given number of leaf nodes, the leaf nodes are placed at the greatest height possible. One common balanced tree structure is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1. One may also consider binary trees where no leaf is much farther away from the root than any other leaf. (Different balancing schemes allow different definitions of much farther.)

Degenerate trees Left A degenerate (or pathological) tree is where each parent node has only one associated child node; performance will behave like a linked list data structure. Right Shape of the binary search tree depends entirely on the order of insertions and deletions, and can become degenerate.

Outline 1 2

: expression trees Expression tree: The leaves are operands, such as constants or variable names, and the other nodes contain operators.

Outline 1 2

Order of list is obvious, tree not so much Any process for visiting all of the nodes in some order is called a traversal. Any traversal that lists every node in the tree exactly once is called an enumeration of the tree s nodes.

Outline 1 2

traversal: parents is visited before the children : F, B, A, D, C, E, G, I, H

1 Check if the current node is empty / null. 2 Display the data part of the root (or current node). 3 Traverse the left subtree by recursively calling the pre-order function. 4 Traverse the right subtree by recursively calling the pre-order function. : F, B, A, D, C, E, G, I, H

1 Process the root 2 Process the nodes in the left subtree with a recursive call 3 Process the nodes in the right subtree with recursive call : F, B, A, D, C, E, G, I, H.

recursive pseudocode algorithm p r e o r d e r ( node ) i f ( node == n u l l ) return v i s i t ( node ) p r e o r d e r ( node. l e f t ) p r e o r d e r ( node. r i g h t ) What is trace on this tree?

iterative pseudocode algorithm i t e r a t i v e P r e o r d e r ( node ) i f ( node == n u l l ) return s = empty s t a c k s. push ( node ) while ( not s. isempty ( ) ) node = s. pop ( ) v i s i t ( node ) // r i g h t c h i l d i s pushed f i r s t so t h a t // l e f t i s p r o c e s s e d f i r s t i f ( node. r i g h t!= n u l l ) s. push ( node. r i g h t ) i f ( node. l e f t!= n u l l ) s. push ( node. l e f t )

Outline 1 2

traversal: children are visited before the parent : A, C, E, D, B, H, I, G, F.

recursive 1 Check if the current node is empty / null. 2 Traverse the left subtree by recursively calling the post-order function. 3 Traverse the right subtree by recursively calling the post-order function. 4 Display the data part of the root (or current node). : A, C, E, D, B, H, I, G, F.

recursive 1 Process the nodes in the left subtree with a recursive call 2 Process the nodes in the right subtree with recursive call 3 Process the root : A, C, E, D, B, H, I, G, F.

recursive pseudocode algorithm p o s t o r d e r ( node ) i f ( node == n u l l ) return p o s t o r d e r ( node. l e f t ) p o s t o r d e r ( node. r i g h t ) v i s i t ( node ) What is trace on this tree?

iterative pseudocode algorithm i t e r a t i v e P o s t o r d e r ( node ) s = empty s t a c k l a s t N o d e V i s i t e d = n u l l while ( not s. isempty ( ) or node!= n u l l ) i f ( node!= n u l l ) s. push ( node ) node = node. l e f t e l s e peeknode = s. peek ( ) // i f r i g h t c h i l d e x i s t s and t r a v e r s i n g n // from l e f t c h i l d, then move r i g h t i f ( peeknode. r i g h t!= n u l l and l a s t N o d e V i s i t e d!= peeknode. r i g h t ) node = peeknode. r i g h t e l s e v i s i t ( peeknode ) l a s t N o d e V i s i t e d = s. pop ( )

Outline 1 2

An inorder traversal first visits the left child (including its entire subtree), then visits the node, and finally visits the right child (including its entire subtree). only : A, B, C, D, E, F, G, H, I.

: recursive 1 Check if the current node is empty / null. 2 Traverse the left subtree by recursively calling the function. 3 Display the data part of the root (or current node). 4 Traverse the right subtree by recursively calling the function. Why just for binary? : A, B, C, D, E, F, G, H, I.

: recursive 1 Process the nodes in the left subtree with a recursive call 2 Process the root 3 Process the nodes in the right subtree with recursive call : A, B, C, D, E, F, G, H, I.

: recursive i n o r d e r ( node ) i f ( node == n u l l ) return i n o r d e r ( node. l e f t ) v i s i t ( node ) i n o r d e r ( node. r i g h t ) What is trace on this tree?

: iterative i t e r a t i v e I n o r d e r ( node ) s = empty s t a c k while ( not s. isempty ( ) or node!= n u l l ) i f ( node!= n u l l ) s. push ( node ) node = node. l e f t e l s e node = s. pop ( ) v i s i t ( node ) node = node. r i g h t

Outline 1 2

recursive Useful for printing if indent each item to its depth in the tree 1 Process the nodes in the right subtree with a recursive call 2 Process the root 3 Process the nodes in the left subtree with a recursive call What is trace on this tree?

Outline 1 2

of traversal to non-binary 1 Perform pre-order operation. 2 For each i from 1 to the number of children do: 1 Visit i-th, if present. 2 Perform operation. 3 Perform post-order operation.

Outline 1 2

Counting example template <typename E> i n t count ( BinNode<E> r o o t ) { // i f Nothing to count i f ( r o o t == NULL) return 0 ; return 1 + count ( root >l e f t ( ) ) + count ( root >r i g h t ( ) ) ; } What is trace on this tree?

Outline 1 2

a function to a whole tree? Functions as parameters to functions. Pass as parameter: void func(int&)

Whichever func used has to accept one int void a p p l y ( void func ( i n t &), i n t data [ ], i n t n ) { i n t i ; f o r ( i =0; i <n ; i ++) { func ( data [ i ] ) ; } } void s e v e n u p ( i n t & i ) { i +=7; } a p p l y ( seven up, data, 1 0 ) ;

func() now can require different parameter types template <typename T> void a p p l y ( void func (T&), i n t data [ ], i n t n ) { i n t i ; f o r ( i =0; i <n ; i ++) { func ( data [ i ] ) ; } } // f u n c t i o n t h a t t a k e s a c h a r a c t e r i n s t e a d a p p l y ( c o n v e r t t o u p p e r, data, 1 0 ) ;

Only requirement is a single parametef for func template <typename T> void a p p l y (T func, i n t data [ ], i n t n ) { i n t i ; f o r ( i =0; i <n ; i ++) { func ( data [ i ] ) ; } }... a p p l y ( c o n v e r t t o u p p e r, data, 1 0 ) ; a p p l y ( seven up, data, 1 0 ) ;...

any operation to whole tree template <typename T, typename E> void p r e o r d e r (T func, BTNode<E> n o d e p t r ) { i f ( n o d e p t r!= NULL) } { } func ( node ptr >data ) ; p r e o r d e r ( node ptr >l e f t ) ; p r e o r d e r ( node ptr >r i g h t ) ; void s e v e n u p ( i n t & i ) { i +=7; } p r e o r d e r ( seven up, n o d e p t r ) ;