From eb0d7868dc6c03efae1ee39959163815916037d2 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 27 Mar 2025 21:39:47 -0400 Subject: [PATCH] time complexity for all 3 --- lectures/21_trees_IV/README.md | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/lectures/21_trees_IV/README.md b/lectures/21_trees_IV/README.md index d9ee5b7..7fb4b81 100644 --- a/lectures/21_trees_IV/README.md +++ b/lectures/21_trees_IV/README.md @@ -1,21 +1,12 @@ - # Lecture 21 --- Trees, Part IV -Review from Lecture 20 -- Breadth-first and depth-first tree search -- Increement/decrement operator -- Tree height, longest-shortest paths, breadth-first search -- Last piece of ds_set: removing an item, erase -- Erase with parent pointers, increment operation on iterators -- Limitations of our ds set implementatioN - ## Today’s Lecture - Morris Traversal ## 21.1 Morris Traversal -Morris Traversal is a tree traversal algorithm that allows inorder (and also preorder) traversal of a binary tree without using recursion or a stack, achieving O(1) space complexity. It modifies the tree temporarily but restores it afterward. +Morris Traversal is a tree traversal algorithm that allows in-order, pre-order, and post-order traversal of a binary tree without using recursion or a stack/queue, achieving O(1) space complexity. It modifies the tree temporarily but restores it afterward. Instead of using extra memory (like recursion stack or an explicit stack), Morris Traversal utilizes threaded binary trees by: @@ -80,11 +71,6 @@ $ ./a.out Inorder Traversal using Morris Traversal: 4 2 6 5 7 1 3 9 8 ``` -### Time and Space Complexity - -- Time Complexity: O(N) (each node is visited at most twice) - -- Space Complexity: O(1) (no extra space used except for modifying pointers) ## 21.3 Morris Traversal - Pre Order @@ -130,12 +116,6 @@ Preorder Traversal using Morris Traversal: 1 2 4 5 6 7 3 8 9 ``` -### Time and Space Complexity - -- Time Complexity: O(N) (each node is visited at most twice) - -- Space Complexity: O(1) (no extra space used except for modifying pointers) - ## 21.4 Morris Traversal - Post Order Post order is different, and we need to write some helper functions here. @@ -207,3 +187,9 @@ $ ./a.out Postorder Traversal using Morris Traversal: 4 6 7 5 2 9 8 3 1 ``` + +## Time and Space Complexity in Morris Traversal (in-order, pre-order, post-order) + +- Time Complexity: O(N) (each node is visited at most twice) + +- Space Complexity: O(1) (no extra space used except for modifying pointers)