Question 31
MediumWhat is the balance factor of a node in an AVL tree?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the balance factor of a node in an AVL tree?
Height of left subtree − height of right subtree, which must be −1, 0 or 1
Value of the node
Depth of the node
Number of children
Sign up free to see the answer and a step-by-step explanation.
Which rotation fixes an AVL tree after inserting into the left subtree of the left child (an LL imbalance)?
A left-right double rotation
A single right rotation
No rotation
A single left rotation
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def insert(n, v):
if not n:
return Node(v)
if v < n.v:
n.l = insert(n.l, v)
else:
n.r = insert(n.r, v)
return n
root = None
for v in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, v)
def ino(n):
return [] if not n else ino(n.l) + [n.v] + ino(n.r)
print(ino(root)[2])
60
50
Sign up free to see the answer and a step-by-step explanation.
Which of these sequences could NOT be the inorder traversal of a BST?
7, 8, 9, 10
10, 20, 15, 30
5, 10, 15, 20
1, 2, 3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v):
self.v, self.l, self.r = v, None, None
def ins(n, v):
if not n:
return N(v)
if v < n.v:
n.l = ins(n.l, v)
else:
n.r = ins(n.r, v)
return n
r = None
for v in [50, 30, 70, 20, 40, 60, 80, 35]:
r = ins(r, v)
def pre(n):
return [n.v] + pre(n.l) + pre(n.r) if n else []
print(pre(r))
[50, 30, 20, 40, 35, 70, 60, 80]
[50, 30, 20, 35, 40, 70, 60, 80]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v):
self.v, self.l, self.r = v, None, None
def ins(n, v):
if not n:
return N(v)
if v < n.v:
n.l = ins(n.l, v)
else:
n.r = ins(n.r, v)
return n
r = None
for v in [8, 3, 10, 1, 6, 14, 4, 7, 13]:
r = ins(r, v)
def kth(n, k):
st = []
while True:
while n:
st.append(n); n = n.l
n = st.pop()
k -= 1
if k == 0:
return n.v
n = n.r
print(kth(r, 4))
6
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v):
self.v, self.l, self.r = v, None, None
def ins(n, v):
if not n:
return N(v)
if v < n.v:
n.l = ins(n.l, v)
else:
n.r = ins(n.r, v)
return n
r = None
for v in [8, 3, 10, 1, 6, 14, 4, 7, 13]:
r = ins(r, v)
def floor(n, x):
best = None
while n:
if n.v == x:
return x
if n.v < x:
best, n = n.v, n.r
else:
n = n.l
return best
print(floor(r, 12), floor(r, 5), floor(r, 0))
10 4 None
13 6 None
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def valid(n, lo=float("-inf"), hi=float("inf")):
if n is None:
return True
v, l, r = n
return lo < v < hi and valid(l, lo, v) and valid(r, v, hi)
t1 = (5, (3, None, None), (8, (6, None, None), None))
t2 = (5, (3, None, (6, None, None)), (8, None, None))
print(valid(t1), valid(t2))
False False
False True
Sign up free to see the answer and a step-by-step explanation.
A BST is built by inserting 40, 20, 60, 10, 30, 50, 70, 25. What is the height (number of levels)?
4
3
5
8
Sign up free to see the answer and a step-by-step explanation.
Deleting 20 from the BST built from 40, 20, 60, 10, 30, 50, 70, 25 (using the inorder successor). What replaces 20?
30
40
10
25
Sign up free to see the answer and a step-by-step explanation.
30
40
[50, 30, 70, 20, 40, 60, 80, 35]
[20, 30, 35, 40, 50, 60, 70, 80]
3
7
10 6 1
14 4 0
True True
True False