Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
binary search tree in data structure

Binary search tree in Data Structure

Posted on 4 June 202023 April 2023 By YASH PAL No Comments on Binary search tree in Data Structure

The binary search tree is a tree in which all the values in the left subtree are less than the value of the root node and values of the right subtree are greater than the value of the root node.

binary search tree

as you see in the above example the left subtree has values lesser than the root node’s value and the right subtree has a value greater than the root node’s value.

Properties of Binary search tree

  • All the keys in the left subtree of the root node are less than the key in the root node.
  • All the keys in the right subtree of the root node are greater than the key in the root node.
  • The Left and right subtrees of the root node are also binary search trees.

Program to implement a binary search tree using Python.

class TreeEmptyError(Exception):
    pass

class Node:
    def __init__(self, value):
        self.info = value
        self.lchild = None
        self.rchild = None

class BinarySearchTree:

    def __init__(self):
        self.root = None
    def is_empty(self):
        return self.root is None
    def _insert(self, p, x):
        if p is None:
            p = Node(x)
        elif x < p.info:
            p.lchild = self._insert(p.lchild, x)
        elif x > p.info:
            p.rchild = self._insert(p.rchild, x)
        else:
            print(x, " already present in the tree")
        return p

    def insert1(self, x):
        p = self.root
        par = None        while p is not None:
            par = p
            if x < p.info:
                p = p.lchild
            elif x > p.info:
                p = p.rchild
            else:
                print(x + " already present in the tree")
                return
        temp = Node(x)

        if par is None:
            self.root = temp
        elif x < par.info:
            par.lchild = temp
        else:
            par.rchild = temp

    def search(self, x):
        return self._search(self.root, x) is not None
    def _search(self, p, x):
        if p is None:
            return None        if x < p.info:
            return self._search(p.lchild, x)
        if x > p.info:
            return self._search(p.rchild, x)
        return p

    def search1(self, x):
        p = self.root
        while p is not None:
            if x < p.info:
                p = p.lchild
            elif x > p.info:
                p = p.rchild
            else:
                return True        return False
    def delete(self, x):
        self.root = self._delete(self.root, x)

    def _delete(self, p, x):
        if p is None:
            print(x, " not found")
            return p

        if x < p.info:
            p.lchild = self._delete(p.lchild, x)
        elif x > p.info:
            p.rchild = self._delete(p.rchild, x)
        else:
            if p.lchild is not None and p.rchild is not None:
                s = p.rchild
                while s.lchild is not None:
                    s = s.lchild
                p.info = s.info
                p.rchild = self._delete(p.rchild, s.info)
            else:
                if p.lchild is not None:
                    ch = p.lchild
                else:
                    ch = p.rchild
                p = ch
        return p

    def delete1(self, x):
        p = self.root
        par = None
        while p is not None:
            if x == p.info:
                break            par = p
            if x < p.info:
                p = p.lchild
            else:
                p = p.rchild

            if p is None:
                print(x, " not found")
                return
            if p.lchild is not None and p.rchild is not None:
                ps = p
                s = p.rchild

                while s.lchild is not None:
                    ps = s
                    s = s.lchild
                p.info = s.info
                p = s
                par = ps

            if p.lchild is not None:
                ch = p.lchild
            else:
                ch = p.rchild

            if par is None:
                self.root = ch
            elif p == par.lchild:
                par.lchild = ch
            else:
                par.rchild = ch

    def min1(self):
        if self.is_empty():
            raise TreeEmptyError("Tree is empty")
        p = self.root
        while p.lchild is not None:
            p = p.lchild
        return p.info

    def max1(self):
        if self.is_empty():
            raise TreeEmptyError("Tee is empty")
        p = self.root
        while p.rchild is not None:
            p = p.rchild
        return p.info

    def min2(self):
        if self.is_empty():
            raise TreeEmptyError("Tree is empty")
        return self._min(self.root).info

    def _min(self, p):
        if p.lchild is None:
            return p
        return self._min(p.lchild)

    def max2(self):
        if self.is_empty():
            raise TreeEmptyError("Tree is empty")
        return self._max(self.root).info

    def _max(self, p):
        if p.rchild is None:
            return p
        return self._max(p.rchild)

    def display(self):
        self._display(self.root, 0)
        print()

    def _display(self, p, level):
        if p is None:
            return        self._display(p.rchild, level + 1)
        print()

        for i in range(level):
            print("  ", end='')
        print(p.info)
        self._display(p.lchild, level + 1)

    def preorder(self):
        self._preorder(self.root)
        print()

    def _preorder(self, p):
        if p is None:
            return        print(p.info, " ")
        self._preorder(p.lchild)
        self._preorder(p.rchild)

    def inorder(self):
        self._inorder(self.root)
        print()

    def _inorder(self, p):
        if p is None:
            return        self._inorder(p.lchild)
        print(p.info, " ")
        self._inorder(p.rchild)

    def postorder(self):
        self._postorder(self.root)
        print()

    def _postorder(self, p):
        if p is None:
            return        self._postorder(p.lchild)
        self._postorder(p.rchild)
        print(p.info, " ")

    def height(self):
        return self._height(self.root)

    def _height(self, p):
        if p is None:
            return 0
        hL = self._height(p.lchild)
        hR = self._height(p.rchild)

        if hL > hR:
            return 1 + hL
        else:
            return 1 + hR


###################################
bst = BinarySearchTree()

while True:
    print("1.Display Tree")
    print("2.Search(Iterative)")
    print("3.Search(Recursive)")
    print("4.Insert a new node(Iterative)")
    print("5.Insert a noew node(Recursive)")
    print("6.Delete a node(Iterative)")
    print("7.Delete a node(Recursive)")
    print("8.Find Minimum key(Iterative)")
    print("9.Find Minimum key(Recursive)")
    print("10.Find Maximum key(Iterative)")
    print("11.Find Maximum key(Recursive)")
    print("12.Preorder Traversal")
    print("13.Inorder Traversal")
    print("14.Postoder Traversal")
    print("15.Height of tree")
    print("16.Quit")
    choice = int(input("Enter your choice : "))

    if choice == 1:
        bst.display()
    elif choice == 2:
        x = int(input("Enter the key to be searched : "))
        if bst.search1(x):
            print("Key found")
        else:
            print("Key not found")
    elif choice == 3:
        x = int(input("Enter the key to be searched : "))
        if bst.search(x):
            print("Key found")
        else:
            print("Key not found")

    elif choice == 4:
        x = int(input("Etner the key to be inserte : "))
        bst.insert1(x)
    elif choice == 5:
        x = int(input("Enter the key to be inserted : "))
        bst.insert1(x)
    elif choice == 6:
        x = int(input("Enter the element to be deleted : "))
        bst.delete1(x)
    elif choice == 7:
        x = int(input("Enter the element to be deleted : "))
        bst.delete(x)
    elif choice == 8:
        print("Minimum key is ", bst.min1())
    elif choice == 9:
        print("Minimum key is ", bst.min2())
    elif choice == 10:
        print("Maximum key is ", bst.max1())
    elif choice == 11:
        print("Maximum key is ", bst.max2())
    elif choice == 12:
        bst.preorder()
    elif choice == 13:
        bst.inorder()
    elif choice == 14:
        bst.postorder()
    elif choice == 15:
        print("Height of tree is ", bst.height())
    elif choice == 16:
        break    else:
        print("wrong choice")
    print()
Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Level order traversal of Binary Tree
Next Post: Insertion in Binary Search Tree

Related Tutorials

Reading input in c programming Reading Input in a C program C Programming Tutorials
The First C Program C Programming Tutorials
Compiling C Programs C Programming Tutorials
History of c programming language HISTORY OF C Programming Language C Programming Tutorials
c character sets C Character Sets C Programming Tutorials
c programming interview questions and answers C Programming Interview Questions and Answers C Programming Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme