Page 1 of 42
Linear Data Structure:
“A data structure is a linear data structure if its data items(elements) form a sequence or a linear list”.
In plain English, linear data structures have a logical beginning and a logical end. Examples: array, stack, linked list, queue.
Non-Linear Data Structure:
Unlike linear data structures, elements of a non-linear data structure do not form a sequence because of that they can be traversed in any desired order or non-sequential order. Examples:
Tree, Graph etc.
Page 2 of 42
Let’s take some of the real life examples.
Here the Stark Family Tree from Game of Thrones.
Game of Thrones: Stark family tree (Source)
By looking at this tree, we can easily define relationships between
each character.
Page 3 of 42
We can say that
Rickardand
Unknown(yet to be revealed) are grandparents of
Aryaand her siblings i.e.
Robb,
Sansa,
BranRickon, and
Eddard&
Catelynare their parents.
Also
Aryaand
Jon Snoware cousin cause their parents are siblings. I hope Jon Snow at least knows this 😄.
Here’s another example which shows tree of a directory on file
system.
Page 4 of 42
Tree - Terminology
In linear data structure data is organized in sequential order and in non-linear data structure data is organized in random order. A tree is a very popular non-linear data structure used in a wide range of applications. A tree data structure can be defined as follows...
Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.
In tree data structure, every individual element is called as Node. Node in a tree data structure stores the actual data of that particular element and link to next element in hierarchical structure.
In a tree data structure if we have N number of nodes then we can have a maximum of N-1 number of links.
Page 5 of 42
What are trees?
Page 6 of 42
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 notacceptable in today's computational world.
Different tree data structures allow quicker and easier access to the data as it is a non-linear data structure.
Example
Page 7 of 42
Terminology
In a tree data structure, we use the following terminology...
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have a root node. We can say that the root node is the origin of the tree data structure. In any tree, there must be only one root node. We never have multiple root nodes in a tree.
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
Page 8 of 42
3. Parent
In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE. In simple words, the node which has a branch from it to any other node is called a parent node. Parent node can also be defined as "The node which has child / children".
4. Child
Page 9 of 42 In a tree data structure, the node which is descendant of any node is called as CHILD Node. In simple words, the node which has a link from its parent node is called as child node. In a tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes.
5. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple words, the nodes with the same parent are called Sibling nodes.
6. Leaf
Page 10 of 42 In a tree data structure, the node which does not have a child is called as LEAF Node. In simple words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a node with no child. In a tree, leaf node is also called as 'Terminal' node.
7. Internal Nodes
In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In simple words, an internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-Terminal' nodes.
8. Degree
Page 11 of 42 In a tree data structure, the total number of children of a node is called as DEGREE of that Node. In simple words, the Degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'
9. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In simple words, in a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step).
Page 12 of 42
10. Height
In a tree data structure, the total number of edges from leaf node to a particular node in the longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of the tree. In a tree, height of all leaf nodes is '0'.
11. Depth
In a tree data structure, the total number of egdes from root node to a particular node is called as DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
Page 13 of 42
12. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J has length 4.
13. Sub Tree
In a tree data structure, each child from a node forms a subtree recursively. Every child node will form a subtree on its parent node.
Page 14 of 42
Tree Applications
Representing hierarchical data.
Storing data in a way that makes it efficiently searchable .
Representing sorted lists of data.
As a workflow for compositing digital images for visual effects.
Router algorithms.
Binary Search Trees(BSTs) are used to quickly check whether an element is present in a set or not.
Heap is a kind of tree that is used for heap sort.
A modified version of a tree called Tries is used in modern routers to store routing information.
Most popular databases use B-Trees and T-Trees, which are variants of the tree structure we learned above to store their data
Compilers use a syntax tree to validate the syntax of every program you write.
Page 15 of 42
Binary Tree
Binary tree is a special tree data structure in which each node can have at most 2 children. Thus, in a binary tree,
Each node has either 0 child or 1 child or 2 children.
Example-
Unlabeled Binary Tree
A binary tree is unlabeled if its nodes are not assigned any label.
Page 16 of 42
Labeled Binary Tree-
A binary tree is labeled if all its nodes are assigned a label.
Tree Traversal
Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.
Various tree traversal techniques are-
Page 17 of 42
Depth First Traversal
Following three traversal techniques fall under Depth First Traversal- 1. Preorder Traversal- MLR
2. Inorder Traversal- LMR 3. Postorder Traversal- LRM
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)
Middle(Print) → Left → Right
Example-
Consider the following example-
Page 18 of 42
Preorder Traversal Shortcut
Traverse the entire tree starting from the root node keeping yourself to the left.
Applications-
Preorder traversal is used to get prefix expression of an expression tree.
Page 19 of 42
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-
Page 20 of 42
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
Page 21 of 42
Left → Right → Root
Example-
Consider the following example-
Page 22 of 42
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-
Page 23 of 42
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.
Page 24 of 42
Page 25 of 42
Page 26 of 42
Binary Tree Properties
Important properties of binary trees are-
Property-01:
Minimum number of nodes in a binary tree of height H
= H + 1
Example-
To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.
Property-02:
Maximum number of nodes in a binary tree of height H
Page 27 of 42
= 2H+1 – 1
Example-
Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted = 15.
We can not insert more number of nodes in this binary tree.
Property-03:
Total Number of leaf nodes in a Binary Tree
= Total Number of nodes with 2 children + 1
Page 28 of 42
Example-
Consider the following binary tree-
Here,
Number of leaf nodes = 3
Number of nodes with 2 children = 2
Clearly, number of leaf nodes is one greater than number of nodes with 2 children.
This verifies the above relation.
NOTE
It is interesting to note that-
Number of leaf nodes in any binary tree depends only on the number of nodes with 2 children.
Property-04:
Page 29 of 42 Maximum number of nodes at any level ‘L’ in a binary tree
= 2L
Example-
Maximum number of nodes at level-2 in a binary tree
= 22
= 4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
Page 30 of 42
Types of Binary Trees-
Binary trees can be of the following types-
1. Rooted Binary Tree 2. Full / Strictly Binary Tree 3. Complete / Perfect Binary Tree 4. Almost Complete Binary Tree 5. Skewed Binary Tree
1. Rooted Binary Tree-
A rooted binary tree is a binary tree that satisfies the following 2 properties-
It has a root node.
Each node has at most 2 children.
Page 31 of 42 Example-
2. Full / Strictly Binary Tree-
A binary tree in which every node has either 0 or 2 children is called as a Full binary tree.
Full binary tree is also called as Strictly binary tree.
Example-
Page 32 of 42
Here,
First binary tree is not a full binary tree.
This is because node C has only 1 child.
3. Complete / Perfect Binary Tree-
A complete binary tree is a binary tree that satisfies the following 2 properties-
Every internal node has exactly 2 children.
All the leaf nodes are at the same level.
Complete binary tree is also called as Perfect binary tree.
Example-
Page 33 of 42
Here,
First binary tree is not a complete binary tree.
This is because all the leaf nodes are not at the same level.
4. Almost Complete Binary Tree-
An almost complete binary tree is a binary tree that satisfies the following 2 properties-
All the levels are completely filled except possibly the last level.
The last level must be strictly filled from left to right.
Example-
Page 34 of 42
Here,
First binary tree is not an almost complete binary tree.
This is because the last level is not filled from left to right.
5. Skewed Binary Tree-
A skewed binary tree is a binary tree that satisfies the following 2 properties-
All the nodes except one node has one and only one child.
The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Example-
Page 35 of 42
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-
Page 36 of 42
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-
Page 37 of 42 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.
Page 38 of 42
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.
Page 39 of 42
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.
Page 40 of 42
This is the required Binary Search Tree.
BST Traversal-
A binary search tree is traversed in exactly the same way a binary tree is traversed.
In other words, BST traversal is same as binary tree traversal.
Example-
Consider the following binary search tree-
Page 41 of 42
Now, let us write the traversal sequences for this binary search tree-
Preorder Traversal-
100 , 20 , 10 , 30 , 200 , 150 , 300
Inorder Traversal-
10 , 20 , 30 , 100 , 150 , 200 , 300
Postorder Traversal-
10 , 30 , 20 , 150 , 300 , 200 , 100
Important Notes-
Note-01:
Inorder traversal of a binary search tree always yields all the nodes in increasing order.
Page 42 of 42
Note-02:
Unlike Binary Trees,
A binary search tree can be constructed using only preorder or only postorder traversal result.
This is because inorder traversal can be obtained by sorting the given result in increasing order.