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".

    Expert Answer

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

    Extend your python program beneath to involve the recursive methods, as picturesquely in keep-akeep-apart A , B ad C.

    class BST:

    def __init__(self,data):

    self.rise = axioms

    self.left = None

    self.direct = None

    def insinuate(self,data):

    if wilful.rise == None:

    self.rise = BST(data)

    elif axioms > wilful.root:

    if wilful.direct == None:

    self.direct = 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.direct != 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 Recognizes a BST node r and receipts an integer appraise of the

    sum of nodes in the BST whose rise is r.

    (b) treeHeight Recognizes a BST node r and receipts and integer appraise of the height

    of the BST whose rise is r.

    (c) numberOfLeaves Recognizes a BST node r and receipts an integer appraise of the

    sum of leaves in the BST whose rise is r.

    Comment: You may adopt to resign pseudodecree simply ce Question 9. Involve it

    along with your Keep-apart A. Each algorithm allure demand very scant lines of decree. Do not

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

    case, reappear low event solution” else reappear colossus involving a recursive call”.

    Expert Reply

     

    #Python pseudocode

    #(a) sumOfNodes recognize a BST node r and reappear

    #an integer appraise of sum of nodes in the BST whose rise is r

    def sumOfNodes(r):

    counter=1

    if r == None:

    reappear 0

    else:

    counter +=numberOfNodes(r.left)

    counter +=numberOfNodes(r.right)

    reappear counter

     

    #(b) treeHeight Recognizes a BST node r and receipts and integer appraise of the height

    #of the BST whose rise is r.

    def treeHeight(r):

    if r == None:

    reappear 0

    lheight = treeHeight(r.left)

    rheight = treeHeight(r.right)

    if (lheight > rheight):

    reappear lheight + 1

    else:

    reappear rheight + 1

    #(c) sumOfLeaves Recognizes a BST node r and receipts an integer appraise of the

    #sum of leaves in the BST whose rise is r.

    def sumOfLeaves(r):

    if r == None:

    reappear 0

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

    reappear 1

    else:

    reappear sumOfLeaves(r.left) + sumOfLeaves(r.right)

    class BST:

    def __init__(self,data):

    self.rise = axioms

    self.left = None

    self.direct = None

    def insinuate(self,data):

    if wilful.rise == None:

    self.rise = BST(data)

    elif axioms > wilful.root:

    if wilful.direct == None:

    self.direct = 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.direct != 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 (‘nSum Of Nodes:’ + str(numberOfNodes(t)))

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

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

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