Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Create binary-search-tree.py #118

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
43 changes: 43 additions & 0 deletions binary-search-tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#funtction to search a given key in BST
def search(root,key):
if root is None or root.val == key:
return root
if root.val < key:
return search(root.right,key)
return search(root.left,key)


class Node:
def __init__(self,key):
self.right = None
self.left = None
self.val = key
def insert(root,key):
if root is None:
return Node(key)
else:
if root.val == key:
return root
elif root.val < key:
root.right = insert(root.right,key)
else:
root.right = insert(root.left,key)
return root

def inorder(root):
if root:
inorder(root.lefft)
print(root.val)
inorder(root.right)


x = Node(50)
x = insert(x,40)
x = insert(x,30)
x = insert(x,20)
x = insert(x,80)
x = insert(x,60)
x = insert(x,90)