File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ # Copyright (C) Deepali Srivastava - All Rights Reserved
2+ # This code is part of DSA course available on CourseGalaxy.com
3+
4+ class Node :
5+ def __init__ (self ,value ):
6+ self .info = value
7+ self .lchild = None
8+ self .rchild = None
9+
10+ class BinarySearchTree :
11+ def __init__ (self ):
12+ self .root = None
13+
14+ def is_empty (self ):
15+ return self .root == None
16+
17+ def insert (self ,x ):
18+ self .root = self ._insert (self .root , x )
19+
20+ def _insert (self ,p , x ):
21+ if p is None :
22+ p = Node (x )
23+ elif x < p .info :
24+ p .lchild = self ._insert (p .lchild , x )
25+ else :
26+ p .rchild = self ._insert (p .rchild , x )
27+ return p
28+
29+ def inorder (self ):
30+ self ._inorder (self .root )
31+ print ()
32+
33+ def _inorder (self , p ):
34+ if p is None :
35+ return
36+ self ._inorder (p .lchild )
37+ print (p .info , " " )
38+ self ._inorder (p .rchild )
39+
40+
41+ ####################################
42+ n = int (input ("Enter the number of elements to be sorted : " ))
43+
44+ tree = BinarySearchTree ()
45+ for i in range (n ):
46+ x = int (input ("Enter element : " ))
47+ tree .insert (x )
48+
49+ tree .inorder ()
50+
51+ ####################################
52+
You can’t perform that action at this time.
0 commit comments