About
heap.display() # Output: Min Heap: [1, 5, 3, 10, 8]
min_value = heap.extract_min() print("Extracted Min Value:", min_value) # Output: Extracted Min Value: 1
heap.display() # Output: Min Heap: [3, 5, 8, 10]
peek_value = heap.peek_min() print("Peeked Min Value:", peek_value) # Output: Peeked Min Value: 3
print("Heap Size:", heap.size()) # Output: Heap Size: 4
17.Program to Print the Nodes at Odd Levels of a Tree class Node: def init(self, data): self.right=None self.left=None self.data=data
def oddNode(root,level): if root is None: return 0 else: if(level%2!=0): print(root.data) oddNode(root.left,level+1) oddNode(root.right,level+1)
root=Node(5) root.left=Node(3) root.right=Node(9) root.left.left=Node(4) root.left.right=Node(1) oddNode(root,1)
- Program to Create Expression Tree from Infix Expression
class Node: def _init_(self, value): self.value = value self.left = None self.right = None
def is_operator(char): return char in ['+', '-', '*', '/']
def precedence(char): if char == '+' or char == '-': return 1 elif char == '*' or char == '/': return 2 else: return 0
def infix_to_postfix(expression): postfix = [] stack = []
for char in expression:
if char.isalnum():
postfix.append(char)
elif char == '(':
stack.append(char)
elif char == ')':
while stack and stack[-1] != '(':
postfix.append(stack.pop())
stack.pop() # Discard '('
elif is_operator(char):
while stack and stack[-1] != '(' and precedence(char) <= precedence(stack[-1]):
postfix.append(stack.pop())
stack.append(char)
while stack:
postfix.append(stack.pop())
return ''.join(postfix)
def build_expression_tree(postfix): stack = []
for char in postfix:
if char.isalnum():
node = Node(char)
stack.append(node)
elif is_operator(char):
right = stack.pop()
left = stack.pop()
node = Node(char)
node.left = left
node.right = right
stack.append(node)
return stack.pop()
def inorder_traversal(root): if root is None: return
inorder_traversal(root.left)
print(root.value, end=' ')
inorder_traversal(root.right)
Example usage
expression = "a+bc-(d/e+fg)"
postfix = infix_to_postfix(expression) print("Postfix expression:", postfix)
root = build_expression_tree(postfix)
print("Inorder traversal of the expression tree:") inorder_traversal(root)
19.Program to find Height of a binary tree class Node: def init(self, data): self.right=None self.left=None self.data=data def height(root): if root is None: return 0 else: left=height(root.left) right=height(root.right) return 1+max(left, right)
root=Node(5) root.left=Node(3) root.right=Node(9) root.left.left=Node(4) root.left.right=Node(1) val=height(root) print("Height of tree =",val) 20.Program to calculate & display Product of all leaf nodes of binary tree class Node: def init(self, data): self.right=None self.left=None self.data=data def product(root): if root is None: return 0 else: if root.left is None and root.right is None: L.append(root.data) product(root.left) product(root.right) root=Node(5) root.left=Node(9) root.right=Node(4) root.left.left=Node(3) root.left.right=Node(5) L=[] p=1 product(root) for i in L: p=p*i print(p)
- Program to Find kth maximum and kth minimum value in a binary search tree
class Node: def _init_(self, value): self.value = value self.left = None self.right = None
def inorder_traversal(root, values): if root is None: return
inorder_traversal(root.left, values)
values.append(root.value)
inorder_traversal(root.right, values)
def find_kth_maximum(values, k): if k > len(values) or k < 1: return None
return values[-k]
def find_kth_minimum(values, k): if k > len(values) or k < 1: return None
return values[k - 1]
Example usage
BST
root = Node(4) root.left = Node(2) root.right = Node(6) root.left.left = Node(1) root.left.right = Node(3) root.right.left = Node(5) root.right.right = Node(7)
Find kth maximum and kth minimum
values = [] inorder_traversal(root, values)
k = 3 kth_maximum = find_kth_maximum(values, k) kth_minimum = find_kth_minimum(values, k)
print(f"{k}th maximum value: {kth_maximum}") print(f"{k}th minimum value: {kth_minimum}")
- Program to Convert BST to min-heap
class Node: def _init_(self, value): self.value = value self.left = None self.right = None
def inorder_traversal(root, values): if root is None: return
inorder_traversal(root.left, values)
values.append(root.value)
inorder_traversal(root.right, values)
def build_min_heap(values, root): if root is None: return None
root.value = values.pop(0)
root.left = build_min_heap(values, root.left)
root.right = build_min_heap(values, root.right)
return root
def convert_bst_to_min_heap(root): values = [] inorder_traversal(root, values)
return build_min_heap(values, root)
def print_inorder(root): if root is None: return
print_inorder(root.left)
print(root.value, end=' ')
print_inorder(root.right)
Example usage
BST
root = Node(4) root.left = Node(2) root.right = Node(6) root.left.left = Node(1) root.left.right = Node(3) root.right.left = Node(5) root.right.right = Node(7)
Convert BST to min-heap
converted_root = convert_bst_to_min_heap(root)
Print the inorder traversal of the converted min-heap
print("Inorder traversal of the converted min-heap:") print_inorder(converted_root)
- Program to combine two binary search trees into a single tree
class Node: def _init_(self, value): self.value = value self.left = None self.right = None
def inorder_traversal(root, values): if root is None: return
inorder_traversal(root.left, values)
values.append(root.value)
inorder_traversal(root.right, values)
def create_bst_from_sorted(values, start, end): if start > end: return None
mid = (start + end) // 2
root = Node(values[mid])
root.left = create_bst_from_sorted(values, start, mid - 1)
root.right = create_bst_from_sorted(values, mid + 1, end)
return root
def combine_bsts(root1, root2): values1 = [] values2 = []
inorder_traversal(root1, values1)
inorder_traversal(root2, values2)
combined_values = sorted(values1 + values2)
return create_bst_from_sorted(combined_values, 0, len(combined_values) - 1)
def print_inorder(root): if root is None: return
print_inorder(root.left)
print(root.value, end=' ')
print_inorder(root.right)
Example usage
Tree 1
root1 = Node(4) root1.left = Node(2) root1.right = Node(5) root1.left.left = Node(1) root1.left.right = Node(3)
Tree 2
root2 = Node(7) root2.left = Node(6) root2.right = Node(8)
Combine the trees
combined_root = combine_bsts(root1, root2)
Print the combined tree
print("Inorder traversal of the combined tree:") print_inorder(combined_root)
24.Program to Count number of nodes in a binary tree class Node: def init(self, data): self.left=None self.right=None self.data=data
def count(root): if root is None: return 0 else: return 1+count(root.left)+count(root.right)
root=Node(5) root.left=Node(2) root.right=Node(4) root.left.left=Node(5) root.left.right=Node(3) val=count(root) print("count = ",val) 25.
- Program to remove all nodes which don’t line in any path with sum >= K.
class Node: def _init_(self, value): self.value = value self.left = None self.right = None
def remove_nodes(root, k): if root is None: return None
root.left = remove_nodes(root.left, k)
root.right = remove_nodes(root.right, k)
if root.left is None and root.right is None:
if root.value < k:
return None
return root
def print_tree(root): if root is None: return
print_tree(root.left)
print(root.value, end=' ')
print_tree(root.right)
Example usage
root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7)
k = 6
print("Original tree:")