SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  * @author leer
  6.  * Created at 3/15/19 8:19 PM
  7.  */
  8. public class BinaryTree<T> {
  9.   public static class TreeNode<T> {
  10.     TreeNode left, right;
  11.     Comparable<T> key;
  12.  
  13.     public TreeNode() {
  14.     }
  15.  
  16.     public TreeNode(TreeNode left, TreeNode right, Comparable<T> key) {
  17.       this.left = left;
  18.       this.right = right;
  19.       this.key = key;
  20.     }
  21.  
  22.     @Override
  23.     public String toString() {
  24.       return key.toString() + " ";
  25.     }
  26.   }
  27.  
  28.   private TreeNode root = new TreeNode();
  29.  
  30.   public BinaryTree() {
  31.     System.out.println("Input node in preOrder:");
  32.     root = buildTree(root);
  33.   }
  34.  
  35.   public BinaryTree(TreeNode root) {
  36.     this.root = root;
  37.   }
  38.  
  39.   private ArrayList<TreeNode> preOrderList = new ArrayList<>();
  40.   private ArrayList<TreeNode> inOrderList = new ArrayList<>();
  41.   private ArrayList<TreeNode> postOrderList = new ArrayList<>();
  42.  
  43.   private void preOrder(TreeNode root) {
  44.     if (root == null) {
  45.       return;
  46.     }
  47.     preOrderList.add(root);
  48.     preOrder(root.left);
  49.     preOrder(root.right);
  50.   }
  51.  
  52.   private void inOrder(TreeNode root) {
  53.     if (root == null) {
  54.       return;
  55.     }
  56.     inOrder(root.left);
  57.     inOrderList.add(root);
  58.     inOrder(root.right);
  59.   }
  60.  
  61.   private void postOrder(TreeNode root) {
  62.     if (root == null) {
  63.       return;
  64.     }
  65.     postOrder(root.left);
  66.     postOrder(root.right);
  67.     postOrderList.add(root);
  68.   }
  69.  
  70.   public ArrayList<TreeNode> getPreOrderList() {
  71.     preOrderList.clear();
  72.     preOrder(root);
  73.     return preOrderList;
  74.   }
  75.  
  76.   public ArrayList<TreeNode> getInOrderList() {
  77.     inOrderList.clear();
  78.     inOrder(root);
  79.     return inOrderList;
  80.   }
  81.  
  82.   public ArrayList<TreeNode> getPostOrderList() {
  83.     postOrderList.clear();
  84.     postOrder(root);
  85.     return postOrderList;
  86.   }
  87.  
  88.   private Scanner scanner = new Scanner(System.in);
  89.  
  90.   public TreeNode buildTree(TreeNode root) {
  91.     int key = scanner.nextInt();
  92.     if (key == -1) {
  93.       root = null;
  94.     } else {
  95.       root = new TreeNode(null, null, key);
  96.       root.left = buildTree(root.left);
  97.       root.right = buildTree(root.right);
  98.     }
  99.     // return back the copy
  100.     return root;
  101.   }
  102.  
  103.   public static void main(String[] args) {
  104. //    TreeNode<Integer> node1 = new TreeNode<>(null, null, 1);
  105. //    TreeNode<Integer> node2 = new TreeNode<>(null, null, 2);
  106. //    TreeNode<Integer> node3 = new TreeNode<>(null, null, 10);
  107. //    TreeNode<Integer> node4 = new TreeNode<>(node1, node2, 7);
  108. //    TreeNode<Integer> node5 = new TreeNode<>(node3, null, 6);
  109. //    TreeNode<Integer> root = new TreeNode<>(node4, node5, 5);
  110. //    BinaryTree<Integer> binaryTree = new BinaryTree<>(root);
  111.  
  112.     // input 5 7 1 -1 -1 2 -1 -1 6 10 -1 -1 -1
  113.     BinaryTree<Integer> binaryTree = new BinaryTree<>();
  114.     System.out.println(binaryTree.getPreOrderList().toString());
  115.     System.out.println(binaryTree.getInOrderList().toString());
  116.     System.out.println(binaryTree.getPostOrderList().toString());
  117.   }
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top