0% found this document useful (0 votes)
36 views6 pages

Dsa Lab 08-2024.Docx

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

Dsa Lab 08-2024.Docx

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

DHA SUFFA UNIVERSITY

Department of Computer Science


CS-2001L
Data Structures & Algorithms Lab
Fall 2024
LAB 08

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

Binary Tree Representation in Java:


A tree is represented by a reference to the topmost node in tree. If the tree is empty, then
value of root is null.
A Tree node contains following parts.
1. Data
2. Reference to left child
3. Reference to right child

In Java, we can represent a tree node using class Node. Below is an example of a tree node with
an integer data.

Creating Binary Tree


Traversing Binary Tree
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical
way to traverse them, trees can be traversed in different ways. Following are the generally used
ways for traversing trees.

Depth First Traversals:


(a) Preorder (Root, Left, Right) : 1 2 4 5 3
(b) Inorder (Left, Root, Right) : 4 2 5 1 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1

(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

Figure 2: This method is member of the BinaryTree.java class


(c) Postorder
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.

Figure 3: This method is member of the BinaryTree.java class


TASKS
Perform the following tasks for binary tree, create separate function for each task.
1. Calculate level of the binary tree.
2. Check whether a given tree is complete tree, full tree or both.
3. Check the children sum property in binary tree, i.e. for every node data values must be
equal to the sum of data values of left and right child.

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

You might also like