Traversals on a Binary Search Tree

There is a binary search tree equipped with the following methods besides the usual ones:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def printLCR(node):
    if node.val == None:
        return
    printRLC(node.left)
    print(node.val)
    printRLC(node.right)

def printRLC(node):
    if node.val == None:
        return
    printLCR(node.right)
    printLCR(node.left)
    print(node.val)

The following data (in the following order) are inserted into the binary search tree:

10, 4, 17, 2, 15, 1, 6, 29, 9, 14, 13, 8, 16

If the output of printRLC is a 0 , a 1 , , a 12 a_0, a_1, \ldots, a_{12} , compute i = 0 12 ( i × a i ) \displaystyle\sum _{i=0} ^{12} {(i \times a_{i})} .


If something is not covered, it can be asked here . Changes will be made!


The answer is 699.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Zeeshan Ali
Apr 28, 2018

The BST that we get by inserting the given set of values in order is as follows; And the printRLC(root) gives the following result; 16,13,14,15,17,29,1,2,4,8,9,6,10. Therefore; i = 0 12 ( i × a i ) = 0 × 16 + 1 × 13 + 2 × 14 + 3 × 15 + 4 × 17 + 5 × 29 + 6 × 1 + 7 × 2 + 8 × 4 + 9 × 8 + 10 × 9 + 11 × 6 + 12 × 10 = 699 \displaystyle\sum _{i=0} ^{12} {(i \times a_{i})}=0\times16+1\times13+2\times14+3\times15+4\times17+5\times29+6\times1+7\times2+8\times4+9\times8+10\times9+11\times6+12\times10=699

Following is the python3 code;

  • Create the above BST and call printRLC(root) on that.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Node:
    def __init__(self,key):
        self.val = key
        self.left = None
        self.right = None

def insert(root, node):
    if not root:
        root = node
    else:
        if root.val < node.val:
            if not root.right:
                root.right = node
            else:
                insert(root.right, node)
        else:
            if not root.left:
                root.left = node
            else:
                insert(root.left, node)

def inorder(root):
    if root:
        inorder(root.left)
        print(root.val)
        inorder(root.right)        

def printLCR(root):
    if root:
        printRLC(root.left)
        print(root.val, end=',')
        printRLC(root.right)

def printRLC(root):
    if root:
        printLCR(root.right)
        printLCR(root.left)
        print(root.val, end=',')

VALUES = [10, 4, 17, 2, 15, 1, 6, 29, 9, 14, 13, 8, 16]

tree = Node(10)
for v in VALUES[1:]:
    insert(tree, Node(v))

print ("RESULT ", end="= ")
printRLC(tree)

1
RESULT = 16,13,14,15,17,29,1,2,4,8,9,6,10,

  • Using the above result we can now calculate the summation.
1
2
3
4
5
SUM = 0
RESULT = [16,13,14,15,17,29,1,2,4,8,9,6,10]
for i in range(len(RESULT)):
    SUM += i * RESULT[i]
print ("SUM =", SUM)

1
SUM = 699

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...