Skip to main content

TreeNode

A binary tree node implementation.

Constructor

TreeNode(value, left=None, right=None)
value
any
required
The value of the node.
left
TreeNode
default:"None"
Reference to the left child node.
right
TreeNode
default:"None"
Reference to the right child node.

Methods

copy()

Return a deep copy of the node and its subtree.
node.copy()
return
TreeNode
A new TreeNode with the same structure and values as the original.
Example:
from ucx_dsa import TreeNode

original = TreeNode(5)
original.left = TreeNode(3)
original.right = TreeNode(7)

copy = original.copy()
# copy is a separate tree with the same structure
Time Complexity: O(n) where n is the number of nodes in the subtree
Print the contents of the node and its subtree in a visual tree format.
node.print(level=0)
level
int
default:"0"
Starting indentation level of the node (used internally for recursion).
Example:
from ucx_dsa import TreeNode

root = TreeNode(5)
root.left = TreeNode(3)
root.right = TreeNode(7)

root.print()
# Output:
#    7
# 5
#    3
Time Complexity: O(n) where n is the number of nodes in the subtree

Special Methods

__lt__(other)

Compare the value of two nodes using the less-than operator.
node1 < node2
other
TreeNode
required
The other node to compare against.
return
bool
True if this node’s value is less than the other node’s value.

__repr__()

Return the string representation of a node.
str(node)
repr(node)
return
str
String representation of the node’s value, or “none” if value is None.
Example:
from ucx_dsa import TreeNode

node = TreeNode(42)
print(node)  # Output: 42

Tree

A binary search tree (BST) implementation. Can be treated as a plain binary tree if BST operations (insert, search, delete) are not used and nodes are set manually.

Constructor

Tree(root=None)
root
TreeNode
default:"None"
Root node of the tree.
Example:
from ucx_dsa import Tree, TreeNode

# Create an empty tree
tree = Tree()

# Or create a tree with a root node
tree = Tree(TreeNode(10))

Methods

search(value)

Search for a value in the binary search tree.
tree.search(value)
value
any
required
Value to search for.
return
TreeNode
The node with the matching value.
Raises:
  • ValueError: If value is not found in the tree
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(5)
tree.insert(3)
tree.insert(7)

node = tree.search(3)
print(node.value)  # Output: 3
Time Complexity: O(log n) average case, O(n) worst case

insert(value)

Insert a value in the binary search tree.
tree.insert(value)
value
any
required
The value to insert.
return
TreeNode
The root of the tree after insertion.
Raises:
  • ValueError: If value already exists in the tree
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(10)
tree.insert(5)
tree.insert(15)
tree.insert(3)
tree.insert(7)
Time Complexity: O(log n) average case, O(n) worst case

insert_iterative(value)

Insert a value in the binary search tree using an iterative implementation.
tree.insert_iterative(value)
value
any
required
The value to insert.
Raises:
  • ValueError: If value already exists in the tree
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert_iterative(20)
tree.insert_iterative(10)
tree.insert_iterative(30)
Time Complexity: O(log n) average case, O(n) worst case

delete(value)

Delete a value from the binary search tree.
tree.delete(value)
value
any
required
The value to delete.
return
TreeNode
The root of the tree after deletion.
Raises:
  • ValueError: If value is not found in the tree
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(10)
tree.insert(5)
tree.insert(15)

tree.delete(5)
# 5 is now removed from the tree
Time Complexity: O(log n) average case, O(n) worst case

successor_node(node=None)

Return the successor node (the minimum value in a binary search tree’s right subtree).
tree.successor_node(node=None)
node
TreeNode
default:"None"
The starting node. If None, uses the tree’s root.
return
TreeNode
Node with the lowest value in the subtree, or None if not found.
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(10)
tree.insert(5)
tree.insert(15)
tree.insert(12)
tree.insert(20)

successor = tree.successor_node()
print(successor.value)  # Output: 5 (leftmost node)
Time Complexity: O(h) where h is the height of the tree

predecessor_node(node=None)

Return the predecessor node (the maximum value in a binary search tree’s left subtree).
tree.predecessor_node(node=None)
node
TreeNode
default:"None"
The starting node. If None, uses the tree’s root.
return
TreeNode
Node with the highest value in the subtree, or None if not found.
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(10)
tree.insert(5)
tree.insert(15)
tree.insert(12)
tree.insert(20)

predecessor = tree.predecessor_node()
print(predecessor.value)  # Output: 20 (rightmost node)
Time Complexity: O(h) where h is the height of the tree

print()

Print the values in the BST in a visual tree format.
tree.print()
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(10)
tree.insert(5)
tree.insert(15)

tree.print()
# Output:
#    15
# 10
#    5

Special Methods

__len__()

Return the number of nodes in the BST.
len(tree)
return
int
The number of nodes in the BST.
Example:
from ucx_dsa import Tree

tree = Tree()
tree.insert(10)
tree.insert(5)
tree.insert(15)

print(len(tree))  # Output: 3
Time Complexity: O(n)

__eq__(other)

Compare two Tree objects for value-based equality (structure and values).
tree1 == tree2
other
Tree
required
The other tree to compare against.
return
bool
True if both are Tree instances and their structures and values are equal.
Example:
from ucx_dsa import Tree

tree1 = Tree()
tree1.insert(10)
tree1.insert(5)

tree2 = Tree()
tree2.insert(10)
tree2.insert(5)

print(tree1 == tree2)  # Output: True
Time Complexity: O(n)

Build docs developers (and LLMs) love