Free Online Tutorials  Go To Your University  Placement Preparation  Online Live ClassesYoutube Live Link

SUMMER TRAINING AT GOEDUHUB TECHNOLOGIES, JAIPUR (Call:- 7976731765)Project Based Best Summer Training Courses in Jaipur

0 like 0 dislike
in Examples, Exercises and Projects by Goeduhub's Expert (8.1k points)
Binary Search Tree Search Operation | Python implementation

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.1k points)
selected by
Best answer
class BinarySearchTreeNode:
    def __init__(selfdata): = data
        self.left = None
        self.right = None

    def add_child(selfdata):
        if data ==
            return # node already exist

        if data <
            if self.left:
                self.left = BinarySearchTreeNode(data)
            if self.right:
                self.right = BinarySearchTreeNode(data)

    def search(selfval):
        if == val:
            return True

        if val <
            if self.left:
                return False

        if val >
            if self.right:
                return False

    def in_order_traversal(self):
        elements = []
        if self.left:
            elements += self.left.in_order_traversal()


        if self.right:
            elements += self.right.in_order_traversal()

        return elements

def build_tree(elements):
    print("Building tree with these elements:",elements)
    root = BinarySearchTreeNode(elements[0])

    for i in range(1,len(elements)):

    return root

if __name__ == '__main__':
    countries = ["India","Pakistan","Germany""USA","China","India","UK","USA"]
    country_tree = build_tree(countries)

    print("UK is in the list? ","UK"))
    print("Sweden is in the list? ","Sweden"))

    numbers_tree = build_tree([1741209231834])
    print("In order traversal gives this sorted list:",numbers_tree.in_order_traversal())


About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © Social::   |  |