aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-07-26 02:13:55 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-07-26 02:54:02 +0100
commit6dfd820772b0de4d85912d4770a5bd6b2caaf619 (patch)
tree693baf47b72d0b8d7b6409175b309c81edb5aec3
parent5cbdf7a045c18877b8ae83a7f105e1cf32866a79 (diff)
downloadcw_tree-6dfd820772b0de4d85912d4770a5bd6b2caaf619.tar.gz
cw_tree-6dfd820772b0de4d85912d4770a5bd6b2caaf619.tar.bz2
cw_tree-6dfd820772b0de4d85912d4770a5bd6b2caaf619.zip
Added to_string for Nodes
Recursive implementation with proper indenting!
-rw-r--r--src/main.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 91accad..da63953 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -101,6 +101,31 @@ word_t alloc_node(Node n)
return nodes.size() - 1;
}
+void indent_depth(int depth, std::stringstream &ss)
+{
+ for (int i = 0; i < depth; ++i)
+ ss << "\t";
+}
+
+std::string to_string(Node n, int depth = 1)
+{
+ std::stringstream ss;
+ ss << "(" << to_string(n.value) << "\n";
+ indent_depth(depth, ss);
+ if (n.left == -1)
+ ss << "()";
+ else
+ ss << to_string(nodes[n.left], depth + 1);
+ ss << "\n";
+ indent_depth(depth, ss);
+ if (n.right == -1)
+ ss << "()";
+ else
+ ss << to_string(nodes[n.right], depth + 1);
+ ss << ")";
+ return ss.str();
+}
+
std::queue<word_t> to_iterate;
void iterate(void)