ans................................................
output:
code to copy:
import java.util.Scanner;
public class PlotBinaryTree {
static class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data)
{
this.data=data;
left=right=null;
}
TreeNode()
{
}
}
static void inorder(TreeNode root)
{
if(root==null)
return;
inorder(root.left);
System.out.print(root.data+" ");
inorder(root.right);
}
static void preorder(TreeNode root)
{
if(root==null)
return;
System.out.print(root.data+" ");
preorder(root.left);
preorder(root.right);
}
static int pow(int a,int b)
{
int c=1;
for(int i=0;i<b;i++)
c=c*a;
return c;
}
public static void plot(int tree[][],int no)
{
int level[]=new int[16];
int index[]=new int[16];
level[0]=0;
index[0]=8;//For tree with maximum 15 nodes
int isNode[]=new int[16];
isNode[0]=1;
for(int i=0;i<16;i++)
{
if(tree[i][1]!=-1)
{
isNode[tree[i][1]]=1;
level[tree[i][1]]=level[i]+1;
index[tree[i][1]]=index[i]-pow(2,2-level[i]);
}
if(tree[i][2]!=-1)
{
isNode[tree[i][2]]=1;
level[tree[i][2]]=level[i]+1;
index[tree[i][2]]=index[i]+pow(2,2-level[i]);
}
}
int nodes=0;
for(int i=1;i<16;i++)
{
if(level[i]==0)
level[i]=-1;
if(isNode[i]==1)
nodes++;
}
System.out.println("The array for the binary tree is as
follows:");
System.out.println("Index Data Left-link Right-link");
System.out.println("----- ---- --------- ----------");
for(int i=0;i<16;i++)
{
if(isNode[i]==1)
{
System.out.println(i+"\t"+tree[i][0]+"\t"+tree[i][1]+"\t"+tree[i][2]);
}
else
{
System.out.println(i);
}
}
System.out.println("The binary tree is plotted as follows:");
for(int i=0;i<4;i++)
{
String line="";
for(int j=0;j<16;j++)
{
int f=0;
for(int k=0;k<16;k++)
{
if(level[k]==i&&index[k]==j)
{
f=1;
line+=tree[k][0];
}
}
if(f==0)
line+="_";
}
line+="_";
if(!line.equals("_________________"))
System.out.println(line);
}
TreeNode t[]=new TreeNode[16];
for(int i=0;i<16;i++)
t[i]=new TreeNode();
for(int i=0;i<16;i++)
{
if(tree[i][0]!=-1)
{
isNode[i]=1;
t[i].data=tree[i][0];
if(tree[i][1]!=-1)
t[i].left=t[tree[i][1]];
if(tree[i][2]!=-1)
t[i].right=t[tree[i][2]];
}
}
System.out.print("In-Order traversal of this binary tree: ");
inorder(t[0]);
System.out.println();
System.out.print("Pre-Order traversal of this binary tree:
");
preorder(t[0]);
System.out.println();
}
public static void main(String[] args) {
int tree1[][]=new
int[][]{{5,1,2},{3,3,4},{7,5,6},{2,7,-1},{4,-1,-1},{6,-1,-1},{9,8,9},{1,-1,-1},{8,-1,-1},{10,-1,-1},{-1,-1,-1},{-1,-1,-1},{-1,-1,-1},{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
int tree2[][]=new int[16][3];
int tree3[][]=new int[16][3];
Scanner sc=new Scanner(System.in);
System.out.println("Welcome to play this Binary Tree Tool designed
by Dr. Simon Lin!\n");
while(true)
{
System.out.println("Please enter a number for the binary tree: (0
is to stop the tool ,we have tree 1,2,3 to pick)");
int n=sc.nextInt();
if(n==0)
break;
else if(n==1)
plot(tree1,1);
else if(n==2)
plot(tree2,2);
else if(n==3)
plot(tree3,3);
else
System.out.println("Invalid input...Try again");
}
System.out.println("Thank you for using this Binary Tree Tool
designed by Dr. Simon Lin!");
}
}
Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...
Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...
Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...
Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...
JAVA Given the sequence of in-order traversal for a general binary tree, stored in an array inOrder[] = {9, 5, 1, 7, 2, 12, 8, 4, 3, 11 }. And given the sequence of post-order traversal for the same tree, stored in an array postOrder[] = {9, 1, 2, 12, 7, 5, 3, 11, 4, 8}. Please implement the following method: static BinaryTree buildTree(Object inOrder[], Object postOrder[]) -The method buildTree() returns a BinaryTree object in memory that is constructed on...
1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only). 2- Apply your proposed algorithm in the previous point to construct the binary tree with the following traversals (java code only): In order traversal: 9 8 6 1 2 5 4 Postorder traversal: 9 6 1 8 5 4 2
3. (8 points) Using the implementation of binary search tree operations we discussed in class, draw the trees that result from the following operations: (a) Inserting 142, 400, 205, 127, 100, 320, 160, 141, and 110 into an initially-empty tree (in that order). (b) Deleting 142 from the tree you drew for part (a). 4. (8 points) Draw the unique binary tree that has a preorder traversal of 4, 1, 6, 3, 7, 5, 9, 2, 8 and an inorder...
Data structures Exercises: For the following binary tree (Index-Value): 0 1 2 3 4 5 6 7 8 9 A C E G B P D X F H Give the pre-order traversal. Give the post-order traversal. Give the in-order traversal. Determine the height of the tree. Using these values: 8 6 4 3 5 9 2 1 6 Build a binary search tree. Build an AVL Tree. Build a 2-3 Tree. Build a min-heap. Build a max-heap. Apply a...
(4 marks) Consider the binary tree shown in Figure 8. Perform the iterative pre-order tree traversal using a stack as shown to you in class. Write your answers in the U- shaped diagrams in Figure 9. For the first stack, show the value on the stack before entering the loop. For the rest of the stacks, show the values on the stack at the end of each loop iteration. 42 3 67 19 19 35 12 29 55 60 54...
Question 25 3 pts Add the following values (in the order provided) to a binary search tree and provide the values in the order you would get if you did an inorder traversal of the tree. Values: 6, 3, 7,4,9,1,0, 8, 5, 2 Question 26 3 pts Add the following values (in the order provided) to a binary search tree and provide the values in the order you would get if you did an postorder traversal of your tree. Values:...
Tree & Hash Table & Heap Use the following integer keys 73, 58, 91, 42, 60, 130, 64, 87 to perform the followings: a) Binary Search Tree - Draw a binary search tree - Retrieve the integers keys in post-order - Retrieve the integers keys in pre-order - Draw a binary search tree after node 58 is deleted b) Create a Hash Table using the methods described below. Show the final array after all integer keys are inserted. Assumes that...