Update Function Lang
Update Function Lang
class TreeNode:
def __init__(self, value):
if not isinstance(value, int) or value < 0 or value >= 100:
raise ValueError("Node value must be a whole non-negative integer less
than 100.")
self.value = value
self.left = None
self.right = None
self.parent = None
class Tree:
def __init__(self, root_value):
self.root = TreeNode(root_value)
parent = node_to_update.parent
# Ensure the new value does not violate the BST property with respect to
the parent
if parent:
if parent.left == node_to_update and new_value >= parent.value:
raise ValueError("The new value should be less than the parent.")
elif parent.right == node_to_update and new_value <= parent.value:
raise ValueError("The new value should be greater than the
parent.")
# If both left and right children exist, check if new_value is within their
range
if node_to_update.left and node_to_update.right:
if not (node_to_update.left.value < new_value <
node_to_update.right.value):
raise ValueError("The new value should be greater than the left
child and less than the right child.")
# If only the left child exists, ensure new_value is greater than it
elif node_to_update.left:
if new_value <= node_to_update.left.value:
raise ValueError("The new value should be greater than the left
child.")
# If only the right child exists, ensure new_value is less than it
elif node_to_update.right:
if new_value >= node_to_update.right.value:
raise ValueError("The new value should be less than the right
child.")
# Update the node value if all checks pass
node_to_update.value = new_value
print("Node updated successfully.")
self.display_tree()
def display_tree(self):
if self.root is None:
print("Empty tree")
return
levels = self._get_levels(self.root)
output = []
def display_leaf_nodes(self):
print("\nLeaf Nodes:")
self._print_leaf_nodes(self.root)
def display_root(self):
print(f"\nRoot Node: {self.root.value}")
def display_parents(self):
print("\nParent Nodes:")
self._print_parents(self.root)
def display_children(self):
print("\nChild Nodes:")
self._print_children(self.root)
def tree_level(self):
print("\nTree Level (Height):", self._get_tree_level(self.root))
while queue:
level_size = len(queue)
level = []
for _ in range(level_size):
node, depth = queue.popleft()
spacing = (2 ** (max_depth - depth + 1)) - 1
level.append((node, spacing))
if node:
if node.left:
node.left.parent = node
if node.right:
node.right.parent = node
queue.append((node.left, depth + 1))
queue.append((node.right, depth + 1))
levels.append(level)
return levels
def main():
while True:
root_value = input("Enter the root value (must be a whole number < 100):
").strip()
if re.match(r"^(1|[1-9][0-9]?)$", root_value):
root_value = int(root_value)
break
else:
print("Invalid input. Please enter a whole number between 1 and 99.")
tree = Tree(root_value)
tree.display_tree()
while True:
print("\nAvailable commands:")
print("add <parent> <child> - Add a child node")
print("update <old> <new> - Update a node's value")
print("delete <node> - Delete a node")
print("display_leaf_nodes - Display all leaf nodes")
print("display_siblings <parent> - Display siblings of given parent node")
print("display_root - Display root node")
print("display_parents - Display all parent nodes")
print("display_children - Display all child nodes")
print("tree_level - Display the level (height) of the tree")
print("display_tree - Display the current tree structure")
print("display_subtree <node> - Display the subtree rooted at a given
node")
print("exit - Exit the program")
command = command_parts[0].lower()
try:
if command == "add" and len(command_parts) == 3:
parent_value = int(command_parts[1])
child_value = int(command_parts[2])
tree.add(parent_value, child_value)
elif command == "update" and len(command_parts) == 3:
old_value = int(command_parts[1])
new_value = int(command_parts[2])
tree.update(old_value, new_value)
elif command == "delete" and len(command_parts) == 2:
node_value = int(command_parts[1])
tree.delete(node_value)
elif command == "leafNodes":
tree.display_leaf_nodes()
elif command == "siblings" and len(command_parts) == 2:
parent_value = int(command_parts[1])
tree.display_siblings(parent_value)
elif command == "root":
tree.display_root()
elif command == "parents":
tree.display_parents()
elif command == "children":
tree.display_children()
elif command == "level":
tree.tree_level()
elif command == "display tree":
tree.display_tree()
elif command == "subtree" and len(command_parts) == 2:
node_value = int(command_parts[1])
tree.display_subtree(node_value)
elif command == "exit":
print("Exiting...")
break
else:
print("Invalid command or wrong number of arguments.")
except ValueError as e:
print("Error:", e)
if __name__ == "__main__":
main()