You are on page 1of 27

Trees in Java

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Agenda

• Introduction to tree and the different terminologies used in a tree.


• Introduction to Binary Search Tree – Working with an example
• Tree vs Binary search tree
• What is the use of Trees?
• Operations on Binary search tree
• Different traversals such as Preorder, Inorder, and Postorder
• Java Implementation for 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
Introduction to Tree

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Tree

• Tree is a data structure in which we store data in a hierarchical format.


• Tree is not a linear data structure.
• In the tree, we do not store data in sequence.
• Tree contains data stored in multiple levels.
• Tree can also be defined as a collection of nodes, where the node contains data and links to child
nodes.

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Tree

Important Terminologies associated with the tree are as below:-


• Root
• Parent
• Edge
• Child
• Leaf Node
• Siblings

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

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

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM
Leaf Node
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Binary Search Tree

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.

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Binary Search Tree

15

12 20

10 14 18 25

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

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.

• Binary Search Tree


– Binary Search Tree a node can either have 0 or utmost 2 nodes.
– It follows some sort of order while arranging the elements.
– The values in the left subtree are always less than the parent node and values in the right
subtree greater than that of a parent node.

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

• The time needed to perform an operation on a tree is O(log N)

• Ordered arrays are bad at Insertions/Deletions

• Also searching for items in Linked lists is slow

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

• Traversals such as Preorder, Inorder, and Postorder

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

• It is a technique of tree traversing in which we visit the root node first.

• Following steps are involved in Preorder traversal


1. Visit the root.
2. Visit all the nodes on the left side
3. Visit all the nodes on the right side

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Preorder Traversal

15

12 20

10 14 18 25

15, 12, 10, 14, 20, 18, 25


DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM

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.

• Following steps are involved in Inorder traversal


1. Visit all the nodes on the left side
2. Visit the root.
3. Visit all the nodes on the right side

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Inorder Traversal

15

12 20

10 14 18 25

10, 12, 14, 15, 18, 20, 25


DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM

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.

• Following steps are involved in postorder traversal


1. Visit all the nodes on the left side
2. Visit all the nodes on the right side
3. Visit the root.

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Postorder Traversal

15

12 20

10 14 18 25

10, 14, 12, 18, 25, 20, 15 DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

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

DO NOT WRITE ANYTHING


HERE. LEAVE THIS SPACE FOR
WEBCAM

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary

• Introduction to tree and the different terminologies used in a tree.


• Introduction to Binary Search Tree – Working with an example
• Tree vs Binary search tree
• What is the use of Trees?
• Operations on Binary search tree
• Different traversals such as Preorder, Inorder, and Postorder
• Java Implementation for 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

You might also like