41-52
41-52
# Example usage
if __name__ == "__main__":
tree = BinaryTree()
print("Pre-order Traversal:")
tree.pre_order(tree.root) # Output: 50 30 20 40 70 60 80
print("\nIn-order Traversal:")
tree.in_order(tree.root) # Output: 20 30 40 50 60 70 80
print("\nPost-order Traversal:")
tree.post_order(tree.root) # Output: 20 40 30 60 80 70 50
# Shift elements of arr[0..i-1] that are greater than key to one position ahead
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
# Example array
arr = [5, 1, 4, 2, 8]
# Build a max-heap
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
# Example array
arr = [4, 10, 3, 5, 1]
# Example usage
arr = [1, 3, 5, 7, 9, 11]
target = 7
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found")
python
Copy code
# Calculate mid
mid = (low + high) // 2
# Example usage
arr = [1, 3, 5, 7, 9, 11]
target = 9
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found")
51. Create a vector from the positive elements of an
array.
def create_positive_vector(arr):
# Initialize an empty vector
positive_vector = []
return positive_vector
# Example array
array = [-3, 5, -1, 7, 0, -8, 12]