Upload
anjelita-ortiz
View
22
Download
2
Embed Size (px)
DESCRIPTION
Data Structure. Chapter# 5 Tree. Course Instructor AMEER JAMAL SHAH. TREES:. - PowerPoint PPT Presentation
Citation preview
Data Structure
Chapter# 5Tree
COURSE INSTRUCTORAMEER JAMAL SHAH
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.
Tree Image
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
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
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
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
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
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
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).
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
12
Preorder of Expression Tree
/ * + a b - c d + e fGives prefix form of expression.
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
14
Inorder by Projection
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
16
Postorder of Expression Tree
a b + c d - * e f + /Gives postfix form of expression.
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 * *
The End