Skip to content
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

Create BST #119

Open
wants to merge 1 commit into
base: main
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
54 changes: 54 additions & 0 deletions Programs/BST
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class GFG :
@staticmethod
def main( args) :
tree = BST()
tree.insert(30)
tree.insert(50)
tree.insert(15)
tree.insert(20)
tree.insert(10)
tree.insert(40)
tree.insert(60)
tree.inorder()
class Node :
left = None
val = 0
right = None
def __init__(self, val) :
self.val = val
class BST :
root = None
def insert(self, key) :
node = Node(key)
if (self.root == None) :
self.root = node
return
prev = None
temp = self.root
while (temp != None) :
if (temp.val > key) :
prev = temp
temp = temp.left
elif(temp.val < key) :
prev = temp
temp = temp.right
if (prev.val > key) :
prev.left = node
else :
prev.right = node
def inorder(self) :
temp = self.root
stack = []
while (temp != None or not (len(stack) == 0)) :
if (temp != None) :
stack.append(temp)
temp = temp.left
else :
temp = stack.pop()
print(str(temp.val) + " ", end ="")
temp = temp.right

if __name__=="__main__":
GFG.main([])

# This code is contributed by rastogik346.