Trees, Heaps, Priority Queues, Tries, Pt. 2 Practice Session

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Trees, Heaps, Priority Queues,

Tries, pt. 2
Practice Session
Azamat Ordabekov
Delete a node from Binary Heap
Given a Binary Heap (max heap). What happens if we delete a tree
root? Explain step by step
Delete a node from Binary Heap
Problem Set: Binary Search Tree Sum
Given Binary Search Tree. Write a function, which returns the sum of the
tree.
Example:
Input: Output: 128
15
/ \
7 35
/ \ /\
1 10 20 40
Problem Set: Binary Search Tree Sum
This problem can be solved by using recursion.
Tree Structure:

Given function input:


int sum(TreeNode node)
What is it recurrence relation?
What cases can have this function?
Problem Set: Binary Search Tree Sum
This problem can be solved by using recursion.
Given function input:
int sum(TreeNode node)
What is it recurrence relation:
node.value + sum(node.left) + sum(node.right)
What cases can have this function?
1. If node equals to NULL, then return 0
2. In all other cases return recurrence relation
Problem Set: Binary Search Tree Sum
Problem Set: BST Range Sum
Given a binary search tree, return the sum of values of all nodes with
value between L and R (inclusive).
Example:
Input: L = 9, R = 17Output: 25
15
/ \
7 35
/ \ /\
1 10 20 40
Problem Set: BST Range Sum
This problem can be solved by using recursion.
Tree Structure:

Given function input:


int sum(TreeNode node, int L, int R)
What is it recurrence relation?
What cases can have this function?
Problem Set: BST Range Sum
This problem can be solved by using recursion.
Given function input:
int sum(TreeNode node, int L, int R)
What is it recurrence relation:
node.value + sum(node.left, L, R) + sum(node.right, L, R)
What cases can have this function?
1. If node equals to NULL, then return 0
2. If the value of node is in the range, then return next recurrence relation:
node.value + sum(node.left, L, R) + sum(node.right, L, R)
3. If the value not in the range, then return default recurrence relation:
sum(node.left, L, R) + sum(node.right, L, R)
Problem Set: BST Range Sum
Problem Set: Symmetric Tree
Given a binary tree, write a function which checks whether the tree is
symmetric or not
Example:
Input: Output: True
6
/\
5 5
/\/\
1 10 10 1
Problem Set: Symmetric Tree
Input: Output: False
20
/\
12 12
\ \
5 5
Problem Set: Symmetric Tree
This problem can be solved by using recursion.
Tree Structure:

Given two function inputs:


1. bool isSymmetric(TreeNode node)
2. Bool checker(TreeNode left, TreeNode right)
What are recurrence relations of each function?
What cases can have these functions?
Problem Set: Symmetric Tree
Given two function inputs:
1. bool isSymmetric(TreeNode node)
2. Bool checker(TreeNode left, TreeNode right)
What are recurrence relations of each function?
First function: node == null || checker(node.left, node.right)
Second function: checker(left.left, right.right) && checker(left.right,
right.left)
What cases can have these functions?
Problem Set: Symmetric Tree

You might also like