TreeNode
A binary tree node implementation.
Constructor
TreeNode(value, left=None, right=None)
Reference to the left child node.
Reference to the right child node.
Methods
copy()
Return a deep copy of the node and its subtree.
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(level=0)
Print the contents of the node and its subtree in a visual tree format.
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.
The other node to compare against.
True if this node’s value is less than the other node’s value.
__repr__()
Return the string representation of a node.
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
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.
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.
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)
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.
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)
The starting node. If None, uses the tree’s root.
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)
The starting node. If None, uses the tree’s root.
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.
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.
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).
The other tree to compare against.
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)