Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)...

19
Data Structures Data Structures Azhar Maqsood Azhar Maqsood School of Electrical Engineering and School of Electrical Engineering and Computer Sciences (SEECS-NUST) Computer Sciences (SEECS-NUST) Binary Trees Binary Trees

description

Depth First and Breadth First Traversal Depth-first traversals: using the top-down view of the tree structure. Traverse the root, the left subtree and the right subtree. Breadth-first or level-order traversal: visit nodes in order of increasing depth. For nodes of same depth visit in left-to-right order.

Transcript of Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)...

Page 1: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Data StructuresData Structures

Azhar MaqsoodAzhar MaqsoodSchool of Electrical Engineering and Computer Sciences School of Electrical Engineering and Computer Sciences

(SEECS-NUST)(SEECS-NUST)

Binary TreesBinary Trees

Page 2: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Visiting and Traversing a Node

• Many applications require that all of the nodes of a tree be “visited”.

• Visiting a node may mean printing contents, retrieving information, making a calculation, etc.

• Traverse: To visit all the nodes in a tree in a systematic fashion.• A traversal can pass through a node without visiting

it at that moment.

Page 3: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Depth First and Breadth First Traversal

• Depth-first traversals: using the top-down view of the tree structure. Traverse the root, the left subtree and the right subtree.

• Breadth-first or level-order traversal: visit nodes in order of increasing depth. For nodes of same depth visit in left-to-right order.

Page 4: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Traversal strategies

• Preorder traversal• Work at a node is performed before its children are

processed.

• Postorder traversal• Work at a node is performed after its children are

processed.

• Inorder traversal• For each node:

• First left child is processed, then the work at the node is performed, and then the right child is processed.

Page 5: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Tree Traversal Types

Page 6: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Preorder traversal

• In the preorder traversal, the root node is processed first, followed by the left subtree and then the right subtree.

PrePreorder = root node of each subtree order = root node of each subtree beforebefore the subsequent left and right the subsequent left and right subtrees.subtrees.

Page 7: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Preorder Traversal

Page 8: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Preorder Traversal

• Visit root before traversing subtrees.

F

A

B C

G

IH

D E

J K

Page 9: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Inorder Traversal

• In the inorder traversal, the left subtree is processed first, followed by the root node, and then the right subtree.

Inorder = root node in betweenthe left and right subtrees.

Page 10: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Inorder Traversal

Page 11: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Inorder Traversal

• In an inorder traversal a node is visited after its left subtree and before its right Subtree

• Application: draw a binary tree or Arithmetic expression printing

((2 × (a − 1)) + (3 × b))

Page 12: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Example of Binary Tree (inorder)

Page 13: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Inorder Traversal

• Visit root between left and right subtree.

A

B C

G

IH

D E

J K

F

Page 14: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

• In the postorder traversal, the left subtree is processed first, followed by the right subtree, and then the root node.

Postorder = root node after the left and right subtrees.

Postorder traversalPostorder traversal

Page 15: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Postorder TraversalPostorder traversalPostorder traversal

Page 16: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

• In a postorder traversal, a node is visited after its descendants

• Application: compute space used by files in a directory and its subdirectories

Postorder traversalPostorder traversal

Page 17: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

• Visit root after traversing subtrees.

A

B C

G

IH

D E

J K

F

Postorder traversalPostorder traversal

Page 18: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Expression Trees

+

a

b c

×

× +

×

d e

f

g

+

Expression tree for ( a + b × c) + ((d ×e + f) × g)

There are three notations for a mathematical expression: 1) Infix notation : ( a + (b × c)) + (((d ×e) + f) × c)2) Postfix notation: a b c × + d e × f + g * +3) Prefix notation : + + a × b c × + × d e f g

Page 19: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.

Expression Tree traversals

• Depending on how we traverse the expression tree, we can produce one of these notations for the expression represented by the three.

• Inorder traversal produces infix notation.• This is a overly parenthesized notation. • Print out the operator, then print put the left subtree inside parentheses,

and then print out the right subtree inside parentheses. • Postorder traversal produces postfix notation.

• Print out the left subtree, then print out the right subtree, and then printout the operator.

• Preorder traversal produces prefix notation. • Print out the operator, then print out the right subtree, and then print

out the left subtree.