+ All Categories
Home > Documents > Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In...

Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In...

Date post: 21-May-2020
Category:
Upload: others
View: 13 times
Download: 0 times
Share this document with a friend
19
CS350: Data Structures © James Moscola James Moscola Department of Engineering & Computer Science York College of Pennsylvania CS350: Data Structures Tree Traversal
Transcript
Page 1: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATALO

G 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania

CS350: Data StructuresTree Traversal

Page 2: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Defining Trees Recursively

• Trees can easily be defined recursively

• Definition of a binary tree (a tree with arity of 2) - A binary tree is either empty (represented by

a null pointer), or is made of a single node,where the left and the right pointers eachpoint to a binary tree

2

Subtree1

Subtree2

Page 3: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Defining Trees Recursively

• More generally, a tree with unspecified arity is defined as - A tree is a collection of nodes (one node is called the root)- A collection of nodes can be empty, otherwise a tree consists of a

root node (R) and zero or more non-empty subtrees each of whose roots are connected by an edge to the root R

• Because they are easily defined recursively, it is also easy to traverse them (visit each node) recursively

3

Page 4: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Duplicating a Tree

4

/** * Return a reference to a node that is the root of a * duplicate of the binary tree rooted at the current node. */public BinaryNode<AnyType> duplicate( ){ // Create the new node for the new tree BinaryNode<AnyType> root = new BinaryNode<AnyType>(element, null, null);

if( left != null ) // If there's a left subtree root.left = left.duplicate( ); // Duplicate and attach if( right != null ) // If there's a right subtree root.right = right.duplicate( ); // Duplicate and attach return root; // Return resulting tree}

data

left subtree

right subtree

Page 5: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Determining the Size of a Tree

• The size of a tree can be defined as: -LeftSubtree.size( ) + RightSubtree.size( ) + 1

- Determine the size of each subtree, add them together, add 1 additional node to represent theroot node

5

Subtree1

Subtree2

Page 6: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Determining the Size of a Tree (Cont.)

6

/** * Return the size of the binary tree rooted at t. */ public static int size( BinaryNode<AnyType> t ) { if( t == null ) return 0; else return 1 + size( t.left ) + size( t.right ); }

Page 7: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Tree Traversal

• A tree traversal is a systematic method of visiting each node in a tree

• Different tree traversal algorithms visit nodes of a tree in different orders

• Tree traversal algorithms - Preorder traversal- Postorder traversal- Inorder traversal- Level-order traversal

7

Page 8: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Preorder Traversal

• In preorder traversal, a node is processed and then its children are processed recursively - The duplicate method is an example of a preorder traversal -- the root

is created first, then the subtrees are duplicated

8

A

D E

B C

F

G A B D E G C F

Nodes are visited in the following order:

Page 9: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Preorder Traversal

• In preorder traversal, a node is processed and then its children are processed recursively - The duplicate method is an example of a preorder traversal -- the root

is created first, then the subtrees are duplicated

9

1

3 4

2 6

7

5

Page 10: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Preorder Traversal

10

// Print tree rooted at current node using preorder traversal. public void printPreOrder( ) { System.out.println( element ); // Process Node if( left != null ) left.printPreOrder( ); // Process Left Subtree if( right != null ) right.printPreOrder( ); // Process Right Subtree }

Page 11: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Postorder Traversal

• In postorder traversal, a node is processed after both children are processed recursively - The size method is an example of a postorder traversal -- the size of

subtrees are determined to find the size of the current tree

11

A

D E

B C

F

G D G E B F C A

Nodes are visited in the following order:

Page 12: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Postorder Traversal

• In postorder traversal, a node is processed after both children are processed recursively - The size method is an example of a postorder traversal -- the size of

subtrees are determined to find the size of the current tree

12

7

1 3

4 6

5

2

Page 13: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Postorder Traversal

13

// Print tree rooted at current node using postorder traversal public void printPostOrder( ) { if( left != null ) left.printPostOrder( ); // Process Left Subtree if( right != null ) right.printPostOrder( ); // Process Right Subtree System.out.println( element ); // Process Node }

Page 14: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Inorder Traversal

• In inorder traversal, the left child is processed recursively, then the current node is processed, then the right child is recursively processed

14

A

D E

B C

F

G D B E G A F C

Nodes are visited in the following order:

Page 15: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Inorder Traversal

• In inorder traversal, the left child is processed recursively, then the current node is processed, then the right child is recursively processed

15

5

1 3

2 7

6

4

Page 16: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Inorder Traversal

16

// Print tree rooted at current node using inorder traversal public void printInOrder( ) { if( left != null ) left.printInOrder( ); // Process Left Subtree System.out.println( element ); // Process Current Node if( right != null ) right.printInOrder( ); // Process Right Subtree }

Page 17: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Level-order Traversal

• In level-order traversal, nodes are processed in the tree from top to bottom, left to right - The first level is the root node, the second level consists of the

children of the root node, the third level consists of the grandchildren of the root node, etc.

17

A

D E

B C

F

G A B C D E F G

Nodes are visited in the following order:

Page 18: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Level-order Traversal

• In level-order traversal, nodes are processed in the tree from top to bottom, left to right - The first level is the root node, the second level consists of the

children of the root node, the third level consists of the grandchildren of the root node, etc.

18

1

4 5

2 3

6

7

Page 19: Tree Traversal lecture - GitHub Pages · CS350: Data Structures Level-order Traversal • In level-order traversal, nodes are processed in the tree from top to bottom, left to right

CS350: Data Structures

Level-order Traversal

• A recursive implementation is not well-suited for level-order traversal

• A queue can be used to implement level-order traversal instead

19

q = [instantiate a queue] q.push(root);

while (!q.isEmpty()) { n = q.dequeue(); visit(n); for each child of n { q.enqueue(child); } }


Recommended