Why Tree Data Structure?
Why Tree Data Structure?
Why Tree Data Structure?
A Tree
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
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-
Tree Traversal-
Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.
1. Preorder Traversal-
Algorithm-
Example-
Applications-
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)
Example-
Keep a plane mirror horizontally at the bottom of the tree and take the projection of all the nodes.
Application-
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
• 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.
Problem-01:
If the binary tree in figure is traversed in inorder, then the order in which the nodes will be visited is
____?
Solution-
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
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-
Preorder Traversal : B , A , E
Inorder Traversal : B , A , E
Postorder Traversal : B , E , A
Problem-04:
Which of the following binary trees has its inorder and preorder traversals as BCAD and ABCD
respectively-
Solution-
Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.
Example-
= 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
Insert 50-
Insert 70-
Insert 20-
Insert 10-
Insert 40-
• 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.