0% found this document useful (0 votes)
5 views

TREES

The document provides definitions and characteristics of various types of binary trees, including full, complete, and perfect binary trees. It also includes several functions that perform operations on binary trees, such as calculating height, checking depth, and finding maximum width. Additionally, it discusses recursive functions for traversing and comparing binary trees.

Uploaded by

shwetarana155
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

TREES

The document provides definitions and characteristics of various types of binary trees, including full, complete, and perfect binary trees. It also includes several functions that perform operations on binary trees, such as calculating height, checking depth, and finding maximum width. Additionally, it discusses recursive functions for traversing and comparing binary trees.

Uploaded by

shwetarana155
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Trees

Mohd Ayyoob
(M.Tech CSA, IISc Banglore)
Full Binary Tree Complete Binary Tree Perfect Binary Tree
All nodes have All levels except last is filled. A CBT where the last
either 0 children or 2 Last level is left filled level is also filled
Depth of a NODE
• Number of edges from the node to root
• A root has depth 0
Height of a NODE
• Number of edges in the longest path from the node to leaf or
• Maximum number of edges from node to leaf
• A leaf node has height 0
For a BINARY TREE

For a FULL BINARY TREE


Q3. What does fun() do for a given root node
of non-empty binary tree?

def fun(t):
a, b = 0, 0
If not t.left and not t.right:
return 0
If t.left:
a = fun(t.left)
If t.right:
b = fun(t.right)
ans = max(a, b) + 1
return ans

A. Calculate total number of leaves


B. Calcualte total nodes
C. Calculate height of Tree
D. Calculate total internal nodes
Q3. What does fun() do for a given root node
of non-empty binary tree?

def fun(t):
a, b = 0, 0
If not t.left and not t.right:
return 0
If t.left:
a = fun(t.left)
If t.right:
b = fun(t.right)
ans = max(a, b) + 1
return ans

A. Calculate total number of leaves


B. Calcualte total nodes
C. Calculate height of Tree
D. Calculate total internal nodes
Q3. What does fun() do for a given root node
of non-empty binary tree?

def fun(t):
a, b = 0, 0
If not t.left and not t.right:
return 0
If t.left:
a = fun(t.left)
If t.right:
b = fun(t.right)
ans = max(a, b) + 1
return ans

A. Calculate total number of leaves


B. Calcualte total nodes
C. Calculate height of Tree
D. Calculate total internal nodes
Q3. What does fun() do for a given root node
of non-empty binary tree?

def fun(t):
a, b = 0, 0
If not t.left and not t.right:
return 0
If t.left:
a = fun(t.left)
If t.right:
b = fun(t.right)
ans = max(a, b) + 1
return ans

A. Calculate total number of leaves


B. Calcualte total nodes
C. Calculate height of Tree
D. Calculate total internal nodes
Q4. What does fun(root) return for a given tree?

def fun(root):
If not root:
return 0
else if root.left is None and root.right is None:
return 2
else:
return 3 + fun(root.left) + func(root.right)
Q4. What does fun(root) return for a given tree?

def fun(root):
If not root:
return 0
else if root.left is None and root.right is None:
return 2
else:
return 3 + fun(root.left) + func(root.right)
Q5. What does checkDEEP(root) return for a given tree?

def checkDEEP(root):
temp = None
If not root :
return None
Enqueue(root)

while not IsEmptyQueue() :


temp = Dequeue()
If temp.left:
Enqueue(temp.left)
If temp.right:
Enqueue(temp.right)
return temp
Q5. What does checkDEEP(root) return for a given tree?

def checkDEEP(root):
temp = None
If not root :
return None
Enqueue(root)

while not IsEmptyQueue() :


temp = Dequeue()
If temp.left:
Enqueue(temp.left)
If temp.right:
Enqueue(temp.right)
return temp
Q6. What does findSomething(root) return for a given tree?
def findSomething(root):
level = 0
If not root :
return 0
Enqueue(root)
Enqueue(None)

while not IsEmptyQueue() :


root = Dequeue()
If root is None:
if not IsEmptyQueue():
Enqueue(None)
level += 1
else:
if root.left:
Enqueue(root.left)
if root.right:
Enqueue(root.right)
return level
Q6. What does findSomething(root) return for a given tree?
def findSomething(root):
level = 0
If not root :
return 0
Enqueue(root)
Enqueue(None)

while not IsEmptyQueue() :


root = Dequeue()
If root is None:
if not IsEmptyQueue():
Enqueue(None)
level += 1
else:
if root.left:
Enqueue(root.left)
if root.right:
Enqueue(root.right)
return level
Q7. What does findSomething(root) return for a given tree?
def findSomething(root):
level = 0
If not root :
return 0
Enqueue(root)
Enqueue(None)
while not IsEmptyQueue() :
root = Dequeue()
If root is None:
if not IsEmptyQueue():
Enqueue(None)
level += 1
else:
if root.value > threshold:
if root.left:
Enqueue(root.left)
if root.right:
Enqueue(root.right)
else:
continue
return level
Q9. What does maxWidth(root) return for a given tree?

def maxWidth(root):
if not root:
return 0
Enqueue(root)
j= 0
while not IsEmptyQueue():
i= len(queue)
j= max(j, i)
for _ in range(i):
node = Dequeue()
if node.left:
Enqueue(node.left)
if node.right:
Enqueue(node.right)
return j
Q9. What does maxWidth(root) return for a given tree?

def maxWidth(root):
if not root:
return 0
Enqueue(root)
j= 0
while not IsEmptyQueue():
i= len(queue)
j= max(j, i)
for _ in range(i):
node = Dequeue()
if node.left:
Enqueue(node.left)
if node.right:
Enqueue(node.right)
return j
Q10. What does check(root1, root2) return for a given tree?

def check(root1, root2):


if root1 is None and root2 is None:
return 1
if root1 is None or root2 is None:
return 0
return root1.data == root2.data and
check(root1.left, root2.right) and
check(root1.right, root2.right)
Q11. What does below function?

def check(root1, root2):


if root1 is None and root2 is None:
return 1
if root1 is None or root2 is None:
return 0
return (
check(root1.left, root2.right) and
check(root1.right, root2.right)
)
Q12. What does below function?

def do(t):
if t:
do(t.left)
do(t.right)
t.right, t.left = t.left, t.right
Q12. What does below function?

def do(t):
if t:
do(t.left)
do(t.right)
t.right, t.left = t.left, t.right

You might also like