Class Tree<T>

java.lang.Object
util.collection.Tree<T>
Type Parameters:
T - the type of value stored in each tree node

public class Tree<T> extends Object
Tree is a generic class representing a tree data structure with nodes and children.

It supports both general and binary trees, and provides methods for common tree operations such as checking symmetry, fullness, depth, leaf count, degree, and diameter.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    TreeNode represents a node in the tree, holding a value and a list of children.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty tree (root is null).
    Tree(T rootValue)
    Constructs a tree with the specified root value.
    Tree(T rootValue, Map<T,List<T>> childrenMap)
    Constructs a tree with the specified root value and a map of children for each node.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of leaf nodes in the tree.
    int
    Returns the diameter of the tree (the length of the longest path between any two nodes).
    int
    Returns the depth (height) of the tree.
    Returns the root node of the tree.
    boolean
    Checks if the tree is a binary tree (each node has at most two children).
    boolean
    Checks if the tree is empty (root value is null).
    boolean
    Checks if the tree is full (every node has 0 or 2 children).
    boolean
    Checks if the tree is symmetric (a mirror of itself).
    int
    Returns the maximum degree (number of children) of any node in the tree.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Tree

      public Tree()
      Constructs an empty tree (root is null).
    • Tree

      public Tree(T rootValue)
      Constructs a tree with the specified root value.
      Parameters:
      rootValue - the value for the root node
    • Tree

      public Tree(T rootValue, Map<T,List<T>> childrenMap)
      Constructs a tree with the specified root value and a map of children for each node.
      Parameters:
      rootValue - the value for the root node
      childrenMap - a map where each key is a node value and the value is a list of its children's values
  • Method Details

    • getRoot

      public Tree.TreeNode<T> getRoot()
      Returns the root node of the tree.
      Returns:
      the root node
    • isEmpty

      public boolean isEmpty()
      Checks if the tree is empty (root value is null).
      Returns:
      true if the tree is empty, false otherwise
    • isBinaryTree

      public boolean isBinaryTree()
      Checks if the tree is a binary tree (each node has at most two children).
      Returns:
      true if the tree is binary, false otherwise
    • isSymmetric

      public boolean isSymmetric()
      Checks if the tree is symmetric (a mirror of itself).
      Returns:
      true if the tree is symmetric, false otherwise
    • isFull

      public boolean isFull()
      Checks if the tree is full (every node has 0 or 2 children).
      Returns:
      true if the tree is full, false otherwise
    • getDepth

      public int getDepth()
      Returns the depth (height) of the tree.
      Returns:
      the depth of the tree
    • countLeaves

      public int countLeaves()
      Returns the number of leaf nodes in the tree.
      Returns:
      the number of leaves
    • maxDegree

      public int maxDegree()
      Returns the maximum degree (number of children) of any node in the tree.
      Returns:
      the maximum degree
    • diameter

      public int diameter()
      Returns the diameter of the tree (the length of the longest path between any two nodes).
      Returns:
      the diameter of the tree