Binary Tree
Binary Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Agenda
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Binary Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Binary Tree
• A tree where a node can have 0 to maximum 2 children is called a Binary Tree.
• Binary Tree does not store the data in sequential memory locations.
• Binary Tree is a non-linear data structure where the data is stored in a hierarchical format.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Binary Tree
EDGE(LINK)
LEVEL 1
PARENT NODE B E
LEVEL 2
CHILD NODE C D F G
SIBLINGS
DO NOT WRITE ANYTHING
LEAF NODE HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Why Binary Tree?
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Why Binary Tree?
Tree Binary
Tree
• A binary tree can have maximum of 2 nodes as child nodes. DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Why Binary Tree?
Advantages
• Solves the problem when we want to store the information in a hierarchical form.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Binary Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Binary Tree
• Full Binary Tree - it’s a Binary Tree where in every node has either 0 or 2 children.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Binary Tree
• Complete Binary Tree - where all the levels are completely filled with nodes except the level which
is at last and in the last level, all the nodes tend to be on the left side as much as possible.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Binary Tree
• Perfect Binary Tree - is a Binary Tree where all the internal nodes have 2 child nodes and all the leaf
nodes must be at the same level.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Applications of Binary Trees
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Applications of Binary Trees
• Decision Tree – is a supervised machine learning algorithm which will use a binary tree, for the
decision – making process.
• Expression Evaluation.
• Sorting.
• Data Compression.
• Routing Tables.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode for Binary Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode Binary Tree
• Pseudocode
struct node *NodeNew( int data) void insert(struct node *r, intdata)
{ {
struct node *Node struct node *tmp
Node.data = data if(tmp.left == NULL){
Node.left = NULL temp.left = Nodenew(data)
Node.right = NULL if(tmp.right == NULL){
} temp.right = Nodenew(data)
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode Binary Tree
• Pseudocode
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Preorder Traversal
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Preorder Traversal
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Preorder Traversal
15
12 20
10 14 18 25
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inorder Traversal
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inorder Traversal
• It is a technique of tree traversing in which we visit the root node after visiting the left side of the
tree and before the traversing the right side of the tree.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inorder Traversal
15
12 20
10 14 18 25
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Postorder Traversal
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Postorder Traversal
• It is a technique of tree traversing in which we visit the root node at last after visiting all the nodes
on the left side and all the nodes on the right side.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Postorder Traversal
15
12 20
10 14 18 25
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Binary Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Binary Search Tree
• Binary search tree is a special type of tree in which every node in the tree can have a maximum of 2
child nodes.
• In this type of tree data on the left side of every node must be small and data on the right- hand
side of every node must be bigger than data in the root node.
• We use a binary search tree when our main operation is frequent searching.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Binary Search Tree
15
12 20
10 14 18 25
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operations on BST
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operations on BST
• Insert
• Delete
• Search
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode for BST
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode Binary Search Tree
• Pseudocode
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode Binary Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pseudocode Binary Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
• We discussed an introduction for a Binary Tree, how it looks and different terminologies associated
with it.
• We looked at different uses of a Binary Tree and also the different types such as Full, Complete and
Perfect Binary Trees.
• We learnt the different applications of a Binary Tree and also the Pseudocode was discussed.
• We discussed different types of traversal methods such as pre-order, in-order and post-order.
• We discussed Binary Search Tree, its uses and also the Pseudocode for a Binary Search Tree.
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited