eightmoons

Tree.java

Nov 9th, 2020 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5. public class Tree {
  6.     Node root;
  7.  
  8.     static class Node {
  9.         int value;
  10.         Node left, right;
  11.  
  12.         Node(int value) {
  13.             this.value = value;
  14.             this.left = null;
  15.             this.right = null;
  16.         }
  17.     }
  18.  
  19.     public void inOrder(Node root) {
  20.         if (root != null) {
  21.             inOrder(root.left);
  22.             System.out.print(root.value + " ");
  23.             inOrder(root.right);
  24.         }
  25.     }
  26.  
  27.     void preOrder(Node root) {
  28.         if (root != null) {
  29.             System.out.print(root.value + " ");
  30.             preOrder(root.left);
  31.             preOrder(root.right);
  32.         }
  33.     }
  34.  
  35.     void postOrder(Node root) {
  36.         if (root != null) {
  37.             postOrder(root.left);
  38.             postOrder(root.right);
  39.             System.out.print(root.value + " ");
  40.         }
  41.     }
  42.  
  43.     public Node insertLevelOrder(int[] arr, Node root, int i) {
  44.         if (i < arr.length) {
  45.             Node temp = new Node(arr[i]);
  46.             root = temp;
  47.             root.left = insertLevelOrder(arr, root.left,
  48.                     2 * i + 1);
  49.             root.right = insertLevelOrder(arr, root.right,
  50.                     2 * i + 2);
  51.         }
  52.         return root;
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         Tree binaryTree = new Tree();
  57.         Scanner scan = new Scanner(System.in);
  58.         ArrayList<Integer> input = new ArrayList<>();
  59.         System.out.println("Enter integers [n to stop]");
  60.         int counter = 1;
  61.         do {
  62.             try {
  63.                 System.out.print(counter + ". >> ");
  64.                 input.add(scan.nextInt());
  65.                 counter++;
  66.             } catch (InputMismatchException exception) {
  67.                 break;
  68.             }
  69.         } while (true);
  70.         binaryTree.root = binaryTree.insertLevelOrder(input.stream().mapToInt(Integer::intValue).toArray(), binaryTree.root, 0);
  71.         System.out.print("Full binary tree: ");
  72.         for (int num : input) {
  73.             System.out.print(num + " ");
  74.         }
  75.         System.out.print("\nInOrder Traversal: ");
  76.         binaryTree.inOrder(binaryTree.root);
  77.         System.out.print("\nPreOrder Traversal: ");
  78.         binaryTree.preOrder(binaryTree.root);
  79.         System.out.print("\nPostOrder Traversal: ");
  80.         binaryTree.postOrder(binaryTree.root);
  81.     }
  82. }
Add Comment
Please, Sign In to add comment