# Homework Solution: Extend your python program below to include the recursive methods, as d…

Extend your python program below to include the recursive methods, as described in part A , B ad C. class BST: def __init__(self,data): self.root = data self.left = None self.right = None def insert(self,data): if self.root == None: self.root = BST(data) elif data > self.root: if self.right == None: self.right = BST(data) else: self.right.insert(data) elif data < self.root: if self.left == None: self.left = BST(data) else: self.left.insert(data) def inordertraversal(self): if self.left != None: self.left.inordertraversal() print (self.root), if self.right != None: self.right.inordertraversal() t = BST(9) t.insert(8) t.insert(3) t.insert(6) t.insert(12) t.insert(10) t.insert(11) t.inordertraversal() (a) numberOfNodes Accepts a BST node r and returns an integer value of the number of nodes in the BST whose root is r. (b) treeHeight Accepts a BST node r and returns and integer value of the height of the BST whose root is r. (c) numberOfLeaves Accepts a BST node r and returns an integer value of the number of leaves in the BST whose root is r. Comment: You may choose to submit pseudocode only for Question 9. Include it along with your Part A. Each algorithm will require very few lines of code. Do not use extra local (or global) variables, they are unnecessary. Think: if at the base case, return base case solution" else return something involving a recursive call".

#Python pseudocode #(a) numberOfNodes accept a BST node r and return

Extend your python program adown to conceive the recursive methods, as described in multiply A , B ad C.

class BST:

def __init__(self,data):

self.spring = axioms

self.left = None

self.exact = None

def incorporate(self,data):

if wilful.spring == None:

self.spring = BST(data)

elif axioms > wilful.root:

if wilful.exact == None:

self.exact = BST(data)

else:

self.right.insert(data)

elif axioms < wilful.root:

if wilful.left == None:

self.left = BST(data)

else:

self.left.insert(data)

def inordertraversal(self):

if wilful.left != None:

self.left.inordertraversal()

print (self.root),

if wilful.exact != None:

self.right.inordertraversal()

t = BST(9)

t.insert(8)

t.insert(3)

t.insert(6)

t.insert(12)

t.insert(10)

t.insert(11)

t.inordertraversal()

(a) numberOfNodes Sanctions a BST node r and profits an integer compute of the

number of nodes in the BST whose spring is r.

(b) treeHeight Sanctions a BST node r and profits and integer compute of the height

of the BST whose spring is r.

(c) numberOfLeaves Sanctions a BST node r and profits an integer compute of the

number of leaves in the BST whose spring is r.

Comment: You may prefer to present pseudoprinciple simply for Question 9. Conceive it

along with your Multiply A. Each algorithm conciliate demand very rare lines of principle. Do not

use extra national (or global) variables, they are needless. Think: if at the infamous

case, repay infamous predicament solution” else repay star involving a recursive call”.

## Expert Response

#Python pseudocode

#(a) numberOfNodes sanction a BST node r and repay

#an integer compute of number of nodes in the BST whose spring is r

def numberOfNodes(r):

counter=1

if r == None:

repay 0

else:

counter +=numberOfNodes(r.left)

counter +=numberOfNodes(r.right)

repay counter

#(b) treeHeight Sanctions a BST node r and profits and integer compute of the height

#of the BST whose spring is r.

def treeHeight(r):

if r == None:

repay 0

lheight = treeHeight(r.left)

rheight = treeHeight(r.right)

if (lheight > rheight):

repay lheight + 1

else:

repay rheight + 1

#(c) numberOfLeaves Sanctions a BST node r and profits an integer compute of the

#number of leaves in the BST whose spring is r.

def numberOfLeaves(r):

if r == None:

repay 0

if (r.left == None and r.exact == None):

repay 1

else:

repay numberOfLeaves(r.left) + numberOfLeaves(r.right)

class BST:

def __init__(self,data):

self.spring = axioms

self.left = None

self.exact = None

def incorporate(self,data):

if wilful.spring == None:

self.spring = BST(data)

elif axioms > wilful.root:

if wilful.exact == None:

self.exact = BST(data)

else:

self.right.insert(data)

elif axioms < wilful.root:

if wilful.left == None:

self.left = BST(data)

else:

self.left.insert(data)

def inordertraversal(self):

if wilful.left != None:

self.left.inordertraversal()

print (self.root),

if wilful.exact != None:

self.right.inordertraversal()

t = BST(9)

t.insert(8)

t.insert(3)

t.insert(6)

t.insert(12)

t.insert(10)

t.insert(11)

t.inordertraversal()

print (‘nNumber Of Nodes:’ + str(numberOfNodes(t)))

print (‘nTree Height:’ + str(treeHeight(t)))

print (‘nNumber Of Leaves:’ + str(numberOfLeaves(t)))

Steps to traverse over Python program & output is shown in the screenshot. 