のトラバースツリーの各ノードに訪問フラグを追加しないでください)。私はそれについて考えて楽しい時間を過ごしましたが、のように私には見えません。木のデータを持つメモリの場所を横切る場合を除きます。私たちがポインタを実装するために複数の配列表現を使用しているとしたら、O(n)でツリーをたどることができます。反復スレッドバイナリツリーは単なる一時変数のための一定の余分なスペースを使用することができ、私たちはできる(スタックを使用せずに<em>スレッドバイナリツリー</em>非再帰的<em>O(n)の</em>中を横断する方法だけで一定の余分なスペースと
注これはちょうど宿題についてのコメントを書くために、いくつかのキーボードストロークのエネルギーを節約するために、ない宿題です!
void inorder(threaded_binary_tree *node)
{
threaded_binary_tree *prev = NULL;
// Ignore empty trees
if (node == NULL)
return;
// First, go to the leftmost leaf of the tree
while (node->left != NULL)
node = node->left;
while (node != NULL) {
// Nodes are visited along going upward in the tree
printf("%d\n", node->value);
prev = node;
node = node->right;
if (prev->right_is_child) {
// We're descending into tree
if (node != NULL) {
// Again, go to the leftmost leaf in this subtree
while (node->left != NULL)
node = node->left;
}
}
// else, we're climbing up in the tree
}
}
警告:私は実行していない。このように見えるかもしれO(1)
メモリにそれを通過、その後
typedef struct threaded_binary_tree {
int value;
// Flag that tells whether right points to a right child or to a next
// node in inorder.
bool right_is_child;
struct threaded_binary_tree *left, *right;
} threaded_binary_tree;
:
あなたはhttp://en.wikipedia.org([ウィキペディア]を見てきました/ wiki/Threaded_binary_tree#Non_recursive_Inorder_traversal_for_a_Threaded_Binary_Tree)? – svick
@svickアルゴリズムの2番目のステップは_Step-2です。左の子ノードを訪問先ノードのリストに置き、それを考慮して現在のノードにします。ステップ6.に進みます_私は余分な余裕を持ってそれを行う必要があるという質問に言及しました_ –
私の答えはあなたの質問に関連していますか?もしそうなら、それを1とマークしてください。もしそうでなければ、理由を教えてください。私はそれを洗練しようとします。 – adaszko