0% found this document useful (0 votes)
33 views10 pages

1-Import Java

The document defines a Node class to represent nodes in a binary tree with data and left/right child pointers. It also defines a BinaryTree class with a root node and methods to construct a sample binary tree and perform preorder traversal recursively by visiting the root node, then left subtree, then right subtree. The main method creates a BinaryTree, constructs the sample tree, and calls preOrder() to output the preorder traversal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

1-Import Java

The document defines a Node class to represent nodes in a binary tree with data and left/right child pointers. It also defines a BinaryTree class with a root node and methods to construct a sample binary tree and perform preorder traversal recursively by visiting the root node, then left subtree, then right subtree. The main method creates a BinaryTree, constructs the sample tree, and calls preOrder() to output the preorder traversal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

1-import java.util.

Scanner;

import java.util.concurrent.TimeUnit;

public class Main

public static void main(String[] args)

// array can store a maximum of 100 integervalues

int array[] = new int[100];

// n variable stores number of values in array

int n;

// dup array stores the value from array and its frequency if it has duplicates

int dup[][] = new int[100][1];

// scanner object sc to read input from user

Scanner sc = new Scanner(System.in);

// prompt user to enter size of array

System.out.print("Enter the number of elements in the array: ");

// read size of array n from user

n = sc.nextInt();

// prompt user to enter array elements


System.out.println("Enter " + n + " numbers");

// read n number of array elements

for(int i = 0; i < n; i++)

System.out.print("Enter #" + (i+1) + ": ");

array[i] = sc.nextInt();

// log the starting time before starting finding largest element in array

long startTime = System.currentTimeMillis();

int flag;

System.out.println("The duplicates values in array are: ");

// traverse through the array to find duplicate values

for(int i = 0; i < n; i++)

flag = 0; // indicates whether there exists duplicates of ith element in array

if(array[i] != -1) // if ith element of array is not already verified

for(int j = i+1; j < n; j++) // traverse through array to find duplicates of ith element

if(array[i] == array[j]) // if ith element of array is equal to jth element, then atleast one
duplicate exists

{
flag = 1; // set flag variable to indicate existence of duplicates

array[j] = -1; // make jth element = -1 to indicate it is already counted for duplicates

if(flag == 1) // if ith element has duplicates, display information to user

System.out.print("\t" + array[i]);

flag = 0; // reset flag variable

// log the ending time after finding largest element in array

long endTime = System.currentTimeMillis();

// determine the execution time

long timeElapsed = endTime - startTime;

// display the execution time to user

System.out.println("\nExecution time in milliseconds: " + timeElapsed);

Q2:

import java.util.Scanner;

import java.util.concurrent.TimeUnit;
public class Main

public static void main(String[] args)

// array can store a maximum of 100 integervalues

int array[] = new int[100];

// n variable stores number of values in array

// large variable stores the largest value in array

int n, large;

// scanner object sc to read input from user

Scanner sc = new Scanner(System.in);

// prompt user to enter size of array

System.out.print("Enter the number of elements in the array: ");

// read size of array n from user

n = sc.nextInt();

// prompt user to enter array elements

System.out.println("Enter " + n + " numbers");

// read n number of array elements

for(int i = 0; i < n; i++)

{
System.out.print("Enter #" + (i+1) + ": ");

array[i] = sc.nextInt();

// log the starting time before starting finding largest element in array

long startTime = System.currentTimeMillis();

// assume that 0th element of the array is the largest element

large = array[0];

// traverse through the array from 1st element to end of array

// to determine largest number

for(int i = 1; i < n; i++)

if(array[i] > large) // if ith element of array is > than largest element,

large = array[i]; // then make ith element of array as largest element

// display the largest number in array to user

System.out.println("The largest value in the array is " + large);

// log the ending time after finding largest element in array

long endTime = System.currentTimeMillis();

// determine the execution time

long timeElapsed = endTime - startTime;


// display the execution time to user

System.out.println("Execution time in milliseconds: " + timeElapsed);

Q3:

// Node class representing a node in binary tree

class Node

// each node in binary tree contains data field, pointer to left child, pointer to right child

int data;
Node left;

Node right;

// constructor to create new node and initialize fields

// it receives d -- data of node

public Node(int d)

data = d; // initialize data field of node to d

left = null; // initialize left, right fields to null

right = null;

// BinaryTree class

class BinaryTree

// root node of binary tree

Node root;

// method to construct binary tree

// static binary tree is created as follows

// 1

// / \

// 2 5

// / \ /
// 3 4 6

public void constructTree()

root = new Node(1);

root.left = new Node(2);

root.right = new Node(5);

root.left.left = new Node(3);

root.left.right = new Node(4);

root.right.left = new Node(6);

// method for initializing preorder traversal of binary tree

public void preOrder()

preorder(root);

// method for performing preorder traversal of binary tree starting from root node

// it is a recursive method

private void preorder(Node node)

// if current node is not there, stop traversing in the current direction

if (node == null)

return;
// display the current nodes data

System.out.print(node.data + "\t");

// continue traversing to the left of current node

preorder(node.left);

// continue traversing to the right of current node

preorder(node.right);

// driver class

public class Main

public static void main(String[] args) throws Exception

// create instance of binary tree

BinaryTree bt = new BinaryTree();

// construct static binary tree

bt.constructTree();
// traverse binary tree in preorder

System.out.println("Traversing binary tree in preOrder: ");

bt.preOrder();

You might also like