Class BinaryTree

java.lang.Object
edu.neu.khoury.cs6510.f24.wp161.BinaryTree

public class BinaryTree extends Object
The BinaryTree class represents a binary tree, where each node has a left and right child. The tree supports operations like checking if it's a binary search tree (BST), calculating its maximum depth, and finding the maximum value within the tree.
  • Constructor Details

    • BinaryTree

      public BinaryTree(Node root)
      Constructs a binary tree with the specified root node.
      Parameters:
      root - the root node of the binary tree
    • BinaryTree

      public BinaryTree()
      Constructs an empty binary tree (i.e., a tree with no nodes).
  • Method Details

    • getRoot

      public Node getRoot()
      Returns the root node of the binary tree.
      Returns:
      the root node of the tree, or null if the tree is empty
    • isBst

      public boolean isBst()
      Checks if the binary tree is a binary search tree (BST). A binary search tree is a binary tree in which, for every node n, all nodes in the left subtree have values less than n's value, and all nodes in the right subtree have values greater than n's value.
      Returns:
      true if the tree is a BST, false otherwise
    • getMaxDepth

      public Integer getMaxDepth()
      Returns the maximum depth of the binary tree. The maximum depth is the longest path from the root node to a leaf node.
      Returns:
      the maximum depth of the tree
    • getMaxValue

      public Integer getMaxValue()
      Returns the maximum value held in the binary tree.
      Returns:
      the maximum integer value in the tree
      Throws:
      IllegalStateException - if the tree is empty
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object