Dsa Lab 08-2024.Docx
Dsa Lab 08-2024.Docx
Objective
Learning about implementation of following concepts
• Binary Tree
• Binary Tree Traversal
o Pre-Order traversal
o In-Order traversal
o Post-Order traversal
Binary Trees
A binary tree T is defined as a finite set of elements, called nodes, such that
a) T is empty (called the null tree or empty tree), or
b) T contains a distinguished node R, called the root of T, and the remaining nodes of T
form an ordered pair of disjoint binary trees T1 and T2.
c) A tree whose elements have at most 2 children is called a binary tree. Since each
element in a binary tree can have only 2 children, we typically name them the left and
right child.
Example
In Java, we can represent a tree node using class Node. Below is an example of a tree node with
an integer data.
(a) Preorder
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Figure 1:This method is member of the BinaryTree.java class
(b) Inorder
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)Postorder
Submission Guidelines
• Write your codes in different notepad (.java) files with question number e.g. Q1.java
• Place all files in a folder named your roll number e.g. cs162XXX.
• Compress all files into one (.zip) file named same as your roll number.
• Upload it on LMS
• -100 policies for plagiarism