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 8856827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:26:43+00:00 2026-06-14T14:26:43+00:00

When I send a string of bits to be decoded, it seems to require

  • 0

When I send a string of bits to be decoded, it seems to require one additional bit to decode properly. I’ve printed out the tree in pre-order, and I’ve drawn the tree on paper to make sure I wasn’t missing something. The pre-order and my drawn tree match, but the bits required to produce the correct letter is off.

public void decode(String code){
    String result = "";
    TreeNode current = root;
    current.preOrder();
    for(int i = 0;i < code.length();i++){
        //left--0
        if(Character.getNumericValue(code.charAt(i))==0){
            if(current.getLeft() == null){
                result += current.getWeight().getLetter();
                current = root;
                i--;
            }else
                current=current.getLeft();
        }
        //right--1
        else if(Character.getNumericValue(code.charAt(i))==1){
            if(current.getRight() == null){
                result += current.getWeight().getLetter();
                current = root;
                i--;
            }else
                current=current.getRight();
        }
    }
    System.out.println(result);
}

My tree is building correctly every time which makes me believe the error is in the decoding method. However, I can’t seem to figure out why it needs additional bits.

  • 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-06-14T14:26:45+00:00Added an answer on June 14, 2026 at 2:26 pm

    Without seeing how your nodes are laid out I can only guess. When traversing left/right you probably need to check if you’ve landed on a leaf node and emit its character if so.

    if (current.getLeft() == null) {
        ...
    }
    else {
        current = current.getLeft();
        if (current.isLeaf()) {
            result += current.getWeight().getLetter();
            current = root;
        }
    }
    

    The same goes for the right side.

    Don’t Repeat Yourself

    To avoid duplicating the two lines that append the character and reset the current node to the root four times, you could instead set a flag and check it at the end of the for block.

    boolean append = false;
    if (Character.getNumericValue(code.charAt(i)) == 0) {
        if (current.getLeft() == null) {
            append = true;
            i--;
        }
        else {
            current = current.getLeft();
            if (current.isLeaf()) {
                append = true;
            }
        }
    }
    // same for right side ...
    if (append) {
        result += current.getWeight().getLetter();
        current = root;
    }
    

    Other Tips

    • Switch from two if checks for 0 or 1 to a switch with a default that throws an IllegalArgumentException.
    • Switch the for loop to a while to avoid pre-decrementing i just to have it incremented again and avoid stopping the loop.
    • Start with append set to true since four out of six cases append.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i would like to send string arraylist values from one class another class.i
serial.write() method in pyserial seems to only send string data. I have arrays like
I need to send string messages between applications (many clients and one server). When
Hi I am uploadingfile to php server.For this we require to send string parameter
I can only send string or numeric values,how to send an array?
I want to send String from C# to Jave I do it in C#
I want to send base64 string through ajax I encoded it through JavaScript function
I need to send a string as parameter in UIButton selector. Taking global string
I'm trying to send a String[] over an open socket connection but it's doesn't
In Javascript I can send XML string to JSP server (XmlAction.jsp): Javascript Code: var

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.