0% found this document useful (0 votes)
18 views35 pages

Lecture 9 Week 11 Intro Trees 26112024 121749am

Uploaded by

mahamtahir.2747
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)
18 views35 pages

Lecture 9 Week 11 Intro Trees 26112024 121749am

Uploaded by

mahamtahir.2747
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/ 35

Data Structure &

Algorithms – CSC 221


Lecture 9
Instructor: Dr. Muhammad Asfand-e-yar

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Today’s Lecture
• Definition & examples of Tree
• Tree Terminology
• Levels of tree, Depth of Tree, and Height of Tree
• Ancestor, Descendent, Edge and Path
• Binary Tree
• Strictly Binary Tree
• Complete Binary Tree
• Traversing Tree
• Pre-Order
• In-Order
• Post-Order
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
REAL WORLD DATA STRUCTURE

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Tree
Data structure such as Arrays, Stacks, Linked List and Queues
are linear data structure. Elements are arranged in linear manner
i.e. one after another.

Tree is a non-linear data structure.

Tree imposes a Hierarchical structure, on a collection of items.

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Tree

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples: directory structure

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Organizational organograms

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples: mathematical expressions

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Definition
A tree is a finite set of nodes, such that:
• There is a special node called the root.
• The remaining nodes are partitioned into n  0 disjoint sets T1, T2,
......., Tn.
• Each of the sets T1, T2, ......., Tn is itself a tree (subtrees of root).

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Trees - Definition and Terminology
Tree: A finite set of elements called nodes (or vertices) and a
finite set of directed arcs that connect pairs of nodes. If the tree
is not empty, one of the nodes, called the root, has no incoming
arcs.

Leaf: Node with no outgoing arcs.

Nodes directly accessible (using one arc) from a node are called
the children of that node, which is called the parent of these
children; these nodes are siblings of each other.
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Tree
1 root

2 3

4 5 6 7 children of this parent


siblings of each other

8 9

leaves
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Tree Terminologies A

B C
Root
• It is the key node of a tree structure that does not have parent. It is the
first node in hierarchical arrangement.
Node
• The node of a tree stores the data and its role is the same as in the
linked list. Nodes are connected by the means of links with other nodes.
Parent
• It is the immediate predecessor of a node. In the figure A is the parent
of B and C.

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Tree Terminologies A

B C
Child
• All successor nodes are called child nodes. In the figure B and C are the child
nodes of A
Sibling
• The child node of same parent are called sibling.
Link / Edge
• An edge connects the two nodes. The line drawn from one node to other
node is called edge / link. Link is nothing but a pointer to node in a tree
structure.
Leaf / Terminal Node
• This node is located at the end of the tree. It does not have any child hence it
is called leaf node.
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Tree Terminologies
Level
• Level is the rank of tree hierarchy. The
whole tree structure is leveled. The level of
the root node is always at 0. The immediate
children of root are at level 1 and their Root node
children are at level 2 and so on.
Height
Depth Interior nodes
• The depth of a node n is the length of the
Leaf nodes
path from the root to the node.
• The depth (or height) of a tree is the length
of the path from the root to the deepest
node in the tree.
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Levels and Depth
Root
Level 0 Node

Level 1
Arc

Level 2

Level 3

Leaf
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Levels and Depth

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Levels and Depth

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees
A binary tree is a tree in which each node has at most 2 children.

root
O

M T
left subtree right subtree
of node “O” of node “O”
C E • P U

left subtree right subtree


of node “M” of node “M”

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees Examples
Binary Trees

76 99

26 85 26

21 50 80 99 21 76
26
50 85
76
80
85

99
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Binary Trees
If A is the root of a binary tree and B is the root of its left or right subtree,
then A is said to be father of B, and B is said to be the left or right son of A.

left son of A B C right son of A

D E • F G


left son of B right son of B

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees
A node having no sons is called a leaf of the binary tree.

B C

D E F G

leaves

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees
Node n1 is an ancestor of node n2 (and n2 is descendent of n1);
if n1 is either the father of n2 or the father of some ancestor of n2.

A
A is ancestor of D

B C H is descendent of B

D E F G G is ancestor of J

F is ___ of H ???
H I J

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees
A node n2 is left descendent of node n1;
if n2 is either the left son of n1 or a descendent of the left son of
n1.

Similar definition for right descendent.

Two nodes are brothers if they are left and right sons of the same
father.

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Binary Trees

A
G is right descendent of A.

B C
E is left descendent of A.

H is right descendent of B.
D E F G
I is right descendent of C.
H I J
H is left descendent of E.

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation
Root
A

B C

D E F

Left Subtree of A
G H I

BS (CS) – Data Structures & Algorithms Right Subtree of A Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Examples – familiarization with notation
A
An Edge

B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation
Child nodes of C?

Child nodes of A? A

B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation
Descendants of Node C?
A
Descendants of A?

B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation
Parent of Node E?
A
Parent of Node H?

B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation

Ancestors of Node D?
A

Ancestors of Node H?
B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation
A path from J to A
A

Path: B C
If n1, n2,…nk is a sequence of nodes such
that ni is the parent ( child) of ni+1, then F
that sequence is a path. D E

G H I

J
BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,
Bahria University, Islamabad
Examples – familiarization with notation

Leaf node: A
Any node that has empty children
B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Examples – familiarization with notation

Internal node: A
Any node that has at least one
non-emptyChild B C

D E F

G H I

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad
Example Binary Trees

One node Two nodes

Three nodes

BS (CS) – Data Structures & Algorithms Instructor: Dr. Muhammad Asfand-e-yar,


Bahria University, Islamabad

You might also like