You are on page 1of 10

NON LINEAR DATA STRUCTURES

1.0 INTRODUCTION
So far we have learned about Linear Data Structures in data is arranged in a linear sequence; in
which elements are linked one after the other. Such data structures include Arrays, Stack, Queues
and Lists.

Now we will focus on nonlinear data structures in which data elements are hierarchically connected
and are present at various levels. To be more specific we will focus on Trees and Graphs.

2.0 TREES
─ A tree is a non-linear abstract data type with a hierarchy-based structure.
─ It consists of nodes (where the data is stored) that are connected via links.
─ The tree data structure stems from a single node called a root node and has subtrees connected
to the root.
─ Trees are used in many areas of computer science, including operating systems, graphics,
database systems, and computer networking.
─ A tree data structure has a root, branches, and leaves.
─ The difference between a tree in nature and a tree in computer science is that a tree data
structure has its root at the top and its leaves on the bottom.

─ Tree data structure has many use cases in computer science which include:

1
 File Systems: which often use a tree structure to organize files and directories. The
root node represents the main directory, and each subsequent node represents a
subdirectory or file. (Read more about file system)

 HTML DOM: The Document Object Model (DOM) used in web development
represents the structure of an HTML document as a tree. Each HTML element is
represented by a node, with parent-child relationships reflecting the nesting of
elements. (Read more about HTML DOM)

 Network Routing: Tree-based routing algorithms are used in computer networks to


efficiently transmit data packets. The nodes in the tree represent network routers,
and the paths between nodes determine the optimal routing path for data
transmission. This helps in ensuring efficient and reliable communication within a
network. (Read more about Network Routing)

The following are important terms when dealing with trees:

─ Node: this represents an element or a data point with a tree. It contains the actual data and
additional pointers or references that define relationships between nodes.

2
─ Root: the node at the top of the tree is called root. There is only one root per tree and one path
from the root node to any node.
─ Edge: refers to the connection or link between two nodes. It represents the relationship
between a parent node and its child node.
─ Path: refers to the sequence of nodes that are connected by edges of a tree.
─ Parent: refers to the immediate node that is connected to a given node in the direction towards
the root of the tree. Each node, except for the root node, has exactly one parent node.
─ Child: refers to a node that is directly connected to another node in the direction away from the
root of the tree.
─ Leaf: the node which does not have any child node is called the leaf node.
─ Subtree: smaller tree that is formed by selecting a node (referred to as the root of the subtree)
and all of its descendants, including their child nodes and their descendants.
─ Traversing: passing through nodes in a specific order.
─ Levels: represents the generation of a node. If the root node is at level 0, then its next child node
is at level 1, its grandchild is at level 2, and so on.
─ Keys: represents a value of a node based on which a search operation is to be carried out for a
node.

2.1 Types of Trees

There are three types of trees:


1. General Trees
 In this tree every node may have infinite numbers of children.
 No constraint is placed on the tree’s hierarchy.
 This type of tree is the super-set of all other trees.

3
2. Binary Trees
 A binary tree is a tree data structure in which each parent node can have at most
two children.

 The following are the common types of binary trees:


i. Full Binary Tree: a special type of binary tree in which every
parent node/internal node has either two or no children.

ii. Perfect Binary Tree: a binary tree in which every internal node has
exactly two child nodes and all the leaf nodes are at the same level.

4
iii. Complete Binary Tree: is a special type of binary tree in which all
levels except the last level are completely filled, and all nodes in the
last level are as left as possible. In other words, all levels of the tree
are filled except for the last level, which is filled from left to right.

3. Binary Search Trees


 Binary Search Tree (BST) is a binary tree extension with several optional
restrictions.
 The left child value of a node should in BST be less than or equal to the parent value,
and the right child value should always be greater than or equal to the parent’s value.
 This Binary Search Tree property makes it ideal for search operations since we can
accurately determine at each node whether the value is in the left or right sub-tree.

5
2.2 Tree Traversal

─ Tree 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. That is, we cannot randomly access a node in a tree.
─ Generally, we traverse a tree to search or locate a given item or key in the tree or to print all
the values it contains.
─ There are three ways which we use to traverse a tree –
 In-order Traversal
 Pre-order Traversal
 Post-order Traversal

2.2.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.

Example: Consider the tree below and write the output of the in-order traversal

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 in-order traversal of this tree will
be:
D→B→E→A→F→C→G

6
The summary of the algorithm is as follows:

ALGORITHM: IN-ORDER TRAVERSAL


Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree
2.2.2 Pre-order Traversal

In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree. This is also referred to as depth-first traversal.

Example: Consider the tree below and write the output of the pre-order traversal

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
The summary of the algorithm is as follows:

ALGORITHM: PRE-ORDER TRAVERSAL


Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

7
2.2.3 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.

Example: Consider the tree below and write the output of the post-order traversal

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
The summary of the algorithm is as follows:

ALGORITHM: POST-ORDER TRAVERSAL


Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

8
ASSIGNMENT:

This assignment should be done after reading 1.0 INTRODUCTION and 2.0 TREES in the same
group(s) that did assignment 1.

Submission should be done through the email: mnkotagu@live.com with the Subject: Group
Assignment – Trees. Deadline for submission (08/06/2023 11:00AM)

1. Consider the following tree and write the sequence of nodes that will be visited using:

i. In-order Traversal
ii. Pre-order Traversal
iii. Post-order Traversal

2. Consider the binary tree shown below. For each of the traversals listed give the order in which
the nodes are visited
i. In-order Traversal
ii. Pre-order Traversal
iii. Post-order Traversal

9
3. Consider the following scenario. The pre-order traversal of a binary tree is A, B, E, C, D. The in-
order traversal of the same binary tree is B, E, A, D, C. What is the level order sequence for the
binary tree? (Hint find out what is level order traversal before attempting this question)

4. Consider the binary search tree below and answer the questions that follow

a) Write the in-order traversal, and post-order traversal of the given tree
b) If we insert the value 54 into this tree, which node becomes its parent?
c) If we insert the value 27 into this tree, which node becomes its parent?
d) If we delete node with value 70, which node should be its replacement node?
e) If we delete node with the value 25, which node should be its replacement node?

10

You might also like