Skip to content

binary search tree operations in python #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions Structures/BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
class BinaryNode:
def __init__(self ):
self.data = None
self.left = None
self.right = None
self.parent = None


class Binarytree:
def __init__(self,data=None):
self.root = BinaryNode()
self.root.data = data
def search(self,k):
return self.searchtree(self.root,k)
def searchtree(self,r=None,k=None):
if r is None:
return False
if k == r.data:
return True
if k > r.data:
return self.searchtree(r.right,k)
if k < r.data:
return self.searchtree(r.left,k)

def treemin(self,t):
#t = self.root
while (t.left != None):
t = t.left
return t.data
def treemax1(self,r):
#r = self.root
while (r.right != None):
r = r.right
return r
def treemax(self,r):
#r = self.root
while (r.right != None):
r = r.right
return r.data

def treeSuc(self,x):
# x = self.root
if x.right != None:
return self.treemin(x.right)
y = x.parent
while x != y.left and y != None:
x = y
y = y.parent
return y.data
def treePre1(self,r):
#r = self.root
if r.left != None:
return self.treemax1(r.left)
y = r.parent
while y != None and r != y.right:
r = y
y = y.parent
return y
def treePre(self,r):
#r = self.root
if r.left != None:
return self.treemax(r.left)
y = r.parent
while y != None and r != y.right:
r = y
y = y.parent
return y.data

def Insert(self, v):
t = BinaryNode()
t.data=v
t.left = t.right = t.parent = None
x = self.root
y = self.root.parent
while (x != None):
y = x
if v <= x.data:
x = x.left
else:
x = x.right
t.parent = y
if v < y.data:
y.left = t
else:
y.right = t
def delete(self,x):
if x.right is None and x.left is None and x.parent is not None:
if x.parent.data>x.data:
x.parent.left=None
else:
x.parent.right=None
#return
elif (x.right is None and x.left is not None):
if x.parent.data>x.data:
x.parent.left=x.right
else:
x.parent.right=x.right
#return
elif x.left is None and x.right is not None and x.parent is not None:
if x.parent.data>x.data:
x.parent.left=x.left
else:
x.parent.right=x.left
#return
elif x.left is not None and x.right is not None:
y=self.treePre1(x)
x.data=y.data
return self.delete(y)
def printpre(v):
if v is None:
return
print(v.data)
printpre(v.left)
printpre(v.right)


b = Binarytree(10)
b.Insert(5)
b.Insert(14)
b.Insert(3)
b.Insert(8)
b.Insert(11)
b.Insert(6)
b.Insert(13)
b.Insert(7)
print(b.search(3))
printpre(b.root)
print("the predessor")
print(b.treePre(b.root))
print("the successor is ")
print(b.treeSuc(b.root))
print("the maximum in tree is ")
print(b.treemax(b.root))
print("the minimum in tree is ")
print(b.treemin(b.root))
b.delete(b.root)
print("after deleting the node")
printpre(b.root)