python code for binary search tree

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

showing results for - "python code for binary search tree"
Ivanna
17 Jul 2016
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())
queries leading to this page
binary search tree 28bst 29 pythonsearch operation in binary search tree pythonbinary search tree implementation with pythonbinary search algorithm pythonpython code for searching node from bstenter value in bstsearch binary tree pythonpython binary serch treeinsert node tree pythonsearch element in binary tree using pythonhow to use built in binary search tree pythonimplementing binary search tree in pythonhow elements are inserted in bstbst insert recursive javabst data structure pythonsearch a node in binary tree in pythonbinary tree python implementationbinary search tree python implementationhow values are inserter in a binary treebinary search tree search operation in pythonsearch in bst in cprogram construct all possible binary search tree for keys 1 to n and display the post order traversal of each treebinary search tree in python programzcreation of binary search tree in cfull binary search tree implementation pythonbst in pythonpython search method binary treebinary search tree definition pythonbinary search tree python codeinsert bstbinary search tree c 2b 2bmake binary search tree pythonbinary search tree data structure in cbinary search tree implementation pythondesign a binary search treepython code for search in binary search treepython program to implement binary search tree python how to print a binary search treehow to search a binary tree pythonbinary search tree pythonbinary tree search pythonbinary tree 2fbinary search tree in pythonimplement bst pythonbinary tree search in data structure pythonjava bstpython program for inserting data in bst and also add functions for preorder 2cpostorder and inorder traversalbinary tree insertion and deletionpython code for binary search treeinsert node bst pythonpprogram for data insert into bst into pythonbinary search tree python searchconstruct all possible binary search tree for keys 1 to n and display the post order traversal of each treepython binary search treesbinary search tree python usesis there a built in binary search tree in python 3fpython program for binary searchsearch an elements in binary tree with pythonssfind element in binary search tree in python bst insertion pythonpython binary search tree infobst pythonbst insert cppsearch element in binary tree in pythoninsert into binary search treebinary search tree using pythoninsertion in bst pycreate a binary search tree pythonsearch element in binary tree pythonsearch in a virtually complete binary tree pythonbinary search tree array pythonc 2b 2b binary search treesoptimal binary search tree in pythonpython binary tree search for nodehow to construct a adt binary search tree using a list of namesbinary search tree inplementation in pythonbinary search tree implementation in pythonbinary search tree insertion pythonbinsary search tree python 5cbs tree implementationpython sample binary search treesbinary search tree program in pythonbinary search tree using python 3python binary search tree librarybst classinsertion bst pythonpython binary search treesearch for an element in bstbinary search tree c 2b 2b implementation search tree in pythonbinary search tree in pythonbinary search tree search pythonpython binary searchdevelop 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 heightbinary search tree python uses in gamessearch element in binary tree with pythonsshow to create binary search trees in pythonpython bst codingbinary search tree array implementation pythonproblems related to binary search tree in pythonpython bst binary search tree search function pythonpython code for binary search tree