fork download
  1. import java.util.Scanner;
  2.  
  3.  
  4. class Node
  5. {
  6. Integer data;
  7. Node left;
  8. Node right;
  9. Node()
  10. {
  11. data = null;
  12. left = null;
  13. right = null;
  14. }
  15. }
  16. class BinaryTree
  17. {
  18. Node head;
  19. Scanner input = new Scanner(System.in);
  20. BinaryTree()
  21. {
  22. head = null;
  23. }
  24.  
  25. public void createNode(Node temp,Node newnode)
  26. {
  27.  
  28. if(head==null)
  29. {
  30. System.out.println("No value exist in tree, the value just entered is set to Root");
  31. head = newnode;
  32. return;
  33. }
  34. if(temp==null)
  35. temp = head;
  36.  
  37. System.out.println("where you want to insert this value, l for left of ("+temp.data+") ,r for right of ("+temp.data+")");
  38. char inputValue=input.next().charAt(0);
  39. if(inputValue=='l'){
  40. if(temp.left==null)
  41. {
  42. temp.left=newnode;
  43. System.out.println("value got successfully added to left of ("+temp.data+")");
  44. return;
  45. }else {
  46. System.out.println("value left to ("+temp.data+") is occupied 1by ("+temp.left.data+")");
  47. createNode(temp.left,newnode);
  48. }
  49. }
  50. else if(inputValue=='r')
  51. {
  52. if(temp.right==null)
  53. {
  54. temp.right=newnode;
  55. System.out.println("value got successfully added to right of ("+temp.data+")");
  56. return;
  57.  
  58. }else {
  59. System.out.println("value right to ("+temp.data+") is occupied by ("+temp.right.data+")");
  60. createNode(temp.right,newnode);
  61. }
  62.  
  63. }else{
  64. System.out.println("incorrect input plz try again , correctly");
  65. return;
  66. }
  67.  
  68. }
  69. public Node generateTree(){
  70. int [] a = new int[10];
  71. int index = 0;
  72. while(index<a.length){
  73. a[index]=getData();
  74. index++;
  75. }
  76. if(a.length==0 ){
  77. return null;
  78. }
  79. Node newnode= new Node();
  80. /*newnode.left=null;
  81.   newnode.right=null;*/
  82. return generateTreeWithArray(newnode,a,0);
  83.  
  84. }
  85. public Node generateTreeWithArray(Node head,int [] a,int index){
  86.  
  87. if(index >= a.length)
  88. return null;
  89. System.out.println("at index "+index+" value is "+a[index]);
  90. if(head==null)
  91. head= new Node();
  92. head.data = a[index];
  93. head.left=generateTreeWithArray(head.left,a,index*2+1);
  94. head.right=generateTreeWithArray(head.right,a,index*2+2);
  95. return head;
  96. }
  97.  
  98. public Integer getData()
  99. {
  100. System.out.println("Enter the value to insert:");
  101. return (Integer)input.nextInt();
  102. }
  103.  
  104. public void print()
  105. {
  106. inorder(head);
  107. }
  108. public void inorder(Node node)
  109. {
  110. if(node!=null)
  111. {
  112. inorder(node.left);
  113. System.out.println(node.data);
  114. inorder(node.right);
  115. }
  116. else
  117. return;
  118. }
  119. }
  120.  
  121. public class BinaryTreeWorker
  122. {
  123. static BinaryTree treeObj = null;
  124. static Scanner input = new Scanner(System.in);
  125. public static void displaymenu()
  126. {
  127. int choice;
  128. do{
  129. System.out.print("\n Basic operations on a tree:");
  130. System.out.print("\n 1. Create tree \n 2. Insert \n 3. Search value \n 4. print list\n 5. generate a tree \n Else. Exit \n Choice:");
  131. choice = input.nextInt();
  132.  
  133. switch(choice)
  134. {
  135. case 1:
  136. treeObj = createBTree();
  137. break;
  138. case 2:
  139. Node newnode= new Node();
  140. newnode.data = getData();
  141. newnode.left=null;
  142. newnode.right=null;
  143. treeObj.createNode(treeObj.head,newnode);
  144. break;
  145. case 3:
  146. //searchnode();
  147. break;
  148. case 4:
  149. System.out.println("inorder traversal of list gives follows");
  150. treeObj.print();
  151. break;
  152. case 5:
  153. Node tempHead = treeObj.generateTree();
  154. System.out.println("inorder traversal of list with head = ("+tempHead.data+")gives follows");
  155. treeObj.inorder(tempHead);
  156. break;
  157. default:
  158. return;
  159. }
  160. }while(true);
  161. }
  162. public static Integer getData()
  163. {
  164. System.out.println("Enter the value to insert:");
  165. return (Integer)input.nextInt();
  166. }
  167. public static BinaryTree createBTree()
  168. {
  169. return new BinaryTree();
  170. }
  171. public static void main(String[] args)
  172. {
  173. displaymenu();
  174. }
  175. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:121: error: class BinaryTreeWorker is public, should be declared in a file named BinaryTreeWorker.java
public class BinaryTreeWorker
       ^
1 error
stdout
Standard output is empty