python code for binary search tree

#Complete Binary Search Tree Using Python 3

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

class binarytree:
    def __init__(self):
        self.root=None

#INSERT

    def insert(self,data):
        if self.root==None:				
            self.root=node(data)
        else:
            self._insert(data,self.root)
    def _insert(self,data,cur_node):
        if data<cur_node.data:
            if cur_node.left==None:			
                cur_node.left=node(data)
            else:
                self._insert(data,cur_node.left) 
        elif data>cur_node.data:			
            if cur_node.right==None:
                cur_node.right=node(data)
            else:
                self._insert(data,cur_node.right)
        else:
            print('Data In Treee Already')

#REMOVE

    def remove(self,data):
        if self.root!=None:
            self._remove(data,self.root)
    def _remove(self,data,cur_node):
        if cur_node == None:
            return cur_node
        if data<cur_node.data:
            cur_node.left=self._remove(data,cur_node.left)
        elif data>cur_node.data:
            cur_node.right=self._remove(data,cur_node.right)
        else:
            if cur_node.left is None and cur_node.right is None:
                print('Removing Leaf Node')
                if cur_node==self.root:
                    self.root=None
                del cur_node
                return None
            if cur_node.left is None:
                print('Removing None with Right Child')
                if cur_node==self.root:
                    self.root=cur_node.right
                tempnode=cur_node.right
                del cur_node
                return tempnode
            elif cur_node.right is None:
                print('Removing None with Left Child')
                if cur_node==self.root:
                    self.root=cur_node.left
                tempnode=cur_node.left
                del cur_node
                return tempnode
            print('Removing Node with 2 Children')
            tempnode=self.getpred(cur_node.left)
            cur_node.data=tempnode.data
            cur_node.left=self._remove(cur_node.data,cur_node.left)
        return cur_node
    def getpred(self,cur_node):
        if cur_node.right!=None:
            return self.getpred(cur_node.right)
        return cur_node

#INORDER TRAVERSAL

    def inorder(self):
        if self.root!=None:
            self._inorder(self.root)
    def _inorder(self,cur_node):
        if cur_node!=None:
            self._inorder(cur_node.left)
            print(cur_node.data)
            self._inorder(cur_node.right)

#PREORDER TRAVERSAL

    def preorder(self):
        if self.root!=None:
            self._preorder(self.root)
    def _preorder(self,cur_node):
        if cur_node!=None:
            print(cur_node.data)
            self._preorder(cur_node.left)
            self._preorder(cur_node.right)

#POSTORDER TRAVERSAL

    def postorder(self):
        if self.root!=None:
            self._postorder(self.root)
    def _postorder(self,cur_node):
        if cur_node!=None:
            self._postorder(cur_node.left)
            self._postorder(cur_node.right)
            print(cur_node.data)

#MINIMUM VALUE

    def minval(self):
        if self.root!=None:
            return self._minval(self.root)
    def _minval(self,cur_node):
        if cur_node.left!=None:
            return self._minval(cur_node.left)
        return cur_node.data

#MAXIMUM VALUE

    def maxval(self):
        if self.root!=None:
            return self._maxval(self.root)
    def _maxval(self,cur_node):
        if cur_node.right!=None:
            return self._maxval(cur_node.right)
        return cur_node.data

tree=binarytree()

tree.insert(100)
tree.insert(90)					#			 100
tree.insert(110)				#			/	\
tree.insert(95)					#          90   110
tree.insert(30)					#		  /  \
								#		30    95 
tree.remove(110)
tree.remove(90)

tree.inorder()
#tree.preorder()
#tree.postorder()

print(tree.minval())
print(tree.maxval())

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
search an elements in binary tree with pythonss search element in binary tree with pythonss search element in binary tree in python search element in binary tree using python search element in binary tree python python binary search tree library binary search tree python uses in games binary search tree python uses binary search tree python search is there a built in binary search tree in python? binary search tree (bst) python binary search tree inplementation in python binary search tree array implementation python binary search tree implementation in python binary search tree implementation python BINSARY SEARCH TREE PYTHON\ full binary search tree implementation python binary tree search in data structure python Binary tree/binary search tree in python optimal binary search tree in python binary search tree using python python search method binary tree python binary tree search for node binary search tree python implementation python binary search tree .info python how to print a binary search tree implementing binary search tree in python binary search tree search python python program to implement binary search tree how to create binary search trees in python how to use built in binary search tree python binary search tree in python programz search operation in binary search tree python search binary tree python binary search tree search function python how to search a binary tree python search a node in binary tree in python binary search tree using python 3 binary search tree search operation in python binary tree search python binary search tree python code bst data structure python bst insertion python python bst coding python code for searching node from BST python code for search in binary search tree python program for inserting data in BST and also add functions for preorder,postorder and inorder traversal program for data insert into BST into python python binary search python program for binary search binary search tree insertion python implement bst python python binary search trees make binary search tree python Search in a Virtually Complete Binary Tree python insertion bst python create a binary search tree python python bst insertion in bst py binary search tree python insert node tree python FIND ELEMENT IN BINARY SEARCH TREE IN PYTHON problems related to binary search tree in python binary search tree program in python search tree in python binary search tree in python insert node bst pythonp search in bst in c search for an element in bst java bst insert into binary search tree enter value in bst how values are inserter in a binary tree binary search tree data structure in c how to construct a adt binary search tree using a list of names binary search tree c++ implementation Develop a menu driven program to implement Binary Tree/Binary search tree to perform the following operations. i)Insertion ii) Traversing in different order(Depth first Traversal) iii) Search and display the node and its parent node. iv) To find height creation of binary search tree in c binary search tree c++ bst in python binary tree insertion and deletion c ++ binary search trees bst python program construct all possible binary search tree for keys 1 to n and display the post order traversal of each tree construct all possible binary search tree for keys 1 to N and display the post order traversal of each tree design a binary search tree python binary search tree bst insert cpp python sample binary search trees binary search algorithm python binary search tree definition python how elements are inserted in bst bs tree implementation bst class bst insert recursive java insert bst python code for binary search tree
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source