Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class NumericBinaryTree {
- private List<Number> holdTheValues = new ArrayList<Number>();
- private Number rootValue;
- private NumericBinaryTree leftChild;
- private NumericBinaryTree rightChild;
- /**
- * Constructor that will create an empty numeric
- * binary tree.
- * @param
- */
- public NumericBinaryTree() {
- this.rootValue = null;
- this.leftChild = null;
- this.rightChild = null;
- }
- /**
- * Constructor that will create a numeric binary tree with
- * a value.
- * @param takes a value of type number and sets it as the
- * root of the tree
- */
- public NumericBinaryTree(Number rootValue) {
- if (rootValue == null) {
- throw new IllegalArgumentException("");
- }
- this.rootValue = rootValue;
- System.out.println("this is the root value" + rootValue);
- }
- /**
- * Constructor that will create a binary tree with a value and
- * its left and right child.
- *
- * @param takes a value of type number and sets it as the
- * root of the tree. Takes a tree for its left child and right
- * child.
- */
- public NumericBinaryTree(Number rootValue, NumericBinaryTree leftChild,
- NumericBinaryTree rightChild) {
- if (rootValue == null) {
- throw new IllegalArgumentException("");
- }
- this.rootValue = rootValue;
- this.leftChild = leftChild;
- this.rightChild = rightChild;
- System.out.println("in a tree with children whose root value is" + rootValue);
- }
- /**
- * Check to see if the tree is empty.
- *
- * @param
- */
- public boolean isEmpty() {
- return rootValue == null;
- }
- /**
- * Count how many subtrees are in the tree.
- *
- * @param
- */
- public int numberOfNodes() {
- int count = 0;
- if(isEmpty()) {
- return count;
- }
- if(rootValue!= null) {
- count ++;
- }
- if (getLeftChild () != null) {
- count += this.getLeftChild().numberOfNodes();
- }
- if (getRightChild() != null) {
- count += this.getRightChild().numberOfNodes();
- }
- return count;
- }
- /**
- * Check and see if the node is a leaf
- *
- * @return
- */
- public boolean isLeaf() {
- /*if(root.isEmpty()) {
- throw new NullPointerException("");
- }*/
- return leftChild == null && rightChild == null;
- }
- /**
- * Method to get the root value of the current tree.
- *
- * @return rootValue
- */
- public Number getValue() {
- this.rootValue = rootValue;
- if(rootValue == null) {
- throw new NullPointerException("");
- }
- return rootValue;
- }
- /**
- * Method to get the left child of the current tree.
- *
- * @return leftChild
- */
- public NumericBinaryTree getLeftChild(){
- if (leftChild.isEmpty()) {
- throw new NullPointerException("");
- }
- return leftChild;
- }
- /**
- * Method to get the left child of the current tree.
- *
- * @return righChild
- */
- public NumericBinaryTree getRightChild(){
- // if (rightChild.isEmpty()) {
- // throw new NullPointerException("");
- // }
- return rightChild;
- }
- /**
- * Method to set the left child of the current tree.
- *
- * @param child
- */
- public void setLeftChild(NumericBinaryTree child) {
- leftChild = child;
- if ( rootValue==null) {
- throw new NullPointerException("");
- }
- }
- /**
- * Method to set the left child of the current tree.
- *
- * @param child
- */
- public void setRightChild(NumericBinaryTree child) {
- rightChild = child;
- if ( rootValue == null) {
- throw new NullPointerException("");
- }
- }
- /**
- * Find how tall this tree is.
- *
- * @return height of tree;
- */
- public int numberOfLeaves() {
- return 0;
- }
- public final int height(NumericBinaryTree t) {
- if (t == null) {
- return -1;
- }
- return Math.max(height(t.leftChild), height(t.rightChild)) +1;
- }
- /** Determine if two trees are equal to each other
- *
- * @param Object
- * @return boolean
- * @Override
- */
- public boolean equals(Object obj) {
- if(obj == null || rootValue == null) {
- return false;
- }
- NumericBinaryTree compareTree = (NumericBinaryTree) obj;
- return rootValue.equals(compareTree.getValue())
- && leftChild.getValue().equals(compareTree.getValue());
- }
- /** Method to return a hash code value for this tree
- *
- * @return hashValue
- * @Override hashCode
- */
- public int hashCode(){
- int hashValue = rootValue.hashCode();
- if (leftChild!= null) {
- hashValue += 2 * leftChild.hashCode();
- }
- if (rightChild!= null) {
- hashValue += 8 * rightChild.hashCode();
- }
- return hashValue;
- }
- /** Method to that gets a list of all values using preorder traversal.
- *
- * @return List
- */
- /* public ArrayList<Number> preOrderValues() {
- this.holdTheValues.add();
- preOrder();
- holdTheValues.add(rootValue);
- return holdTheValues;
- }*/
- public List<Number> preOrder() {
- holdTheValues.add(rootValue);
- if(rootValue== null) {
- return holdTheValues;
- }
- if(leftChild !=null) {
- leftChild.preOrder();
- }
- if(rightChild !=null) {
- rightChild.preOrder();
- }
- System.out.println("here are the values" + holdTheValues);
- return holdTheValues;
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.ArrayList;
- import java.util.List;
- public class NumericBinaryTreeTest {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Number a = 0;
- Number b = 1;
- Number c = 2;
- Number d = 3;
- List values = new ArrayList<Number>();
- NumericBinaryTree t1 = new NumericBinaryTree(a);
- NumericBinaryTree t2 = new NumericBinaryTree(c);
- NumericBinaryTree t3 = new NumericBinaryTree(c);
- NumericBinaryTree t4 = new NumericBinaryTree(c);
- NumericBinaryTree t5 = new NumericBinaryTree(c);
- NumericBinaryTree t6 = new NumericBinaryTree(b);
- NumericBinaryTree t = new NumericBinaryTree(a,t1,t2);
- NumericBinaryTree tree = new NumericBinaryTree(a,t1,t2);
- t2.setLeftChild(t4);
- t2.setLeftChild(t4);
- t1.setLeftChild(t3);
- t4.setLeftChild(t6);
- //System.out.println(t.equals(t1));
- System.out.println("height " + t.height(t));
- System.out.println(t.hashCode());
- System.out.println(t6.hashCode());
- values = tree.preOrder();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement