Skip to content

Commit 7d10ee9

Browse files
committed
DFS -> PostOrder using recursion
1 parent 8997e6e commit 7d10ee9

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

.ipynb_checkpoints/Searching-checkpoint.ipynb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
},
128128
{
129129
"cell_type": "code",
130-
"execution_count": 48,
130+
"execution_count": 54,
131131
"metadata": {},
132132
"outputs": [
133133
{
@@ -141,7 +141,9 @@
141141
"DFS Inorder uisng recursion -> LVR\n",
142142
"[1, 4, 6, 9, 15, 20, 170]\n",
143143
"DES Preorder uisng recursion -> VLR\n",
144-
"[9, 4, 1, 6, 20, 15, 170]\n"
144+
"[9, 4, 1, 6, 20, 15, 170]\n",
145+
"DES Postorder uisng recursion -> LRV\n",
146+
"[1, 6, 4, 15, 170, 20, 9]\n"
145147
]
146148
}
147149
],
@@ -228,6 +230,9 @@
228230
" def DFSPreorder(self):\n",
229231
" \"\"\"DES Preorder uisng recursion -> VLR\"\"\"\n",
230232
" return traversePreOrder(self.root,[])\n",
233+
" def DFSPostorder(self):\n",
234+
" \"\"\"DES Postorder uisng recursion -> LRV\"\"\"\n",
235+
" return traversePostOrder(self.root,[])\n",
231236
"\n",
232237
" \n",
233238
"def traverseInOrder(node,InLst):\n",
@@ -251,6 +256,18 @@
251256
" if node.right:\n",
252257
" traversePreOrder(node.right,PreLst)\n",
253258
" return PreLst\n",
259+
"\n",
260+
"def traversePostOrder(node,PostLst):\n",
261+
" \"\"\"LRV\"\"\"\n",
262+
"# print(node.val)\n",
263+
"# print(PostLst)\n",
264+
" if node.left:\n",
265+
" traversePostOrder(node.left,PostLst)\n",
266+
" if node.right:\n",
267+
" traversePostOrder(node.right,PostLst)\n",
268+
" PostLst.append(node.val)\n",
269+
" \n",
270+
" return PostLst\n",
254271
" \n",
255272
" \n",
256273
"bst = BST()\n",
@@ -268,7 +285,9 @@
268285
"print(bst.DFSInorder.__doc__)\n",
269286
"print(bst.DFSInorder())\n",
270287
"print(bst.DFSPreorder.__doc__)\n",
271-
"print(bst.DFSPreorder())"
288+
"print(bst.DFSPreorder())\n",
289+
"print(bst.DFSPostorder.__doc__)\n",
290+
"print(bst.DFSPostorder())"
272291
]
273292
},
274293
{

Searching.ipynb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
},
128128
{
129129
"cell_type": "code",
130-
"execution_count": 48,
130+
"execution_count": 54,
131131
"metadata": {},
132132
"outputs": [
133133
{
@@ -141,7 +141,9 @@
141141
"DFS Inorder uisng recursion -> LVR\n",
142142
"[1, 4, 6, 9, 15, 20, 170]\n",
143143
"DES Preorder uisng recursion -> VLR\n",
144-
"[9, 4, 1, 6, 20, 15, 170]\n"
144+
"[9, 4, 1, 6, 20, 15, 170]\n",
145+
"DES Postorder uisng recursion -> LRV\n",
146+
"[1, 6, 4, 15, 170, 20, 9]\n"
145147
]
146148
}
147149
],
@@ -228,6 +230,9 @@
228230
" def DFSPreorder(self):\n",
229231
" \"\"\"DES Preorder uisng recursion -> VLR\"\"\"\n",
230232
" return traversePreOrder(self.root,[])\n",
233+
" def DFSPostorder(self):\n",
234+
" \"\"\"DES Postorder uisng recursion -> LRV\"\"\"\n",
235+
" return traversePostOrder(self.root,[])\n",
231236
"\n",
232237
" \n",
233238
"def traverseInOrder(node,InLst):\n",
@@ -251,6 +256,18 @@
251256
" if node.right:\n",
252257
" traversePreOrder(node.right,PreLst)\n",
253258
" return PreLst\n",
259+
"\n",
260+
"def traversePostOrder(node,PostLst):\n",
261+
" \"\"\"LRV\"\"\"\n",
262+
"# print(node.val)\n",
263+
"# print(PostLst)\n",
264+
" if node.left:\n",
265+
" traversePostOrder(node.left,PostLst)\n",
266+
" if node.right:\n",
267+
" traversePostOrder(node.right,PostLst)\n",
268+
" PostLst.append(node.val)\n",
269+
" \n",
270+
" return PostLst\n",
254271
" \n",
255272
" \n",
256273
"bst = BST()\n",
@@ -268,7 +285,9 @@
268285
"print(bst.DFSInorder.__doc__)\n",
269286
"print(bst.DFSInorder())\n",
270287
"print(bst.DFSPreorder.__doc__)\n",
271-
"print(bst.DFSPreorder())"
288+
"print(bst.DFSPreorder())\n",
289+
"print(bst.DFSPostorder.__doc__)\n",
290+
"print(bst.DFSPostorder())"
272291
]
273292
},
274293
{

0 commit comments

Comments
 (0)