Skip to content

Create binary_search_tree.py #845

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

Closed
wants to merge 1 commit into from
Closed
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
60 changes: 60 additions & 0 deletions binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

def insert(root, data):
if root is None:
return Node(data)
if data < root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)
return root


def inorder(root):
if root is None:
return
if root.left is not None:
inorder(root.left)
print(root.data)
if root.right is not None:
inorder(root.right)
return

def minimum(root):
curr = root
while curr.left != None:
curr = curr.left
return curr

def delete(root, data):
if data < root.data:
root.left = delete(root.left, data)
elif data > root.data:
root.right = delete(root.right, data)
else:
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
temp = minimum(root.right)
root.data = temp.data
root.right = delete(root.right,temp.data)
return root

def main():
root = Node(5)
insert(root,4)
insert(root,3)
insert(root,6)
inorder(root)

if __name__ =='__main__':
main()