English 中文(简体)
校对 2. 仅具有固定额外空间的 Bin树
原标题:Iterative Threaded Binary Tree Traversing with only constant extra space

如何在O(n)上不停地转播O(n)的双read树,而不使用一ack(允许在温变数方面使用固定的额外空间,因此,我们可以在树中的每 no子上添加旗帜)。 我花了很多时间思考这个问题,但我看不到什么东西可以做unless<>em>。 我们正在向拥有树木数据的记忆点进行渗透。 让我们说,我们正在利用多种阵列代表来执行点人,然后,我们可以tra笑O(n)的树,谁会想到什么东西?

<><>>>>> http://strong>not homework, only to Save theenergy of some key板 strokes to written comments about homework!

最佳回答

Let s say that we have the following threaded tree representation in C:

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;

然后,在<条码>O(1)上检索。

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
", 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
    }
}

警告:我没有这样做。

问题回答

这是在 Java撰写的法典:

public void inOrder() {
    Node<T> curr = root;
    boolean visited = false; //I haven t come across the node from which I came

    while (curr != null) {
        if (!visited && curr.left != null) { //Go to leftmost node
            curr = curr.left;
        } else {
            System.out.println(curr.head + " ");
            if (curr.right != null) { //I prioritize having childs than "threaded sons"
                curr = curr.right;
                visited = false;
            } else {
                curr = curr.rightThreaded;
                visited = true; //Means that I will come back to a node I ve already looped, but now i ll print it, except if i m at the last node
            }
        }
    }
}

纽埃语是一种内装饰语 树木:

private static class Node<T> {
    private T head;
    private Node<T> left;
    private Node<T> right;
    private Node<T> leftThreaded;
    private Node<T> rightThreaded;

    public Node(T head, Node<T> leftThreaded, Node<T> rightThreaded) {
        this.head = head;
        this.leftThreaded = leftThreaded;
        this.rightThreaded = rightThreaded;
    }
}




相关问题
The Fastest DataStructure to Filter with in C#

Currently we are filtering and sorting data with a datatable. /// <summary> /// Filters the data table and returns a new data table with only the filtered rows. /// </summary>...

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Java large datastructure for storing a matrix

I need to store a 2d matrix containing zip codes and the distance in km between each one of them. My client has an application that calculates the distances which are then stored in an Excel file. ...

Holding onto items after a postback

I have an ASP.NET web application and I want to be able to take items from a master list and store them temporarliy into one of four other lists. The other lists need to survive post backs so that ...

negative number in the stack

I am a new student in the compilers world ^_^ and I want to know is legal represent negative number in the stack. For example: infix: 1-5=-4 postfix: 15- The statements are: push(1) push(5) x=...

What type of struct/container would you use in this instance?

I am trying to figure out what type of structure or container I should use for a quick project. I need to have an unknown number of sets that will be entered from the GUI (each one will have a name, ...