1

I am Trying to solve the following question Monk and Cursed Tree

In this Question i am given a list of values which are inserted into the Binary Search Tree and then other 2 values are given.The starting node and the ending node.Between these values,i need to find the maximum value(including the starting node and Ending Node)

Example-4,7,8,6,3

Start Node-3

Ending Node-7

The Binary Search Tree for the given list of values

Binary Search Tree for the given values

So the path is 3->4->7 and the maximum value in this is 7.

What i have tried is create a function lookup which finds the data in the tree and returns it's parent.What i need to do is call the value returned by my Lookup function again and again and appending the value of parent to another list.Simultaneously call Lookup from end node and append it's parent to the same list.This Operation needs to be carried till the same value is being appended to the list.Once this happens,we know that they have the common ancestor and the path is complete.

Here's what i have tried so far

class Node:
def __init__(self,data):
    self.data=data
    self.left=None
    self.right=None

def Insert_BTreeNode(self,data):
    if self.data:
        if data<self.data:
            if self.left is None:
                self.left=Node(data)
            else:
                self.left.Insert_BTreeNode(data)

        elif data>self.data:
            if self.right is None:
                self.right=Node(data)

            else:
                self.right.Insert_BTreeNode(data)

    else:
        self.data=data

def Lookup(self,data,parent=None):

    if data<self.data:
        if self.left is None:
            return None,None
        return self.left.Lookup(data,self)
    elif data>self.data:
        if self.right is None:
            return None,None
        return self.right.Lookup(data,self)
    else:
        if (parent is not None):
            print(parent.data)
        return (parent)

def next_parent(self,Lookup):
    something=Lookup(Lookup)
    return something

b_tree=list(map(int,input().split()))

x,y=map(int,input().split())

root=Node(b_tree[0])

b_treepath=[]

for i in range(1,len(b_tree)):
    root.Insert_BTreeNode(b_tree[i])

z=root.Lookup(x)
a=root.next_parent(z.data)

Help in solving this question is widely appreciated.

Dhruv Marwha
  • 947
  • 2
  • 12
  • 24

1 Answers1

1

With just minor changes to your good code:

class Node:
    def __init__(self, data):
        self.data=data
        self.left=None
        self.right=None

    def Insert_BTreeNode(self, data):
        if self.data:
            if data<self.data:
                if self.left is None:
                    self.left=Node(data)
                else:
                    self.left.Insert_BTreeNode(data)

            elif data>self.data:
                if self.right is None:
                    self.right=Node(data)
                else:
                    self.right.Insert_BTreeNode(data)
        else:
            self.data=data

    def Lookup(self, data, parent=None):
        '''
        returns data and parent
        '''
        #print "In Lookup", self.data, data
        if data<self.data:
            if self.left is None:
                #print "left lookup and data", self.left, data
                return None,None
            return self.left.Lookup(data, parent=self)
        elif data>self.data:
            if self.right is None:
                #print "right lookup and data", self.right, data
                return None,None
            return self.right.Lookup(data, parent=self)
        else:
            if (parent is not None):
                print "parent of", data, "is", parent.data, parent 
            return (parent)

    def next_parent(self,Lookup):
        something=self.Lookup(Lookup)
        return something

#b_tree=list(map(int,input().split()))
b_tree = [4,1,3,2,10,8,6,7,11]
#x,y=map(int,input().split())
x, y = 11, 7
root=Node(b_tree[0])
b_treepath=[]
for i in range(1,len(b_tree)):
    root.Insert_BTreeNode(b_tree[i])

#z=root.Lookup(7)
#print z, z.data
#a=root.next_parent(z.data)
#print a

x_ = [x]
y_ = [y]
xP = root.Lookup(x)
yP = root.Lookup(y)

# finding parents till root node for both
while(xP):
    x_.append(xP.data)
    xP = root.next_parent(xP.data)
while(yP):
    y_.append(yP.data)
    yP = root.next_parent(yP.data)
print x_, y_
# finding lowest common ancestor
b_treepath=[]
x_ = x_[::-1]
y_ = y_[::-1]
l = min(len(x_), len(y_))
for i in range(l):
    if x_[i]!=y_[i]:
        break
# (i-1)th position has LCA
x_ = x_[i-1:]
y_ = y_[i-1:]
#answer
print x_, y_
print max(max(x_), max(y_))

Note: My working environment is Python2.7, so please mind print statements.

devautor
  • 2,278
  • 2
  • 16
  • 28
  • the i is out of loop so we cannot access the x_and y_elements which are-x_ = x_[i-1:] y_ = y_[i-1:] – Dhruv Marwha Apr 11 '17 at 17:02
  • Code runs perfectly well on my system. What does not work for you exactly? Also, note that "scoping unit in Python is a function" and do read this discussion on the scope of for control variable: http://stackoverflow.com/questions/3611760/scoping-in-python-for-loops – devautor Apr 11 '17 at 21:32
  • I'll try the code again and let you know.The code produces the right output i.e the maximum value along the given path right? And why have you reinitialized the x_ and y_ variables with different values again and again.Why not use different variables altogether? – Dhruv Marwha Apr 11 '17 at 21:34
  • Because aesthetic is a personal choice :) For me, the task was to help you with a working script asap. Then, you know better! – devautor Apr 11 '17 at 21:39
  • Let's learn here, together. Depth here is amazing. I am a noob myself! – devautor Apr 11 '17 at 21:43