0% found this document useful (0 votes)
4 views16 pages

Week 07 Lecture

Uploaded by

lujainmahesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Week 07 Lecture

Uploaded by

lujainmahesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

DATA STRUCTURE

AND ALGORITHMS

Week-07

By: Dr. Shahid Ali Mahar


Email: [email protected]
TREE…
▸ A tree is a nonlinear hierarchical data structure that
consists of nodes connected by edges.
▸ Why Tree Data Structure?
▸ Other data structures such as arrays, linked list,
stack, and queue are linear data structures that store
data sequentially. In order to perform any operation
in a linear data structure, the time complexity
increases with the increase in the data size. But, it
is not acceptable in today's computational world.
▸ Different tree data structures allow quicker and
easier access to the data as it is a non-linear data
structure. 2
TREE…
The tree is mainly used to represent data
containing a hierarchical relationship between
elements. For example Records, family trees and
table of contents.
A tree is collection of nodes, that originate from a
unique starting node called the root.

Nodes are connected by lines called edges.


BINARY TREE
A binary tree is defined as a finite set of elements,
called nodes, such that
• T is empty (called the Null Tree or Empty Tree)
• T is partitioned into three disjoint sub trees
 A single node r, the root
 Two possibly empty sets that are binary trees,
called left and right sub trees
BINARY TREE… A

B C

G H
D E

J K
F
L
1. This TREE consists of 11 nodes represented by the letters A
through L.
2. The root of TREE is the node A at the top of the diagram.
3. B is the left successor of the node A.
4. C is the right successor of the node A.
BINARY TREE… A

B C

G H
D E

J K
F
1. The nodes A, B, C and H have two successors.
L
2. The nodes E and J have only one successor.
3. The nodes D, F, G, L and K have no successor.
4. The nodes with no successor are called terminal nodes.
TERMINOLOGY…
Suppose A is a root node in a tree with left successor B and right successor C
 A is called the PARENT of B and C.
 B is called the LEFT CHILD of A.
 C is called the RIGHT CHILD of A.
 B and C are said to be SIBLINGS.
 Nodes are connected by lines called EDGES.
 Sequence of consecutive edges is called a PATH.
 A terminal node is called a LEAF.
 O and P are Leaf Nodes
 F and J are ANCESTORS of O.
 A forest is a set of DISJOINT trees. Removing the root of a tree produces a forest
 Height of Tree is Maximum Level of its node  Leveln+1
 B, C, D, E, F, G, H, I, J, K, M,O and P are DESCENDANT of A.
Binary Tree From Algebraic Expression
Consider any algebraic expression E involving only Binary Operations
E=(a–b) / ((c*d)+e)

- +

a b * e

c d
Representation of Tree as an Array
S

1
Y
S
A 2 A
3 Y
W B
R T 4 R
5 T
6 W
7 B
left Child = 2n
right Child = 2n+1

n is location of the parent


Representation of Tree as an Linked List

S ROOT INFO Left Right


5 1 A 3 7

A Y 2 B 0 0
3 R 0 0
4 8
W B
R T AVAIL 5 S 1 9

4 6 W 0 0
7 T 0 0
8 10
9 Y 6 2
10 0
TRAVERSING OF BINARY TREES
There are three standard ways of traversing a binary tree
PREORDER
Process the root R
Traverse the left sub tree of R in preorder Node-Left-Right (NLR)
Traverse the right sub tree of R in preorder Traversal
INORDER
Traverse the left sub tree of R in ignored Left-Node-Right (LNR)
Process the root R
Traverse the right sub tree of R in ignored Traversal

POSTORDER
Traverse the left sub tree of R in postorder Left-Right-Node (LRN)
Traverse the right sub tree of R in postorder Traversal
Process the root R
TRAVERSING OF BINARY TREES (Preorder)
NLR A

NLR B C NLR

D E F G

Preorder  NLR: A, B, D, E, C, F, G
TRAVERSING OF BINARY TREES (Inorder)
LNR A

LNR B C LNR

D E F G

Inorder  LNR: D, B, E, A, F, C, G
TRAVERSING OF BINARY TREES (Postorder)
LRN A

LRN B C LRN

D E F G

Postorder  LRN: D, E, B, F, G, C, A
TRAVERSING OF BINARY TREE USING LINKED LIST
100
200 A 500

500
200
600 C 700
300 B 400

300 400 600 700


X D X X E X X F X X G X

Preorder  NLR: A, B, D, E, C, F, G
Inorder  LNR: D, B, E, A, F, C, G
Postorder  LRN: D, E, B, F, G, C, A
THANKS!
Any questions?

16

You might also like