0% found this document useful (0 votes)
25 views26 pages

Tree

Tree data structures can be traversed in different ways. Depth-first traversal involves exploring nodes down one path as far as possible before backtracking. There are three types of depth-first traversal: preorder, inorder, and postorder. Preorder visits the root node first, then left subtree, then right subtree. Inorder visits left subtree, then root, then right subtree. Postorder visits left subtree, right subtree, then root node last. Binary search trees store nodes such that all left descendants are less than the parent node and all right descendants are greater.

Uploaded by

israth.sumona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views26 pages

Tree

Tree data structures can be traversed in different ways. Depth-first traversal involves exploring nodes down one path as far as possible before backtracking. There are three types of depth-first traversal: preorder, inorder, and postorder. Preorder visits the root node first, then left subtree, then right subtree. Inorder visits left subtree, then root, then right subtree. Postorder visits left subtree, right subtree, then root node last. Binary search trees store nodes such that all left descendants are less than the parent node and all right descendants are greater.

Uploaded by

israth.sumona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Tree

Data Structure
Terminology of tree
Types of Tree Traversal:

Level Order Traversal:


Depth First Traversal:
In depth-first traversal, we go in one direction up to the bottom first and then come back and go to the other
direction. There are three types of depth-first traversals. These are:
1. Preorder traversal
2. Inorder traversal
3. Postorder traversal
A binary tree is a recursive data structure. Also, each binary tree has 3 parts: a root node, a left subtree and a
right subtree. These left and right subtrees are also trees in themselves and thus, again further described
recursively.
Preorder Traversal:

Algorithm for Preorder Traversal:


for all nodes of the tree:
Step 1: Visit the root node.
Step 2: Traverse left subtree recursively.
Step 3: Traverse right subtree recursively.

Uses of Preorder Traversal:


If we want to create a copy of a tree, we make use of preorder
traversal.
Preorder traversal helps to give a prefix expression for the
A→B→C→D→E→F→G→H→I. expression tree.
Inorder Traversal:

Algorithm for Inorder Traversal:


for all nodes of the tree:
Step 1: Traverse left subtree recursively.
Step 2: Visit the root node.
Step 3: Traverse right subtree recursively.

Uses of Inorder Traversal:


Used to print the nodes of a binary search tree in non-decreasing
order.
We can also use the inorder traversal to get the non-increasing
order of a BST.
30→20→40→10→50→70→60→80.
Postorder Traversal:

Algorithm for Postorder Traversal:


for all nodes of the tree:

Step 1: Traverse left subtree recursively.


Step 2: Traverse right subtree recursively.
Step 3: Visit the root node.

Uses of Postorder Traversal:


It helps to delete the tree.
7→5→4→20→60→30→10. It helps to get the postfix expression in an expression tree.

Tree implementation in C
What is a Binary Search tree?

Advantages of Binary search tree


•Searching an element in the Binary search
tree is easy as we always have a hint that
which subtree has the desired element.

•As compared to array and linked lists,


insertion and deletion operations are faster
in BST.

In the above figure, we can observe that the root node is 40, and all the nodes of the left
subtree are smaller than the root node, and all the nodes of the right subtree are greater than
the root node.
Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

You might also like