Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can achieve this if nodes have pointers to their parents. When you walk back up the tree (using the parent pointers) you also pass the node you're coming from. If the node you're coming from is the left child of the node you're now at, then you traverse the right child. Otherwise you walk back up to it's parent.</p> <p>EDIT in response to the edit in the question: If you want to iterate through the whole tree, then no this is not possible. In order to climb back up the tree you need to know where to go. However, if you just want to iterate through a <em>single</em> path down the tree then this can be achieved in O(1) additional space. Just iterate down the tree using a while loop, keeping a single pointer to the current node. Continue down the tree until you either find the node you want or hit a leaf node.</p> <p>EDIT: Here's code for the first algorithm (check the iterate_constant_space() function and compare to the results of the standard iterate() function):</p> <pre><code>#include &lt;cstdio&gt; #include &lt;string&gt; using namespace std; /* Implementation of a binary search tree. Nodes are ordered by key, but also * store some data. */ struct BinarySearchTree { int key; // they key by which nodes are ordered string data; // the data stored in nodes BinarySearchTree *parent, *left, *right; // parent, left and right subtrees /* Initialise the root */ BinarySearchTree(int k, string d, BinarySearchTree *p = NULL) : key(k), data(d), parent(p), left(NULL), right(NULL) {}; /* Insert some data */ void insert(int k, string d); /* Searches for a node with the given key. Returns the corresponding data * if found, otherwise returns None.""" */ string search(int k); void iterate(); void iterate_constant_space(); void visit(); }; void BinarySearchTree::insert(int k, string d) { if (k &lt;= key) { // Insert into left subtree if (left == NULL) // Left subtree doesn't exist yet, create it left = new BinarySearchTree(k, d, this); else // Left subtree exists, insert into it left-&gt;insert(k, d); } else { // Insert into right subtree, similar to above if (right == NULL) right = new BinarySearchTree(k, d, this); else right-&gt;insert(k, d); } } string BinarySearchTree::search(int k) { if (k == key) // Key is in this node return data; else if (k &lt; key &amp;&amp; left) // Key would be in left subtree, which exists return left-&gt;search(k); // Recursive search else if (k &gt; key &amp;&amp; right) return right-&gt;search(k); return "NULL"; } void BinarySearchTree::visit() { printf("Visiting node %d storing data %s\n", key, data.c_str()); } void BinarySearchTree::iterate() { visit(); if (left) left-&gt;iterate(); if (right) right-&gt;iterate(); } void BinarySearchTree::iterate_constant_space() { BinarySearchTree *current = this, *from = NULL; current-&gt;visit(); while (current != this || from == NULL) { while (current-&gt;left) { current = current-&gt;left; current-&gt;visit(); } if (current-&gt;right) { current = current-&gt;right; current-&gt;visit(); continue; } from = current; current = current-&gt;parent; if (from == current-&gt;left) { current = current-&gt;right; current-&gt;visit(); } else { while (from != current-&gt;left &amp;&amp; current != this) { from = current; current = current-&gt;parent; } if (current == this &amp;&amp; from == current-&gt;left &amp;&amp; current-&gt;right) { current = current-&gt;right; current-&gt;visit(); } } } } int main() { BinarySearchTree tree(5, "five"); tree.insert(7, "seven"); tree.insert(9, "nine"); tree.insert(1, "one"); tree.insert(2, "two"); printf("%s\n", tree.search(3).c_str()); printf("%s\n", tree.search(1).c_str()); printf("%s\n", tree.search(9).c_str()); // Duplicate keys produce unexpected results tree.insert(7, "second seven"); printf("%s\n", tree.search(7).c_str()); printf("Normal iteration:\n"); tree.iterate(); printf("Constant space iteration:\n"); tree.iterate_constant_space(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload