0% found this document useful (0 votes)
47 views

Data Structure Assignment 2

The document contains code for a BinaryTree class with methods to calculate the height and size of a binary tree, perform level, in-order, pre-order and post-order traversals of a binary tree, and a main method that builds a sample binary tree and calls the methods.

Uploaded by

Muhammad Asad
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)
47 views

Data Structure Assignment 2

The document contains code for a BinaryTree class with methods to calculate the height and size of a binary tree, perform level, in-order, pre-order and post-order traversals of a binary tree, and a main method that builds a sample binary tree and calls the methods.

Uploaded by

Muhammad Asad
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

Name: Fazeel Ahmed Khan

Class: BS Software (Part II)

Roll No: 2k19/SWE/35

Subject: Data Structure & Algorithms

Submitted to: Dr. Niaz Arijo

Date: 16-7-2020
Assignment 2
import java.util.Queue;
import java.util.LinkedList;

public class BinaryTree{

public Node root;

BinaryTree(int item){
root = new Node(item);
}
BinaryTree(){
root = null;
}

public static int Height(Node tree){


if(tree == null){
return 0;
}
return 1+Math.max(Height(tree.left),Height(tree.right));
}
public static int size(Node tree){
if(tree == null){
return 0;
}
return 1+size(tree.left)+size(tree.right);
}
public static void LevelTraversal(Node tree){
Queue<Node> queue = new LinkedList<Node>();
queue.add(tree);

while(!queue.isEmpty()){
Node tempNode = queue.poll();
System.out.print(tempNode.data+" ");

if(tempNode.left != null){
queue.add(tempNode.left);
}
if(tempNode.right != null){
queue.add(tempNode.right);
}

}
}
public static void inOrderTraversal(Node tree){
if(tree == null) return;
inOrderTraversal(tree.left);
System.out.print(tree.data+" ");
inOrderTraversal(tree.right);
}
public static void preOrderTraversal(Node tree){
if(tree == null) return;
System.out.print(tree.data+" ");
preOrderTraversal(tree.left);
preOrderTraversal(tree.right);
}
public static void PostOrderTraversal(Node tree){
if(tree == null) return;
PostOrderTraversal(tree.left);
PostOrderTraversal(tree.right);
System.out.print(tree.data+" ");
}
public static void main(String ...args){
BinaryTree tree = new BinaryTree();

tree.root = new Node(1);

tree.root.left = new Node(2);


tree.root.right = new Node(3);
tree.root.left.right = new Node(5);
tree.root.left.left = new Node(4);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
tree.root.left.left.left = new Node(8);
tree.root.left.left.right = new Node(9);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(11);
tree.root.right.left.left = new Node(12);
tree.root.right.left.right = new Node(13);
tree.root.right.right.left = new Node(14);
tree.root.right.right.right = new Node(15);

System.out.println(Height(tree.root));
System.out.println(size(tree.root));
LevelTraversal(tree.root);
System.out.println();
inOrderTraversal(tree.root);
System.out.println();
preOrderTraversal(tree.root);
System.out.println();
PostOrderTraversal(tree.root);
}
}

class Node{
int data;
Node left, right;
Node(int item){
data = item;
left = right = null;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy