Skip to main content
edited tags
Link
edited tags
Link
Rollback to Revision 4
Source Link
Heslacher
  • 50.4k
  • 5
  • 81
  • 175
class Node:
'''this is the constructor'''
def __init__(self, data):
    self.left = None
    self.right = None
    self.data = data 
def insert(self, data):
    ''' this function work is insert the data in bst'''
    if self.data:
        '''insert left side value '''
        if data < self.data:
            if not self.left:
                self.left = Node(data)
            else:
                self.left.insert(data)
            '''insert right side value'''
        elif data > self.data :
            if not self.right :
                self.right = Node(data)
            else:
                self.right.insert(data)
    else:
        self.data = data
def getMinValue(self,node):
     '''this fuction work is get minimum value'''
    current = node
    while (current.left is not None):
        current = current.left
    return current
def delValue(self,data):       
    '''this fuction work delete value'''
    if data < self.data:
        self.left = self.left.delValue(data)
    elif data > self.data:
        self.right = self.right.delValue(data)
    else:
        if self.left is None:
            temp = self.right
            self = None
            return temp
        elif self.right is None:
            temp = self.left
            self = None
            return temp
        temp = self.getMinValue(self.right)
        self.data = temp.data
        self.right = self.right.delValue(temp.data)
    return self
'''this function work finde a value in bst or return True ordef False'''getSearchValue(self,data):
    if data == self.data:
        return print(Trueself.data,"True")
    if data < self.data:
        if self.left:
            self.left.getSearchValue(data)   
    if data > self.data:
        if self.right:
            self.right.getSearchValue(data)
        return print(False)
'''this function work print adef tree'''print_tree(self):
    if self.left:
        self.left.printTreeprint_tree()
    print(self.data)
    if self.right:
        self.right.printTreeprint_tree()
class Node:
'''this is the constructor'''
def __init__(self, data):
    self.left = None
    self.right = None
    self.data = data
def insert(self, data):
    ''' this function work is insert the data in bst'''
    if self.data:
        '''insert left side value '''
        if data < self.data:
            if not self.left:
                self.left = Node(data)
            else:
                self.left.insert(data)
            '''insert right side value'''
        elif data > self.data :
            if not self.right :
                self.right = Node(data)
            else:
                self.right.insert(data)
    else:
        self.data = data
def getMinValue(self,node):
     '''this fuction work is get minimum value'''
    current = node
    while current.left is not None:
        current = current.left
    return current
def delValue(self,data):       
    '''this fuction work delete value'''
    if data < self.data:
        self.left = self.left.delValue(data)
    elif data > self.data:
        self.right = self.right.delValue(data)
    else:
        if self.left is None:
            temp = self.right
            self = None
            return temp
        elif self.right is None:
            temp = self.left
            self = None
            return temp
        temp = self.getMinValue(self.right)
        self.data = temp.data
        self.right = self.right.delValue(temp.data)
    return self
'''this function work finde a value in bst or return True or False'''
    if data == self.data:
        return print(True)
    if data < self.data:
        if self.left:
            self.left.getSearchValue(data)
    if data > self.data:
        if self.right:
            self.right.getSearchValue(data)
        return print(False)
'''this function work print a tree'''
    if self.left:
        self.left.printTree()
    print(self.data)
    if self.right:
        self.right.printTree()
def __init__(self, data):
    self.left = None
    self.right = None
    self.data = data 
def insert(self, data):
    if self.data:
        if data < self.data:
            if not self.left:
                self.left = Node(data)
            else:
                self.left.insert(data)
        elif data > self.data:
            if not self.right :
                self.right = Node(data)
            else:
                self.right.insert(data)
    else:
        self.data = data
def getMinValue(self,node):
    current = node
    while(current.left is not None):
        current = current.left
    return current
def delValue(self,data):       
    if data < self.data:
        self.left = self.left.delValue(data)
    elif data > self.data:
        self.right = self.right.delValue(data)
    else:
        if self.left is None:
            temp = self.right
            self = None
            return temp
        elif self.right is None:
            temp = self.left
            self = None
            return temp
        temp = self.getMinValue(self.right)
        self.data = temp.data
        self.right = self.right.delValue(temp.data)
    return self
def getSearchValue(self,data):
    if data == self.data:
        return print(self.data,"True")
    if data < self.data:
        if self.left:
            self.left.getSearchValue(data)   
    if data > self.data:
        if self.right:
            self.right.getSearchValue(data)
def print_tree(self):
    if self.left:
        self.left.print_tree()
    print(self.data)
    if self.right:
        self.right.print_tree()
added 317 characters in body
Source Link
Loading
edited tags; edited title
Link
AlexV
  • 7.3k
  • 2
  • 24
  • 47
Loading
edited title
Link
Loading
added 3 characters in body
Source Link
AlexV
  • 7.3k
  • 2
  • 24
  • 47
Loading
Source Link
Loading