@@ -26,7 +26,7 @@ def rinsert(self,root,data):
26
26
27
27
def search (self ,data ):
28
28
if not self .is_empty ():
29
- return self .rsearch (self , self .root ,data )
29
+ return self .rsearch (self .root ,data )
30
30
else :
31
31
raise IndexError ("Binary search tree is empty" )
32
32
@@ -39,6 +39,55 @@ def rsearch(self,root,data):
39
39
return self .rsearch (root .right ,data )
40
40
41
41
def inroder (self ):
42
+ result = []
43
+ self .rinorder (self .root ,result )
44
+ return result
45
+
46
+ def rinorder (self ,root ,result ):
47
+ if root :
48
+ self .rinorder (root .left ,result )
49
+ result .append (root .item )
50
+ self .rinorder (root .right ,result )
51
+
52
+ def preroder (self ):
53
+ result = []
54
+ self .rpreorder (self .root ,result )
55
+ return result
56
+
57
+ def rpreorder (self ,root ,result ):
58
+ if root :
59
+ result .append (root .item )
60
+ self .rpreorder (root .left ,result )
61
+ self .rpreorder (root .right ,result )
62
+
63
+ def postroder (self ):
64
+ result = []
65
+ self .rpostroder (self .root ,result )
66
+ return result
67
+
68
+
69
+ def rpostroder (self ,root ,result ):
70
+ if root :
71
+ self .rpostroder (root .left ,result )
72
+ self .rpostroder (root .right ,result )
73
+ result .append (root .item )
74
+
75
+
76
+ bst = BST ()
77
+ print (bst .is_empty ())
78
+ bst .insert (50 )
79
+ bst .insert (30 )
80
+ bst .insert (40 )
81
+ bst .insert (10 )
82
+ bst .insert (80 )
83
+ bst .insert (70 )
84
+ bst .insert (90 )
85
+ print ("inorder traversal of binary search tree: " ,bst .inroder ())
86
+ print ("preorder traversal of binary search tree: " ,bst .preroder ())
87
+ print ("postorder traversal of binary search tree: " ,bst .postroder ())
88
+ print (bst .is_empty ())
89
+ print (bst .search (90 ))
90
+
42
91
43
92
44
93
0 commit comments