Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.55 KB | None | 0 0
  1. package binary_tree;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6.  * Generic binary tree, storing data of a parametric type in each node
  7.  *
  8.  * @author Chris Bailey-Kellogg, Dartmouth CS 10, Fall 2012
  9.  * @author Scot Drysdale, Winter 2012. Numerous modifications.
  10.  */
  11.  
  12. public class BinaryTree<E> {
  13.     private BinaryTree<E> left, right; // children; can be null
  14.     private E data;
  15.  
  16.     /**
  17.      * Constructs leaf node -- left and right are null
  18.      */
  19.     public BinaryTree(E data) {
  20.         this.data = data;
  21.         this.left = null;
  22.         this.right = null;
  23.     }
  24.  
  25.     /**
  26.      * Constructs inner node
  27.      */
  28.     public BinaryTree(E data, BinaryTree<E> left, BinaryTree<E> right) {
  29.         this.data = data;
  30.         this.left = left;
  31.         this.right = right;
  32.     }
  33.  
  34.     /**
  35.      * Is it an inner node?
  36.      */
  37.     public boolean isInner() {
  38.         return left != null || right != null;
  39.     }
  40.  
  41.     /**
  42.      * Is it a leaf node?
  43.      */
  44.     public boolean isLeaf() {
  45.         return left == null && right == null;
  46.     }
  47.  
  48.     /**
  49.      * Does it have a left child?
  50.      */
  51.     public boolean hasLeft() {
  52.         return left != null;
  53.     }
  54.  
  55.     /**
  56.      * Does it have a right child?
  57.      */
  58.     public boolean hasRight() {
  59.         return right != null;
  60.     }
  61.  
  62.     /**
  63.      * @return its left child
  64.      */
  65.     public BinaryTree<E> getLeft() {
  66.         return left;
  67.     }
  68.  
  69.     /**
  70.      * @return its right child
  71.      */
  72.     public BinaryTree<E> getRight() {
  73.         return right;
  74.     }
  75.  
  76.     /**
  77.      * Sets its left child to be newLeft
  78.      *
  79.      * @param newLeft
  80.      *            - the new left child
  81.      */
  82.     public void setLeft(BinaryTree<E> newLeft) {
  83.         left = newLeft;
  84.     }
  85.  
  86.     /**
  87.      * Sets its right child to be newRight
  88.      *
  89.      * @param newRight
  90.      *            - the new right child
  91.      */
  92.     public void setRight(BinaryTree<E> newRight) {
  93.         right = newRight;
  94.     }
  95.  
  96.     /**
  97.      * @return its data value
  98.      */
  99.     public E getValue() {
  100.         return data;
  101.     }
  102.  
  103.     /**
  104.      * Sets its data value
  105.      *
  106.      * @param newValue
  107.      *            - the new data value
  108.      */
  109.     public void setValue(E newValue) {
  110.         data = newValue;
  111.     }
  112.  
  113.     /**
  114.      * Number of nodes (inner and leaf) in tree
  115.      */
  116.     public int size() {
  117.         int num = 1;
  118.         if (hasLeft())
  119.             num += left.size();
  120.         if (hasRight())
  121.             num += right.size();
  122.         return num;
  123.     }
  124.  
  125.     /**
  126.      * Longest path to a leaf node from here
  127.      */
  128.     public int height() {
  129.         if (isLeaf())
  130.             return 0;
  131.         else {
  132.             int h = 0;
  133.             if (hasLeft())
  134.                 h = Math.max(h, left.height());
  135.             if (hasRight())
  136.                 h = Math.max(h, right.height());
  137.             return h + 1; // inner: one higher than highest child
  138.         }
  139.     }
  140.  
  141.     /**
  142.      * Same structure and data?
  143.      */
  144.     public boolean equals(Object other) {
  145.         if (!(other instanceof BinaryTree<?>))
  146.             return false;
  147.         BinaryTree<E> t2 = (BinaryTree<E>) other;
  148.         if (hasLeft() != t2.hasLeft() || hasRight() != t2.hasRight())
  149.             return false;
  150.         if (!data.equals(t2.data))
  151.             return false;
  152.         if (hasLeft() && !left.equals(t2.left))
  153.             return false;
  154.         if (hasRight() && !right.equals(t2.right))
  155.             return false;
  156.         return true;
  157.     }
  158.  
  159.     /**
  160.      * Leaves, in order from left to right
  161.      */
  162.     public ArrayList<E> fringe() {
  163.         ArrayList<E> f = new ArrayList<E>();
  164.         addToFringe(f);
  165.         return f;
  166.     }
  167.  
  168.     /**
  169.      * Helper for fringe, adding fringe data to the list
  170.      */
  171.     private void addToFringe(ArrayList<E> fringe) {
  172.         if (isLeaf()) {
  173.             fringe.add(data);
  174.         } else {
  175.             if (hasLeft())
  176.                 left.addToFringe(fringe);
  177.             if (hasRight())
  178.                 right.addToFringe(fringe);
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Returns a string representation of the tree
  184.      */
  185.     public String toString() {
  186.         return toStringHelper("");
  187.     }
  188.  
  189.     /**
  190.      * Recursively constructs a String representation of the tree from this
  191.      * node, starting with the given indentation and indenting further going
  192.      * down the tree
  193.      */
  194.     private String toStringHelper(String indent) {
  195.         String ret = "";
  196.         if (hasRight())
  197.             ret += right.toStringHelper(indent + "  ");
  198.         ret += indent + data + "\n";
  199.         if (hasLeft())
  200.             ret += left.toStringHelper(indent + "  ");
  201.         return ret;
  202.     }
  203.  
  204.     /**
  205.      * Creates a list storing the the data values in the subtree of a node,
  206.      * ordered according to the preorder traversal of the subtree.
  207.      *
  208.      * @param dataList
  209.      *            - the list to be returned.
  210.      */
  211.     public void preorder(List<E> dataList) {
  212.         dataList.add(data);
  213.         if (this.hasLeft())
  214.             left.preorder(dataList); // recurse on left child
  215.         if (this.hasRight())
  216.             right.preorder(dataList); // recurse on right child
  217.     }
  218.  
  219.     /**
  220.      * Creates a list storing the the data values in the subtree of a node,
  221.      * ordered according to the inorder traversal of the subtree.
  222.      *
  223.      * @param dataList
  224.      *            - the list to be returned.
  225.      */
  226.     public void inorder(List<E> dataList) {
  227.         if (this.hasLeft())
  228.             left.inorder(dataList); // recurse on left child
  229.         dataList.add(data);
  230.         if (this.hasRight())
  231.             right.inorder(dataList); // recurse on right child
  232.     }
  233.  
  234.     /**
  235.      * Creates a list storing the the data values in the subtree of a node,
  236.      * ordered according to the preorder traversal of the subtree.
  237.      *
  238.      * @param dataList
  239.      *            - the list to be returned.
  240.      */
  241.     public void postorder(List<E> dataList) {
  242.         if (this.hasLeft())
  243.             left.postorder(dataList); // recurse on left child
  244.         if (this.hasRight())
  245.             right.postorder(dataList); // recurse on right child
  246.         dataList.add(data);
  247.     }
  248.  
  249.     /**
  250.      * Reconstructs tree from a preorder and inorder traversal, assuming that no
  251.      * value is repeated. Precondition: the traversals are valid. (Otherwise
  252.      * need lots of error checks.)
  253.      */
  254.     public static <E> BinaryTree<E> reconstructTree(List<E> preorder,
  255.             List<E> inorder) {
  256.  
  257.         BinaryTree<E> returnTree; // Tree to return
  258.  
  259.         if (preorder.size() > 0) { // Is tree non-empty?
  260.  
  261.             // Create iterators to walk through lists and new lists for
  262.             // recursive calls
  263.             Iterator<E> iterPre = preorder.iterator();
  264.             Iterator<E> iterIn = inorder.iterator();
  265.             List<E> leftPre = new LinkedList<E>();
  266.             List<E> rightPre = new LinkedList<E>();
  267.             List<E> leftIn = new LinkedList<E>();
  268.             List<E> rightIn = new LinkedList<E>();
  269.  
  270.             E rootValue = iterPre.next(); // First thing in preorder is root of
  271.                                             // tree
  272.             E inValue = iterIn.next();
  273.  
  274.             // Find things in left subtree and copy into leftPre and leftIn
  275.             while (!rootValue.equals(inValue)) {
  276.                 leftPre.add(iterPre.next());
  277.                 leftIn.add(inValue);
  278.                 inValue = iterIn.next();
  279.             }
  280.  
  281.             BinaryTree<E> leftSubtree = reconstructTree(leftPre, leftIn);
  282.  
  283.             // Copy things in right subtree into rightPre and rightIn
  284.             while (iterPre.hasNext()) {
  285.                 rightPre.add(iterPre.next());
  286.                 rightIn.add(iterIn.next());
  287.             }
  288.  
  289.             BinaryTree<E> rightSubtree = reconstructTree(rightPre, rightIn);
  290.  
  291.             returnTree = new BinaryTree<E>(rootValue, leftSubtree, rightSubtree);
  292.         } else
  293.             returnTree = null; // Empty tree in this case
  294.  
  295.         return returnTree;
  296.     }
  297.  
  298.     /**
  299.      * Testing program
  300.      */
  301.     public static void main(String[] args) {
  302.  
  303.         // Create a tree by building it up
  304.  
  305.         BinaryTree<String> leftChild = new BinaryTree<String>("bear",
  306.                 new BinaryTree<String>("ant"), new BinaryTree<String>("cat"));
  307.         BinaryTree<String> tree = new BinaryTree<String>("cow", leftChild,
  308.                 new BinaryTree<String>("dog"));
  309.  
  310.         // Some tests of methods
  311.         System.out.println("tree: \n" + tree);
  312.         System.out.println("Size of tree = " + tree.size());
  313.         System.out.println("Height of tree = " + tree.height());
  314.         System.out.println("Fringe of tree =" + tree.fringe());
  315.  
  316.         // Build a tree from traversals
  317.         List<String> preList = new LinkedList<String>();
  318.         tree.preorder(preList);
  319.         System.out.println("Preorder traversal of the tree =" + preList);
  320.  
  321.         List<String> inList = new LinkedList<String>();
  322.         tree.inorder(inList);
  323.         System.out.println("Inorder traversal of the tree =" + inList);
  324.  
  325.         List<String> postList = new LinkedList<String>();
  326.         tree.postorder(postList);
  327.         System.out.println("Postorder traversal of the tree =" + postList);
  328.  
  329.         BinaryTree<String> tree1 = reconstructTree(preList, inList);
  330.         System.out.println("tree1: \n" + tree1);
  331.         System.out.println("tree and tree1 are equal? " + tree.equals(tree1));
  332.  
  333.         tree1.setValue("cougar");
  334.         System.out.println("tree: \n" + tree);
  335.         System.out.println("modified tree1: \n" + tree1);
  336.         System.out.println("tree and tree1 are equal? " + tree.equals(tree1));
  337.  
  338.         BinaryTree<String> tree2 = reconstructTree(preList, inList);
  339.         tree2.getLeft().setLeft(null);
  340.         System.out.println("tree2: \n" + tree2);
  341.         System.out.println("Size of tree2 = " + tree2.size());
  342.         System.out.println("Height of tree2 = " + tree2.height());
  343.         System.out.println("Fringe of tree2 =" + tree2.fringe());
  344.         System.out.println("tree and tree2 are equal? " + tree.equals(tree2));
  345.     }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement