0

I can't figure out the last part. can someone help me. how do I write a code so it creates a new output file called "output.txt" displays the output in "output.txt". i tried 4 different ways but i still cannot figure it out. I've looked at multiple articles online as well and i'm just getting confused.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

//class to represent the AVL tree node
class Node {
  int key, height;
  Node left, right;

  Node(int d) {
    key = d;
    height = 1;
  }
}

//public class to represent an AVLTree
public class Main {
  Node root;
  int height(Node N) {
    if (N == null)
      return 0;
    
    return N.height;
  }

  // method to find maximum of two numbers
  int max(int a, int b) {
    return (a > b) ? a : b;
  }

  // a method to perform right rotation
  Node rightRotate(Node y) {
    Node x = y.left;
    Node T2 = x.right;
    
    // Perform rotation
    x.right = y;
    y.left = T2;

    // Update heights
    y.height = max(height(y.left), height(y.right)) + 1;
    x.height = max(height(x.left), height(x.right)) + 1;
    
    // Return new root
    return x;
  }

  // a method for left rotation
  Node leftRotate(Node x) {
    Node y = x.right;
    Node T2 = y.left;
    
    // Perform rotation
    y.left = x;
    x.right = T2;
    
    // Update heights
    x.height = max(height(x.left), height(x.right)) + 1;
    y.height = max(height(y.left), height(y.right)) + 1;

    // Return new root
    return y;
  }

  // returns balance factor of a node
  int getBalance(Node N) {
    if (N == null)
      return 0;
      
    return height(N.left) - height(N.right);
  }

  Node insert(Node node, int key) {
    if (node == null)
      return (new Node(key));
    if (key < node.key)
      node.left = insert(node.left, key);
    else if (key > node.key)
      node.right = insert(node.right, key);
    
    node.height = 1 + max(height(node.left),height(node.right));
    int balance = getBalance(node);

    // if |balace| > 1 then we perform rotation
    // rotation type is based on the value of balance factor

    if (balance > 1 && key < node.left.key)
      return rightRotate(node);
    if (balance < -1 && key > node.right.key)
      return leftRotate(node);
    if (balance > 1 && key > node.left.key) {
      node.left = leftRotate(node.left);
      return rightRotate(node);
    }
    if (balance < -1 && key < node.right.key) {
      node.right = rightRotate(node.right);
      return leftRotate(node);
    }
    return node;
  }

  //method to print the level order of tree
  void printLevelOrder() {
    int h = root.height;
    int i;

    //print all levels of tree
    for (i=1; i<=h; i++) {
      printLevel(root, i);
      System.out.println();
    }
  }

  // method print a level
  void printLevel (Node root ,int level) {
    if (root == null)
      return;

    if (level == 1)
      System.out.print("( "+ root.key + " , "+ root.height +", "+ getBalance(root) +")");
    else if (level > 1) {
      printLevel(root.left, level-1);
      printLevel(root.right, level-1);
    }
  }

  //main method
  public static void main(String[] args) throws IOException {
    //declare a empty tree
    Main tree = new Main();
    //read a input file and insert all the values in AVL tree root
    Scanner scanner = new Scanner(new File("input.txt"));
    FileWriter fw = new FileWriter(new File("output.txt"));
    while(scanner.hasNextInt()) {
      int x = scanner.nextInt();
      //insert x into AVL tree
      tree.root = tree.insert(tree.root, x);
    }

    //Now print the level order of tree
    tree.printLevelOrder();
  }
}

1 Answers1

0

You create a FileWriter but you never use it, you still print out your output to the console using System.out.

Instead you need to use your file output. Here's a way to do it using a PrintStream (the same class as System.out and System.err to minimize changes to your code). First, your output methods need to take a PrintStream as a parameter so they can use it for their output:

  //method to print the level order of tree
  void printLevelOrder(PrintStream out) {
    int h = root.height;
    int i;

    //print all levels of tree
    for (i=1; i<=h; i++) {
      printLevel(root, i, out);

      out.println();
    }
  }

  // method print a level
  void printLevel(Node root, int level, PrintStream out) {
    if (root == null)
      return;

    if (level == 1)
      out.print("( "+ root.key + " , "+ root.height +", "+ getBalance(root) +")");
    else if (level > 1) {
      printLevel(root.left, level-1, out);
      printLevel(root.right, level-1, out);
    }
  }

Then you can use it this way, in your main() method:

    //Now print the level order of tree
    tree.printLevelOrder(System.out); // print to console

    PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
    tree.printLevelOrder(out); // print to file
vmallet
  • 479
  • 3
  • 11