binary search tree in python

Solutions on MaxInterview for binary search tree in python by the best coders in the world

showing results for - "binary search tree in python"
Mariam
09 Jan 2019
1class Binarytree:
2    def __init__(self,data):
3        self.data = data
4        self.left = None
5        self.right = None
6    
7    def addChild(self, data):
8        if data == self.data:
9            return
10        
11        if data < self.data:
12            if self.left:
13                self.left.addChild(data)
14            else:
15                self.left = Binarytree(data)
16        else:
17            if self.right:
18                self.right.addChild(data)
19            else:
20                self.right = Binarytree(data)
21    
22    # This functions find element in binary tree
23    def search(self,val):
24        if val == self.data:
25            return True
26        if val < self.data:
27            if self.left:
28                return self.left.search(val)
29            else:
30                return False
31        else:
32            if self.right:
33                return self.right.search(val)
34            else:
35                return False
36
37def buildtree(element):
38    root = Binarytree(element[0])
39    for i in range(1,len(element)):
40        root.addChild(element[i])
41    return root
42    
43if __name__ == '__main__':
44    element = [39, 87, 21, 42, 95, 52, 12]
45    tree = buildtree(element)
46    print(tree.search(38))
Emiliano
18 May 2019
1class Binarytree:
2    def __init__(self,data):
3        self.data = data
4        self.left = None
5        self.right = None
6    
7    def addChild(self, data):
8        if data == self.data:
9            return
10        
11        if data < self.data:
12            if self.left:
13                self.left.addChild(data)
14            else:
15                self.left = Binarytree(data)
16        else:
17            if self.right:
18                self.right.addChild(data)
19            else:
20                self.right = Binarytree(data)
21    
22    def inorder(self):
23        element = [ ]
24        
25        if self.left:
26            element += self.left.inorder()
27        
28        element.append(self.data)
29        
30        if self.right:
31            element += self.right.inorder()
32        
33        return element
34    
35    def search(self,val):
36        if val == self.data:
37            return True
38        if val < self.data:
39            if self.left:
40                return self.left.search(val)
41            else:
42                return False
43        else:
44            if self.right:
45                return self.right.search(val)
46            else:
47                return False
48
49def buildtree(element):
50    root = Binarytree(element[0])
51    for i in range(1,len(element)):
52        root.addChild(element[i])
53    return root
54    
55if __name__ == '__main__':
56    element = [39, 87, 21, 42, 95, 52, 12]
57    tree = buildtree(element)
58    print(tree.inorder())
59    print(tree.search(38))
Dario
14 Aug 2018
1class BinaryTree:
2
3    def __init__(self, value):
4
5        self.left = None
6        self.right = None
7        self.value = value
8
9    def insert(self, value):
10
11        if self.value:
12            if data < self.value:
13                if self.left is None:
14                    self.left = BinaryTree(value)
15                else:
16                    self.left.insert(value)
17            elif data > self.value:
18                if self.right is None:
19                    self.right = BinaryTree(value)
20                else:
21                    self.right.insert(value)
22        else:
23            self.value = value
24
25    def PrintTree(self):
26        if self.left:
27            self.left.PrintTree()
28        print( self.data),
29        if self.right:
30            self.right.PrintTree()
31
32root = BinaryTree(100)
33root.insert(50)
34root.insert(55)
35root.insert(60)
36root.insert(20)
37root.insert(52)
38
39
40root.PrintTree()
41PythonCopy
Louis
20 Feb 2020
1class Binary:
2    def __init__(self, data):
3        self.data = data
4        self.left = None
5        self.right = None
6    
7    def addChild(self, data):
8        if data == self.data:
9            return
10        if data < self.data:
11            if self.left:
12                self.left.addChild(data)
13            else:
14                self.left = Binary(data)
15        else:
16            if self.right:
17                self.right.addChild(data)
18            else:
19                self.right = Binary(data)
20    
21    def inorder(self):
22        ele = []
23        
24        if self.left:
25            ele += self.left.inorder()
26        ele.append(self.data)
27        
28        if self.right:
29            ele += self.right.inorder()
30        
31        return ele
32    
33    def search(self, data):
34        if data == self.data:
35            return True
36        if data < self.data:
37            if self.left:
38                return self.left.search(data)
39            else:
40                return False
41        else:
42            if self.right:
43                return self.right.search(data)
44            else:
45                return False
46    
47    def find_min(self):
48        if self.left is None:
49            return self.data
50        return self.left.find_min()
51    
52    def find_max(self):
53        if self.right is None:
54            return self.data
55        return self.right.find_max()
56    
57    def delete(self, val):
58        if val < self.data:
59            if self.left:
60                self.left = self.left.delete(val)
61        elif val > self.data:
62            if self.right:
63                self.right = self.right.delete(val)
64        else:
65            if self.left is None and self.right is None:
66                return None
67            if self.left is None:
68                return self.right
69            if self.right is None:
70                return self.left
71            
72            min_val = self.right.find_min()
73            self.data = min_val
74            self.right = self.right.delete(val)
75        
76        return self
77
78
79
80def build(element):
81    root = Binary(element[0])
82    for i in range(1,len(element)):
83        root.addChild(element[i])
84    return root
85
86if __name__ == '__main__':
87    element = [32, 89, 12, 94, 23, 61, 2]
88    tree = build(element)
89    print(tree.inorder())
90    print(tree.search(62))
91    print(tree.find_min())
92    print(tree.find_max())
93    tree.delete(12)
94    print(tree.inorder())
95    
Francesca
21 Sep 2018
1#Complete Binary Search Tree Using Python 3
2
3class node:
4    def  __init__(self,data):
5        self.data=data
6        self.left=None
7        self.right=None
8
9class binarytree:
10    def __init__(self):
11        self.root=None
12
13#INSERT
14
15    def insert(self,data):
16        if self.root==None:				
17            self.root=node(data)
18        else:
19            self._insert(data,self.root)
20    def _insert(self,data,cur_node):
21        if data<cur_node.data:
22            if cur_node.left==None:			
23                cur_node.left=node(data)
24            else:
25                self._insert(data,cur_node.left) 
26        elif data>cur_node.data:			
27            if cur_node.right==None:
28                cur_node.right=node(data)
29            else:
30                self._insert(data,cur_node.right)
31        else:
32            print('Data In Treee Already')
33
34#REMOVE
35
36    def remove(self,data):
37        if self.root!=None:
38            self._remove(data,self.root)
39    def _remove(self,data,cur_node):
40        if cur_node == None:
41            return cur_node
42        if data<cur_node.data:
43            cur_node.left=self._remove(data,cur_node.left)
44        elif data>cur_node.data:
45            cur_node.right=self._remove(data,cur_node.right)
46        else:
47            if cur_node.left is None and cur_node.right is None:
48                print('Removing Leaf Node')
49                if cur_node==self.root:
50                    self.root=None
51                del cur_node
52                return None
53            if cur_node.left is None:
54                print('Removing None with Right Child')
55                if cur_node==self.root:
56                    self.root=cur_node.right
57                tempnode=cur_node.right
58                del cur_node
59                return tempnode
60            elif cur_node.right is None:
61                print('Removing None with Left Child')
62                if cur_node==self.root:
63                    self.root=cur_node.left
64                tempnode=cur_node.left
65                del cur_node
66                return tempnode
67            print('Removing Node with 2 Children')
68            tempnode=self.getpred(cur_node.left)
69            cur_node.data=tempnode.data
70            cur_node.left=self._remove(cur_node.data,cur_node.left)
71        return cur_node
72    def getpred(self,cur_node):
73        if cur_node.right!=None:
74            return self.getpred(cur_node.right)
75        return cur_node
76
77#INORDER TRAVERSAL
78
79    def inorder(self):
80        if self.root!=None:
81            self._inorder(self.root)
82    def _inorder(self,cur_node):
83        if cur_node!=None:
84            self._inorder(cur_node.left)
85            print(cur_node.data)
86            self._inorder(cur_node.right)
87
88#PREORDER TRAVERSAL
89
90    def preorder(self):
91        if self.root!=None:
92            self._preorder(self.root)
93    def _preorder(self,cur_node):
94        if cur_node!=None:
95            print(cur_node.data)
96            self._preorder(cur_node.left)
97            self._preorder(cur_node.right)
98
99#POSTORDER TRAVERSAL
100
101    def postorder(self):
102        if self.root!=None:
103            self._postorder(self.root)
104    def _postorder(self,cur_node):
105        if cur_node!=None:
106            self._postorder(cur_node.left)
107            self._postorder(cur_node.right)
108            print(cur_node.data)
109
110#MINIMUM VALUE
111
112    def minval(self):
113        if self.root!=None:
114            return self._minval(self.root)
115    def _minval(self,cur_node):
116        if cur_node.left!=None:
117            return self._minval(cur_node.left)
118        return cur_node.data
119
120#MAXIMUM VALUE
121
122    def maxval(self):
123        if self.root!=None:
124            return self._maxval(self.root)
125    def _maxval(self,cur_node):
126        if cur_node.right!=None:
127            return self._maxval(cur_node.right)
128        return cur_node.data
129
130tree=binarytree()
131
132tree.insert(100)
133tree.insert(90)					#			 100
134tree.insert(110)				#			/	\
135tree.insert(95)					#          90   110
136tree.insert(30)					#		  /  \
137								#		30    95 
138tree.remove(110)
139tree.remove(90)
140
141tree.inorder()
142#tree.preorder()
143#tree.postorder()
144
145print(tree.minval())
146print(tree.maxval())
Christen
16 Aug 2017
1Binary Search Tree at this link:
2  
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
queries leading to this page
binary search tree 28bst 29 implementation in pythonhow to make a binary search tree with an array pythonbinary tree pythombinary search tree python uses in gamessearch element in binary tree with pythonssbinary search tree definition pythoncode a binary tree inpythonhow to construct a adt binary search tree using a list of names search tree in pythonbinary tree in pythonssbinary tree array implementation pythonbinary tree in python geeksforgeekssimple binary tree pythonbst pythonbinary search tree implementation with pythonbinary tree search in data structure pythonpython binairy treesearch in binary tree pythonfind element in binary search tree in python implement bst pythonbinary tree in data structure pythonpython3 binary treebinary search tree python namedtuplehow to make a binary tree in pythonpyton binary treebinary tree class in pythonfinding a node in a binary search tree pythonbinary tree in python realpythonbinary search tree python librarysearch element in binary tree by pythonis there a built in binary search tree in python 3fc 2b 2b binary search treespython program to implement binary search tree python function to search value in a binary search treebuild a binary tree in pythonbinary search tree python with listis binary tree a binary search tree pythonbinary search tree iterator pythondefine binary tree in pythonbinary tree library pythonbinary tree with pythonthe binary tree pythonpython code for binary search treepython binary search tree librarycreation of binary search tree in cconstruct all possible binary search tree for keys 1 to n and display the post order traversal of each treebinary tree python arraybinary search tree inplementation in pythonbinary tree data structure implementation pythonsearching in binary search tree pythonsearch tree algorithm pythonbinary tree python librarycode for binary tree in pythoncode for binary search tree in pythontree search in pythonpython binary searchbinary search tree using pythonbinary tree code example python binary tree data structure in pythonbinary search tree in python search binary tree python codepython binary search tree infoconstruct binary tree pythonhow to get element in a binary tree pythonsearch element in binary tree with pythonsearch element in binary tree with pythonshow to use built in binary search tree pythonpython binary search treesbinary tree data structure pythoninsertion bst pythonhow values are inserter in a binary treepthon binary treeproblems related to binary search tree in pythonpython code for search in binary search treewhat is binary search tree in pythonsearch in a virtually complete binary tree pythondevelop a menu driven program to implement binary tree 2fbinary search tree to perform the following operations i 29insertion ii 29 traversing in different order 28depth first traversal 29 iii 29 search and display the node and its parent node iv 29 to find heightbst in pythonbinary search tree python usessearch in a tree pythonpython program for binary search treebinary tree 2fbinary search tree in pythonimplement binary search tree pyhtontree binary search tree in pythonpython binary tree codebinary tree class pythonbinary trees in pythonpython binary tree implementationtree search pythonbinary search tree implementation pythonbinary search tree 28bst 29 pythonpython binary search tree implementationpython program for binary searchsearch an element in binary tree with pythonsshow to use binary trees pythonbinary search tree python codeis this a binary search tree pythonbst insert recursive javasearch element in binary tree pythonhow to handle binary tree in pythonexample code binary tree in pythoninsert node bst pythonpsearch element in binary tree using pythonsearch tree in pythonbinary tree python examplepython binary search treebinary search tree data structure in chow to search a binary tree pythonimplement binary tree pythonbst insert cpphow to create binary search tree in pythonhow to print a binary tree in pythonpython how to print a binary search treebinary search tree python implementationis there a binary tree built in in pythonpython binary search tree using arraypython binary tree find elementpython tree searchcreate a binary search tree pythonenter value in bstbinary search tree c 2b 2b implementationbinary serach tree pyhtonbst tree in pythonbinary tree algorithm in pythonpython binary tree 3fpython binary tree methodsbinary search tree python searchbinsary search tree python 5cmake binary search tree pythonpython binary search tree sortbinary search tree search operation in pythonpython function to check if a list is a binary search treepython binary tree examplebinary search tree data structure in pythonhow to write a binary tree in pythonsearch element in binary tree in pythonbinary search tree algorithm in pythonis binary search tree pythondoes python have a binary search treebinary tree example pythonpython data structures binary treepython binarytreebinasy search tree in pythonpython program for inserting data in bst and also add functions for preorder 2cpostorder and inorder traversalbinary search tree program in pythonpython program to check whether a binary tree is a binary search tree pythonbinary tree using pythonbinary tree in pythonbinary tree in data structure program pythonsearch binary tree pythonhow to code a binary tree in pythonpython binary search tree pythontree search in pythonpython sample binary search treesinsert into binary search treehow to do a binary tree in pythonbinery tree pybinary tree pythonbinary search tree c 2b 2bbinary tree made using functions in pythoncounting the elements in a binary search tree pythonbinary tree python implementationbuilding a binary tree pythoncode a binary tree in pythonpython binary tree libraryinsert node tree pythonbinary tree implementation in pythonhow to create a binary search tree in pythonhow to implement binary tree in python without oopbinary tree search pythonbst insertion pythoncreating a binary tree in pythontree ds in pythoinbinary tree search algorithm pythonbinary tree in python moduleimplementation of binary tree in pythonbinary search tree isnert ptyhonbinary tree implementation pythonpython search method binary treebinary trees pythonpython bst codingimplementing binary search tree in pythonimplement a binary search tree in pythoncreate binary search tree pythonprinting binary search tree in pythonbinary search tree insertion pythonequation to binary tree code in pythonbinary tree in pytonbinary tree in pythonsprogram construct all possible binary search tree for keys 1 to n and display the post order traversal of each treebst tree code pythonbinary search tree array pythonbinary searach tree pythonpython binary search tree using array implementationbinary tree in pythinbinary search tree implementation in pythonbinary search tree in python programzbinary search tree search function pythonwhat is a binary tree in pythonbinary tree insertion and deletiondesign a binary search treebst data structure pythonoptimal binary search tree in pythonpython binary tree search for nodepython binary tree searcharray binary tree pythonsearch for an element in bstsymmetric binary tree pythonbinary 5ctree in pythonsearch an elements in binary tree with pythonsshow to create binary search trees in pythonhow elements are inserted in bstbinary search tree pythonis binary tree in python binary search trees in pythoncreate a binary tree in pythonsearch elements in binary tree with pythonssbst classhow to make binary tree in pythonpython binary serch treepython binary treebinary search tree tree pythonsearch the value in binary tree pythonpython code that implements a binary tree data structurebinary search tree class pythonbinary treees in pythonpython search tree implementationfull binary search tree implementation pythonbinary search tree in pythonfind value in binary search tree pythonhow to code a binary tree data structure in pythonbinary search tree search pythonbinary tree module in pythonprogram for data insert into bst into pythondeclare binary tree in pythonbs tree implementationprint binary search tree pythonhow to construct a binary tree in pythonsearch in bst in cinsert bstbst tree pythonsearch a node in binary tree in pythonpython bst binary search tree array implementation pythonsearch operation in binary search tree pythonbinary search algorithm pythonpython code for searching node from bstbinary tree creation in pythonwhat is binary tree in pythonjava bsthow to create a binary tree in pythoninsertion in bst pybinary search tree using python 3binary search tree in python