18
Data Structure Chapter# 5 Tree COURSE INSTRUCTOR AMEER JAMAL SHAH

Data Structure

Embed Size (px)

DESCRIPTION

Data Structure. Chapter# 5 Tree. Course Instructor AMEER JAMAL SHAH. TREES:. - PowerPoint PPT Presentation

Citation preview

Page 1: Data Structure

Data Structure

Chapter# 5Tree

COURSE INSTRUCTORAMEER JAMAL SHAH

Page 2: Data Structure

TREES:

• TREES:- Tree is a nonlinear data structure. This data structure is mainly used to represent data containing a hierarchical relationship between elements., e.g., records of family trees and tables of contents etc. Tree is divided into

i) General Trees. ii) Binary Trees.• General Trees:- It can be defined as a node having finite

number of subnodes is known as a general tree.

Page 3: Data Structure

Tree Image

Page 4: Data Structure

Level of TREES• The tree shown in the fig will be access as : ABCDEF• A Level First• B C Level Second• D E F Level Third

A

CB

E

G

D F

Level

0

1

2

3

Page 5: Data Structure

Binary Tree:- • A Tree which has almost two branches

is called Binary Tree. A binary tree can be easily maintained in the computer, although it seems to be very restricted.

• A Binary Tree “T” has a finite set of elements, called nodes such that:

• T is empty (called the null tree or empty tree), or

• T contains a distinguished node “R” called the root of the tree, and the remaining nodes of the tree form an ordered pair of disjoined binary tree T1 and T2.

R

T1

T3 T4

T2

T6T5

Page 6: Data Structure

Binary Tree

• The node having no preceding node is called the root node.• while the nodes having no succeeding nodes are known as

terminal nodes of the tree.• Each node has two successors– one called the left child– one called the right child– left child and/or right child may be empty

A binary tree is either - empty or - consists of a root and two binary trees, one called the left subtree and one called the right subtree

Page 7: Data Structure

Why a binary tree is so useful?

• For storing a collection of values that has a binary hierarchical organization– arithmetic expressions– boolean logic expressions

• Data structure for a binary search tree• General tree can be stored using a binary

tree data structure

Page 8: Data Structure

An expression tree• An arithmetic expression

consists of an operator and two operands (either of which may be an arithmetic expression)

• some simplifications– only binary operators

(+, *, /, -)– only single digit, non-negative

integer operands

*

- +

5 2 6 1

operands are stored in leaf nodesoperators are stored in branch nodes

Page 9: Data Structure

Traversing a binary tree

• What does it mean to traverse a container?– "visit" each item once in some order

• Many operations on a collection of items involve traversing the container in which they are stored– displaying all items– making a copy of a container

• Linear collections (usually) traversed from first to last– last to first is an alternative traversal order

Page 10: Data Structure

10

Binary Tree Traversal Methods• Preorder

The Parent of the subtree is processed first before going into the left then right subtree (Parent, left, right).

• InorderAfter the complete processing of the left subtree the

Parent is processed followed by the processing of the complete right subtree (left, Parent, right).

• PostorderThe root is processed only after the complete

processing of the left and right subtree (left, right, Parent).

Page 11: Data Structure

11

Preorder Example (visit = print)

H

D

B

A C E G I K M O

N

L

JF

O M N K I J L G E F C A B D H

P-L-R

Page 12: Data Structure

12

Preorder of Expression Tree

/ * + a b - c d + e fGives prefix form of expression.

Page 13: Data Structure

13

Inorder Example (visit = print)

L-P-R

H

D

B

A C E G I K M O

N

L

JF

O N M L K J I H G F E D C B A

Page 14: Data Structure

14

Inorder by Projection

Page 15: Data Structure

15

Postorder Example (visit = print)L-R-PH

D

B

A C E G I K M O

N

L

JF

H L N O M J K I D F G E B C A

Page 16: Data Structure

16

Postorder of Expression Tree

a b + c d - * e f + /Gives postfix form of expression.

Page 17: Data Structure

Traversing an expression tree

4

*

+ *

1 3 - 2

1

preorder (1st touch) * + 1 3 * - 4 1 2

inorder (2nd touch) 1 + 3 * 4 - 1 * 2

postorder (last touch) 1 3 + 4 1 - 2 * *

Page 18: Data Structure

The End