Why Tree Data Structure?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Tree Data Structure

A tree is a nonlinear hierarchical data structure that consists of nodes connected by


edges.

A Tree

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
nonlinear data structure.

Tree Terminologies
Node

A node is an entity that contains a key or value and pointers to its child nodes.

The last nodes of each path are called leaf nodes or external nodes that do not contain
a link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge

It is the link between any two nodes.

Nodes and edges of a tree Root

It is the topmost node of a tree.

Height of a Node

The height of a node is the number of edges from the node to the deepest leaf (ie. the
longest path from the node to a leaf node).

Depth of a Node

The depth of a node is the number of edges from the root to the node.
Height of a Tree

The height of a Tree is the height of the root node or the depth of the deepest node.
Height and depth of each node in a tree

Binary Tree-

• Binary tree is a special tree data structure.


• In a binary tree, each node can have at most 2 children.

Tree Traversal-

Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.

Various tree traversal techniques are-


Depth First Traversal-

Following three traversal techniques fall under Depth First Traversal-


1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal

1. Preorder Traversal-

Algorithm-

1. Visit the root


2. Traverse the left sub tree i.e. call Preorder (left sub tree)
3. Traverse the right sub tree i.e. call Preorder (right sub tree)

Root → Left → Right

Example-

Consider the following example-


Preorder Traversal Shortcut
Traverse the entire tree starting from the r oot node keeping yourself to the left.

Applications-

• Preorder traversal is used to get prefix expression of an expression tree.


• Preorder traversal is used to create a copy of the tree.

2. Inorder Traversal-

Algorithm-

1. Traverse the left sub tree i.e. call Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e. call Inorder (right sub tree)

Left → Root → Right

Example-

Consider the following example-

Inorder Traversal Shortcut

Keep a plane mirror horizontally at the bottom of the tree and take the projection of all the nodes.
Application-

• Inorder traversal is used to get infix expression of an expression tree.

3. Postorder Traversal-

Algorithm-

1. Traverse the left sub tree i.e. call Postorder (left sub tree)
2. Traverse the right sub tree i.e. call Postorder (right sub tree)
3. Visit the root

Left → Right → Root


Example-

Consider the following example-

Postorder Traversal Shortcut

Pluck all the leftmost leaf nodes one by one.


Applications-

• Postorder traversal is used to get postfix expression of an expression tree.


• Postorder traversal is used to delete the tree.
• This is because it deletes the children first and then it deletes the parent.

Breadth First Traversal-

• Breadth First Traversal of a tree prints all the nodes of a tree level by level.
• Breadth First Traversal is also called as Level Order Traversal.

Example-
Application-

• Level order traversal is used to print the data in the same order as stored in the array
representation of a complete binary tree.

To gain better understanding about Tree Traversal,

PRACTICE PROBLEMS BASED ON TREE TRAVERSAL-

Problem-01:

If the binary tree in figure is traversed in inorder, then the order in which the nodes will be visited is
____?
Solution-

The inorder traversal will be performed as-


Problem-02:

Which of the following sequences denotes the postorder traversal sequence of the tree shown in
figure?

1. FEGCBDBA
2. GCBDAFE 3. GCDBFEA
4. FDEGCBA

Solution-

Perform the postorder traversal by plucking all the leftmost leaf nodes one by one.
Then,
Postorder Traversal : G , C , D , B , F , E , A

Thus, Option (C) is correct.

Problem-03:

Let LASTPOST, LASTIN, LASTPRE denote the last vertex visited in a postorder, inorder and preorder
traversal respectively of a complete binary tree. Which of the following is always true?
1. LASTIN = LASTPOST
2. LASTIN = LASTPRE
3. LASTPRE = LASTPOST
4. None of these

Solution-

Consider the following complete binary tree-

Preorder Traversal : B , A , E
Inorder Traversal : B , A , E
Postorder Traversal : B , E , A

Clearly, LASTIN = LASTPRE.


Thus, Option (B) is correct.

Problem-04:

Which of the following binary trees has its inorder and preorder traversals as BCAD and ABCD
respectively-
Solution-

Option (D) is correct.

Binary Search Tree-

Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.

In a binary search tree (BST), each node contains-


• Only smaller values in its left sub tree
• Only larger values in its right sub tree
Example-

Number of Binary Search Trees-

Example-

Number of distinct binary search trees possible with 3 distinct keys


= 2×3C3 / 3+1

= 6C3 / 4

=5

If three distinct keys are A, B and C, then 5 distinct binary search trees are-
Binary Search Tree Construction-

Let us understand the construction of a binary search tree using the following example-

Example-

Construct a Binary Search Tree (BST) for the following sequence of numbers- 50,
70, 60, 20, 90, 10, 40, 100

When elements are given in a sequence,


• Always consider the first element as the root node.
• Consider the given elements and insert them in the BST one by one.

The binary search tree will be constructed as explained below-

Insert 50-

Insert 70-

• As 70 > 50, so insert 70 to the right of 50.


Insert 60-

• As 60 > 50, so insert 60 to the right of 50.


• As 60 < 70, so insert 60 to the left of 70.

Insert 20-

• As 20 < 50, so insert 20 to the left of 50.


Insert 90-

• As 90 > 50, so insert 90 to the right of 50.


• As 90 > 70, so insert 90 to the right of 70.

Insert 10-

• As 10 < 50, so insert 10 to the left of 50.


• As 10 < 20, so insert 10 to the left of 20.

Insert 40-

• As 40 < 50, so insert 40 to the left of 50.


• As 40 > 20, so insert 40 to the right of 20.
Insert 100-

• As 100 > 50, so insert 100 to the right of 50. • As 100 > 70, so insert 100 to the right of 70.
• As 100 > 90, so insert 100 to the right of 90.

You might also like