Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shadowdevnotreal/94ed8ea0684d0f6cdabb46d87f483c99 to your computer and use it in GitHub Desktop.

Select an option

Save shadowdevnotreal/94ed8ea0684d0f6cdabb46d87f483c99 to your computer and use it in GitHub Desktop.
Random Binary Inversion logic
To perform prediction modeling or apply algorithms to analyze this random binary tree, we can take several approaches depending on the context. Since this is a binary tree with random integer values, we can’t apply traditional machine learning algorithms directly as the data lacks labeled features or specific targets. However, we can still apply tree manipulation and analysis methods such as inversion, and we can also test the double inversion process.
Plan:
1. Tree Inversion: We will apply the tree inversion function to this tree.
2. Double Inversion: After the first inversion, we will invert it again to check if the tree is restored to its original form.
3. Comparison: We will compare the original tree and the double-inverted tree to validate if the inversion logic holds.
Running the Process:
I'll invert the tree, perform a double inversion, and display the results.
Let's proceed by inverting this random tree and checking the results.
Tree Inversion Code:
# Recursive function to invert the tree by swapping children
def invert_tree(bit, node):
"""Recursive function to invert the tree by reversing the key bits and swapping children."""
if node is None:
return None # Base case: no node to invert
if isinstance(node, TreeNode):
if bit == 'I':
# Swap left and right if bit is I
return TreeNode(node.key, invert_tree(bit, node.right), invert_tree(bit, node.left))
else:
# Do not swap if bit is O
return TreeNode(node.key, invert_tree(bit, node.left), invert_tree(bit, node.right))
# Function to compare two trees
def are_trees_equal(t1, t2):
if t1 is None and t2 is None:
return True
if t1 is None or t2 is None:
return False
return (t1.key == t2.key) and are_trees_equal(t1.left, t2.left) and are_trees_equal(t1.right, t2.right)
# Let's perform inversion and double inversion
def test_inversion_on_random_tree(tree):
# First inversion with bit 'I'
inverted_tree = invert_tree('I', tree)
# Second inversion to check if the tree is restored
double_inverted_tree = invert_tree('I', inverted_tree)
# Compare the original and the double-inverted tree
result = are_trees_equal(tree, double_inverted_tree)
# Display results
print("Original Tree:")
print_tree(tree)
print("\nInverted Tree:")
print_tree(inverted_tree)
print("\nDouble Inverted Tree (Should match Original):")
print_tree(double_inverted_tree)
if result:
print("\nResult: Pass (Tree restored correctly after double inversion)")
else:
print("\nResult: Fail (Tree not restored correctly)")
# Run the test on the random depth-3 tree
test_inversion_on_random_tree(random_tree)
The results of the tree inversion and double inversion test are as follows:
1. Original Tree:
Root: 26
L--- 63
L--- 3
R--- 46
R--- 67
L--- 52
R--- 96
2. Inverted Tree (after applying bit-reversal with bit 'I'):
Root: 26
L--- 67
L--- 96
R--- 52
R--- 63
L--- 46
R--- 3
3. Double Inverted Tree (should match the original tree):
Root: 26
L--- 63
L--- 3
R--- 46
R--- 67
L--- 52
R--- 96
Conclusion:
The result is a Pass, meaning the tree was restored correctly after the double inversion, confirming the correctness of the inversion logic applied to the randomly generated tree.
If you would like further analysis or a different application of algorithms, feel free to let me know!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment