Class Tree<T>
java.lang.Object
util.collection.Tree<T>
- Type Parameters:
T
- the type of value stored in each tree node
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 ClassesModifier and TypeClassDescriptionstatic class
TreeNode represents a node in the tree, holding a value and a list of children. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of leaf nodes in the tree.int
diameter()
Returns the diameter of the tree (the length of the longest path between any two nodes).int
getDepth()
Returns the depth (height) of the tree.getRoot()
Returns the root node of the tree.boolean
Checks if the tree is a binary tree (each node has at most two children).boolean
isEmpty()
Checks if the tree is empty (root value is null).boolean
isFull()
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.
-
Constructor Details
-
Tree
public Tree()Constructs an empty tree (root is null). -
Tree
Constructs a tree with the specified root value.- Parameters:
rootValue
- the value for the root node
-
Tree
-
-
Method Details
-
getRoot
-
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
-