Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:59:09+00:00 2026-05-27T04:59:09+00:00

I am working on assignment for school. It manly consists of a method that

  • 0

I am working on assignment for school. It manly consists of a method that takes as input a binary tree and returns a double threaded tree. Eg(if left child = null then left child will be connected with preceding inorder parent and if right child = null the it will link to its inorder succesor. Now I have an idea for the implementation…

I iterate recursively trough the original BINARY tree and store into an array the inorder traversal. Now, because my teachers implementation requires that threaded trees be a different class from binary. I must traverse again trough the binary tree and convert each node from binaryNode to threadedNode thus having at the end a “duplicate” of the initial BinaryTree but as Threadedtree type. After I do this I traverse again trough this threadedTree and whenever i see a null left or right child I refer to the inorder arraylist and find the threads.

Now as you might have noticed this is extremely inefficient, i am essentially traversing the tree 3 times. My professor has stated that this could be done recursively with only one traversal, essentially converting to threadedNode and finding the threads all at once. I have tried multiple ways but i can not find one that works. Does anyone have any kind of tip or some way i can implement it? Thanks

This is the method as specified by the instructor

public static <T> ThreadedNode<T> thread(BinaryNode<T> root)
{
   //threads a binary tree
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:59:10+00:00Added an answer on May 27, 2026 at 4:59 am

    The instructor is correct. One traversal is sufficient.

    Traverse the original binary tree, creating new ThreadedNodes as you walk this tree.

    public static <T> ThreadedNode<T> thread(BinaryNode<T> root) {
        // We'll be keeping track of the "previous" node as we go, so use
        // a recursive helper method.  At first, there is no previous.
        return threadHelper(root, null);
    }
    
    private static <T> ThreadedNode<T> threadHelper(BinaryNode<T> n, ThreadedNode<T> previous) {
    
        // Create a new threaded node from the current root.  Note that the threaded nodes
        // are actually created in "preorder".  Assume the ThreadedNode constructor sets
        // the left, right, threadLeft, and threadRight fields to null.
        ThreadedNode<T> t = new ThreadedNode<T>(n.getData());
    
        // First go down the left side, if necessary.
        if (n.getLeft() != null) {
            // If there is a left child we have to descend.  Note that as we go down the
            // left side the previous doesn't change, until we start "backing up".
            t.left = threadHelper(n.getLeft(), previous);
            previous = t.left;
        } else {
            // If there is no left child, connect our left thread to the previous.
            t.threadLeft = previous;
        }
    
        // Now before we go down the right side, see if the previous
        // node (it will be in the left subtree) needs to point here.
        if (previous != null && previous.right == null) {
            previous.threadRight = t;
        }
    
        if (n.getRight() != null) {
            // If there is a right child we can descend the right.  As we go down we
            // update previous to the current node.  We do this just by passing the current
            // node as the second parameter.
            t.right = threadHelper(n.getRight(), t);
        } else {
            // No right child, no worries.  We'll hook up our thread-right pointer
            // later.
        }
        return t;
    }
    

    Consider the tree (A (B (D) ()) C). The first node you hit in an inorder traversal is D. There is no previous node. So save D as previous. Then the next node you hit is B. The previous node was D, which had no right child, so add a threaded right pointer from D to B. Then set previous to B and continue. Next you hit A. B had no right child, so add a threaded right link from B to A. A has a right child so continue, setting previous to A. The next node is C. C has no left child, so add a threaded left link from C to the current value of previous, which is A.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a school assignment that requires me to be able to
I am working on a school assignment that requires operator overloading. I am having
I've been working on a school assignment that makes pretty heavy use of vectors,
I have this school assignment that I've been working on and am totally stumped
I'm working on an assignment that is telling me to assume that I have
I'm currently working through an assignment that deals with Big-O and running times. I
I'm working on a class assignment that started small, so I had it all
Ok, I am working on an assignment for school, and I set up my
I am working on a C++ assignment for class that wants me to overload
I'm working on my first GWT-based Java project for a school assignment. It's a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.