

{"id":143502,"date":"2024-09-30T13:47:07","date_gmt":"2024-09-30T08:17:07","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=143502"},"modified":"2024-09-30T13:47:07","modified_gmt":"2024-09-30T08:17:07","slug":"preorder-and-postorder-method-in-bst-in-dsa-python","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/","title":{"rendered":"Preorder and Postorder Method in BST in DSA Python"},"content":{"rendered":"<h3>Program 1<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Implementation of Binary Search Tree\r\nimport os\r\nclass Node:\r\n    def __init__(self):\r\n        self.ladd=None\r\n        self.data=None\r\n        self.radd=None\r\n\r\nclass Tree:\r\n    def __init__(self):\r\n        self.root=None\r\n        #Create Method\r\n    def createTree(self,r,new1):\r\n        if(new1.data&lt;r.data):\r\n            if(r.ladd==None):\r\n                r.ladd=new1\r\n            else:\r\n                self.createTree(r.ladd,new1)  # Recursion\r\n        if(new1.data&gt;r.data):\r\n            if(r.radd==None):\r\n                r.radd=new1\r\n            else:\r\n                self.createTree(r.radd,new1)  # Recursion    \r\n\r\n        if(new1.data==r.data):\r\n            print(\"duplicate element not allowed .\")        \r\n\r\n     #Inorder Method           \r\n    def inorder(self,p):\r\n        if(p!=None):\r\n            self.inorder(p.ladd)   # left\r\n            print(p.data,end=\"  \")  # Node\r\n            self.inorder(p.radd)  #right\r\n    #Preorder Method           \r\n    def preorder(self,p):\r\n          if(p!=None):\r\n              print(p.data,end=\"  \")  # Node\r\n              self.preorder(p.ladd)  # left\r\n              self.preorder(p.radd)  # right\r\n              \r\n   #Postrder Method           \r\n    def postorder(self,p):\r\n        if(p!=None):\r\n            self.postorder(p.ladd)  # left\r\n            self.postorder(p.radd)  # right \r\n            print(p.data,end=\"  \")  # Node\r\n\r\n\r\n\r\n#Main Menu\r\nT=Tree()\r\nos.system('cls')\r\nwhile(1):\r\n    print(\"\\n-----------------Tree Menu------------------\")\r\n    print(\"                 1.Create Tree \")\r\n    print(\"                 2.Inorder \")\r\n    print(\"                 3.Preorder \")\r\n    print(\"                 4.Postorder \")\r\n    print(\"                 5.Exit \")\r\n    print(\"----------------------------------------------\")\r\n    choice=int(input(\"Enter your choice\"))\r\n    \r\n    if(choice==1):\r\n       ch=\"y\"\r\n       while(ch==\"Y\" or ch==\"y\"):\r\n           n=int(input(\"Enter an element\"))\r\n           new1=Node()\r\n           new1.ladd=None\r\n           new1.data=n\r\n           new1.radd=None\r\n           if(T.root==None):\r\n              T.root=new1\r\n           else:\r\n              T.createTree(T.root,new1)\r\n           ch=input(\"Want to continue: \")\r\n    elif(choice==2):\r\n        print(\"Inorder Elements: \")\r\n        T.inorder(T.root)    \r\n    elif(choice==3):\r\n        print(\"Preorder Elements: \")\r\n        T.preorder(T.root)        \r\n    elif(choice==4):\r\n        print(\"Postorder Elements: \")\r\n        T.postorder(T.root)\r\n    else:\r\n        break<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program 1 # Implementation of Binary Search Tree import os class Node: def __init__(self): self.ladd=None self.data=None self.radd=None class Tree: def __init__(self): self.root=None #Create Method def createTree(self,r,new1): if(new1.data&lt;r.data): if(r.ladd==None): r.ladd=new1 else: self.createTree(r.ladd,new1) # Recursion if(new1.data&gt;r.data):&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32847],"tags":[1992,32853,32922,32923,33172,32848,33171,33170],"class_list":["post-143502","post","type-post","status-publish","format-standard","hentry","category-dsa-python-tutorials","tag-binary-search-tree","tag-dsa-python","tag-dsa-python-practical","tag-dsa-python-program","tag-dsa-python-program-on-preorder-and-postorder-method-in-bst","tag-dsa-using-python","tag-preorder-and-postorder-method-in-binary-search-tree","tag-preorder-and-postorder-method-in-bst-in-dsa-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Preorder and Postorder Method in BST in DSA Python - DataFlair<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Preorder and Postorder Method in BST in DSA Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Program 1 # Implementation of Binary Search Tree import os class Node: def __init__(self): self.ladd=None self.data=None self.radd=None class Tree: def __init__(self): self.root=None #Create Method def createTree(self,r,new1): if(new1.data&lt;r.data): if(r.ladd==None): r.ladd=new1 else: self.createTree(r.ladd,new1) # Recursion if(new1.data&gt;r.data):&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-30T08:17:07+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Preorder and Postorder Method in BST in DSA Python - DataFlair","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/","og_locale":"en_US","og_type":"article","og_title":"Preorder and Postorder Method in BST in DSA Python - DataFlair","og_description":"Program 1 # Implementation of Binary Search Tree import os class Node: def __init__(self): self.ladd=None self.data=None self.radd=None class Tree: def __init__(self): self.root=None #Create Method def createTree(self,r,new1): if(new1.data&lt;r.data): if(r.ladd==None): r.ladd=new1 else: self.createTree(r.ladd,new1) # Recursion if(new1.data&gt;r.data):&#46;&#46;&#46;","og_url":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-09-30T08:17:07+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Preorder and Postorder Method in BST in DSA Python","datePublished":"2024-09-30T08:17:07+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/"},"wordCount":11,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["Binary Search Tree","dsa python","dsa python practical","dsa python program","dsa python program on preorder and postorder method in bst","dsa using python","preorder and postorder method in binary search tree","preorder and postorder method in bst in dsa python"],"articleSection":["DSA using Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/","url":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/","name":"Preorder and Postorder Method in BST in DSA Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2024-09-30T08:17:07+00:00","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/preorder-and-postorder-method-in-bst-in-dsa-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"DSA using Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/dsa-python-tutorials\/"},{"@type":"ListItem","position":3,"name":"Preorder and Postorder Method in BST in DSA Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/143502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=143502"}],"version-history":[{"count":1,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/143502\/revisions"}],"predecessor-version":[{"id":143503,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/143502\/revisions\/143503"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=143502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=143502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=143502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}