0% found this document useful (0 votes)
27 views3 pages

BinaryTreel Taking Input and Output

The document defines a BinaryTreeNode class to represent nodes in a binary tree. It includes methods to take input for a binary tree in level-order and print the tree level-wise. It also includes methods to take input for a binary tree in pre-order and print the tree in pre-order traversal.

Uploaded by

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

BinaryTreel Taking Input and Output

The document defines a BinaryTreeNode class to represent nodes in a binary tree. It includes methods to take input for a binary tree in level-order and print the tree level-wise. It also includes methods to take input for a binary tree in pre-order and print the tree in pre-order traversal.

Uploaded by

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

import java.util.

*;
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left, right;

public BinaryTreeNode(T item) {


data = item;
left = right = null;
}
}
public class Main {
public static BinaryTreeNode<Integer> takeInputLevelWise() {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter root data:");


int rootData = scanner.nextInt();
if (rootData == -1) {
return null;
}

BinaryTreeNode<Integer> root = new BinaryTreeNode<>(rootData);

Queue<BinaryTreeNode<Integer>> pendingNodes = new LinkedList<>();


pendingNodes.add(root);

while (!pendingNodes.isEmpty()) {
BinaryTreeNode<Integer> front = pendingNodes.poll();

System.out.println("Enter left child of " + front.data + ":");


int leftChildData = scanner.nextInt();
if (leftChildData != -1) {
BinaryTreeNode<Integer> leftChild = new
BinaryTreeNode<>(leftChildData);
front.left = leftChild;
pendingNodes.add(leftChild);
}

System.out.println("Enter right child of " + front.data + ":");


int rightChildData = scanner.nextInt();
if (rightChildData != -1) {
BinaryTreeNode<Integer> rightChild = new
BinaryTreeNode<>(rightChildData);
front.right = rightChild;
pendingNodes.add(rightChild);
}
}

return root;
}
public static void printLevelWise(BinaryTreeNode<Integer> root) {
if (root == null) {
return;
}

Queue<BinaryTreeNode<Integer>> pendingNodes = new LinkedList<>();


pendingNodes.add(root);
pendingNodes.add(null); // Null marker for level change

while (!pendingNodes.isEmpty()) {
BinaryTreeNode<Integer> frontNode = pendingNodes.poll();

if (frontNode == null) {
System.out.println();

if (!pendingNodes.isEmpty()) {
pendingNodes.add(null); // Null marker for the next level
}
} else {
System.out.print(frontNode.data + ":");
System.out.print("L:");

if (frontNode.left != null) {
pendingNodes.add(frontNode.left);
System.out.print(frontNode.left.data + ",");
} else {
System.out.print("-1,");
}

System.out.print("R:");

if (frontNode.right != null) {
pendingNodes.add(frontNode.right);
System.out.println(frontNode.right.data);
} else {
System.out.println("-1");
}
}
}
}
public static void main(String[] args) {
BinaryTreeNode<Integer> root = takeInputLevelWise();
printLevelWise(root);
}
}

// preoder input output

import java.util.Scanner;

class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left, right;

public BinaryTreeNode(T item) {


data = item;
left = right = null;
}
}

public class BinaryTreeOperationsJava {


static int node = 1;

public static void printTree(BinaryTreeNode<Integer> root) {


if (root == null) {
return;
}

System.out.print(root.data + ":");

if (root.left != null) {
System.out.print("L" + root.left.data + ",");
}

if (root.right != null) {
System.out.print("R" + root.right.data);
}

System.out.println();
printTree(root.left);
printTree(root.right);
}

public static BinaryTreeNode<Integer> takeInput() {


Scanner scanner = new Scanner(System.in);

System.out.println("Node " + node++);


int rootData = scanner.nextInt();
if (rootData == -1) {
return null;
}

BinaryTreeNode<Integer> root = new BinaryTreeNode<>(rootData);


System.out.println("Enter left child for " + rootData + ":");
root.left = takeInput();
System.out.println("Enter right child for " + rootData + ":");
root.right = takeInput();

return root;
}

public static void main(String[] args) {


// Example Usage:
BinaryTreeNode<Integer> root = takeInput();
System.out.println("Binary tree input completed.");

System.out.println("Printing binary tree:");


printTree(root);
}
}

You might also like