Professional Documents
Culture Documents
Trees in Java Deck-2
Trees in Java Deck-2
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 Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Tree
A Root
Edge
Parent B E
Siblings
Child C D F G
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to 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.
• This is also called an ordered binary tree.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Binary Search Tree
15
12 20
10 14 18 25
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Tree vs Binary Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Tree vs Binary Search Tree
• Normal Tree
– In a tree, a node can have either 0 or maximum n number of nodes.
– It does not follow any strict ordering while arranging the elements.
– Unlike a Binary Search Tree, there is no restriction on the number of nodes a parent can have.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
What is the use of Trees?
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
What is the use of Trees?
• On an average, a tree is more efficient if you need to perform many different types of operations
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operations on Binary Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operations on Binary Search Tree
• Insert
• Delete
• Search
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 of 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
Java Implementation for Binary
Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Java Implementation for Binary Search Tree
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited