Skip to main content
1.Tree
2.Grap
h
In computer science, a tree is a widely-used
data structure that emulates a hierarchical tree
structure with a set of linked nodes.
Nature View of a Tree
branch
e
s
leave
s
root
Computer Scientist’s View
leave
s
roo
t
branches
node
s
What is a Tree
⚫ A tree is a finite nonempty
set of elements.
⚫ It is an abstract model
of a hierarchical
structure.
⚫ consists of nodes with a
parent-child relation.
⚫ Applications:
⚫ Organization charts
⚫ File systems
⚫ Programming
environments
Computers”R”Us
Sale
s
R&
D
Manufacturin
g
Laptop
s
Desktop
s
US International
Europ
e
Asi
a
Canad
a
Tree Representation
Cont…
 Root-The top most
Node.
 Children
 Parents
 Siblings- Have same
parents.
 Leaf- Has no Child.
RELATION OF TREE
1
3
2
4
6
5
8
7
11
10
9
8
Trees Data Structures
⚫Tree
⚫Nodes
⚫Each node can have 0 or more
children
⚫A node can have at most one
parent
⚫Binary tree
⚫Tree with 0–2 children per node
Tre Binary Tree 9
Tree
s
⚫Terminology
⚫Root  no parent
⚫Leaf  no child
⚫Interior  non-leaf
⚫Height  distance from root to
leaf
Root node
Leaf nodes
Interior nodes Height
1
Example Tree
roo
t
Activitie
s
Teachin
g
Researc
h
Sami’s Home
Page
Paper
s
Presentatio
ns
CS21
1
CS10
1
Tree Terminology
⚫Path: Traversal from node to node along the edges
results
in a sequence called path.
⚫Root: Node at the top of the tree.
⚫Parent: Any node, except root has exactly one edge
running upward to another node. The node above it
is called parent.
⚫Child: Any node may have one or more lines
running downward to other nodes. Nodes
below are children.
⚫Leaf: A node that has no children.
⚫Subtree: Any node can be considered to be the
root of a subtree, which consists of its children
subtre
e
Tree Terminology
⚫ Root: node without parent (A)
⚫ Siblings: nodes share the same parent
⚫ Internal node: node with at least
one child (A, B, C, F)
⚫ External node (leaf ): node
without children (E, I, J, K, G, H,
D)
⚫ Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
⚫ Descendant of a node: child,
grandchild,
grand-grandchild, etc.
⚫ Depth of a node: number of ancestors
⚫ Height of a tree: maximum depth of
any node (3)
⚫ Degree of a node: the number of
its children
⚫ Degree of a tree: the maximum
number of
its node.
A
B D
C
G H
E F
I J K
Subtree: tree consisting of a
node and its descendants
General Trees
⚫Trees store data in a hierarchical
manner
⚫ Root node is A
⚫ A's children are B, C, and D
⚫ E, F, and D are leaves
⚫ Length of path from A to E is 2
edges
D
A
C
F
B
E
Trees have layers of nodes,
where some are higher,
others are lower.
Logic of Tr
Used to represent hierarchical
data.

BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A)
Section (B)
CR 1
CR2
group
1
group
2
group 3 group
4
group
1
group
2
group
3
group
4
1
Logic of Tr
Used to represent hierarchical
data.

BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A)
Section (B)
CR 1
CR2
group
1
group
2
group 3 group
4
group
1
group
2
group
3
group
4
1
Cont…
 A Collection of entities called
Nodes.
 Tree is a Non-Linear Data
Structure.
 It’s a hierarchica Structure.
TREE
Nodes
1
Tree Traversal
⚫Unlike linear data structures (Array, Linked
List, Queues, Stacks, etc) which have only one
logical way to traverse them, trees can be
traversed in different ways.
⚫Traversal is a process to visit all the nodes of a
tree and may print their values too. Because, all
nodes are connected via edges (links) we always
start from the root (head) node.
Cont…
⚫ That is, we cannot randomly access a node in a tree.
There are
three ways which we use to traverse a tree
⚫ (a) Inorder (Left, Root, Right) : 4 2 5 1
3
(b) Preorder (Root, Left, Right) : 1 2 4
5 3
(c) Postorder (Left, Right, Root) : 4 5 2
3 1
⚫
In-order Traversal
⚫In this traversal method, the left subtree is
visited first, then the root and later the right sub-
tree. We should always remember that every
node may represent a subtree itself.
Inorder (Left, Root, Right)
Inorder (Left, Root, Right)
⚫We start from A, and following in-order
traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all
the nodes are visited. The output of inorder
traversal of this tree will be −
⚫D → B → E → A → F → C → G
Inorder Algorithm
⚫Until all nodes are traversed −
⚫ Step 1 − Recursively traverse left
subtree.
⚫ Step 2 − Visit root node.
⚫Step 3 − Recursively traverse right
subtree.
Pre-order Traversal
⚫In this traversal method, the root node is visited
first, then the left subtree and finally the right
subtree.
Preorder (Root, Left, Right)
Preorder (Root, Left, Right)
⚫ We start from A, and following pre-order traversal, we first visit A
itself and then move to its left subtree B. B is also traversed pre-order.
The process goes on until all the nodes are visited. The output of pre-
order traversal of this tree will be.
⚫ A → B → D → E → C → F →
G
Algorithm
⚫Until all nodes are traversed −
⚫Step 1 − Visit root node.
⚫Step 2 − Recursively traverse left
subtree.
⚫ Step 3 − Recursively traverse right
subtree.
Post-order Traversal
⚫In this traversal method, the root node is visited
last, hence the name. First we traverse the left
subtree, then the right subtree and finally the
root node.
Postorder (Left, Right, Root)
Postorder (Left, Right, Root)
⚫ We start from A, and following pre-order traversal, we first visit
the left subtree B. B is also traversed post-order. The process
goes on until all the nodes are visited. The output of post-order
traversal of this tree will be.
⚫ D → E → B → F → G → C →
A
Algorithm
⚫Until all nodes are traversed
⚫ Step 1 − Recursively traverse left
subtree.
⚫ Step 2 − Recursively traverse right
subtree.
⚫ Step 3 − Visit root node.
1.Insertio
n
2.Deletion
3.Searchin
g
• If root is NULL
• then create root node
• return
• If root exists then
• compare the data with node.data
• while until insertion position is
located
• If data is greater than node.data
• goto right subtree
• else
• goto left subtree
• endwhile
• insert data
If root.data is equal to
search.data return root
else
while data not found
If data is greater than
node.data goto right subtree
else
goto left
subtree If
data found
return node
endwhile
return data not
found end if
Deletion
algorithm ????
There are a large number of tree types, however three
types specifically are used the majority of the time (both
in real world development and in Computer Science
courses). The types are:
 Binary trees
 B-Trees
 Heaps
Binary trees
⚫Binary tree follows the rule that each parent
node has no more than 2 child nodes. This
means that a binary tree can look like this:
⚫ OR
Strict Binary Tree
⚫Strict Binary Tree is a Binary tree in which
root can have exactly two children or no
children at all. That means, there can be 0 or
2 children of any node.
Complete Binary Tree
⚫Complete Binary Tree is a Strict Binary tree in
which every leaf node is at same level. That
means, there are equal number of children in
right and left subtree for every node.
Extended Binary Tree
⚫An extended binary tree is a transformation
of any binary tree into a complete binary
tree.
⚫This transformation consists of replacing
every null subtree of the original tree with
“special nodes.”
⚫The nodes from the original tree are then called
as internal nodes, while the “special nodes” are
called as external nodes.
Extended Binary Tree
A binary tree with special nodes replacing every null subtree. Every
regular node
has two children, and every special node has no children.
Binary Search Tree
⚫ The value at any node, • Greater than every value in left subtree.
Smaller than every value in right subtree – Example
⚫Y > X
⚫Y < Z
⚫ Values in left sub tree less than parent
⚫ Values in right sub tree greater than parent
⚫ Fast searches in a Binary Search tree, maximum of log n
comparisons.
Binary Search Trees
⚫Example
s
⚫Binary search
tree
B - Trees
⚫B – Tree is another type of search tree called B-
Tree in which a node can store more than one
value (key) and it can have more than two
children.
⚫B-Tree can be defined as follows…
⚫ B-Tree is a self-balanced search tree with multiple keys in every node and more
than two
children for every node.
Operations on a B-Tree
⚫The following operations are performed on a B-
Tree...
⚫Search
⚫Insertion
⚫Deletion
Heap Tree
⚫Heap is a special case of balanced binary tree
data structure where the root-node key is
compared with its children and arranged
accordingly. If α has child
node β then
⚫key(α) ≥ key(β)
⚫A heap can be of two types
⚫ Min-Heap
⚫ Max-Heap
Max-Heap
⚫Max-Heap − Where the value of the root
node is greater than or equal to either of its
children.
Max-Heap insertion
Max-Heap deletion
Max Heap Construction Algorithm
⚫Step 1 − Create a new node.
⚫ Step 2 − Assign new value to the node.
⚫Step 3 − Compare the value of this child node
with its parent.
⚫Step 4 If
− value of parent is less than child,
then swap them.
⚫Step 5 − Repeat step 3 & 4 until Heap property
holds.
Max Heap Deletion
Algorithm
⚫Step 1 − Remove root node.
⚫Step 2 − Move the last element of last level to
root.
⚫Step 3 − Compare the value of this child node
with its parent.
⚫Step 4 If
− value of parent is less than child,
then swap them.
⚫Step 5 − Repeat step 3 & 4 until Heap property
holds.
Min-Heap
⚫Min-Heap − Where the value of the root node
is less than or equal to either of its children.
Min-Heap insertion
Min-Heap deletion