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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:37:22+00:00 2026-06-12T22:37:22+00:00

I am trying to write a code, which, given a Binary Search Tree root

  • 0

I am trying to write a code, which, given a Binary Search Tree root and a level, prints out the elements of the tree at that level. This is working fine:

def myprint(root,level):
    if root:
        if not level:
            print root.data,
        else:
            myprint(root.left,level-1)
            myprint(root.right,level-1)

However, when I try to tweak it to print the elements at a level in reverse order, it dosn’t work. For the following tree:

                         26
                    /          \
                   13          39
                  /  \        /  \
                 6    19     32   51
                / \   / \    / \  / \
               4   8  14    31 33   68
                        \
                         17

if I want to output the elements at level 3 (level of root is 0) from right to left, the output should be 68 33 31 14 8 4. The code above does the reverse correctly, that is, prints out 4 8 14 31 33 68. But the below code doesn’t print the reverse order correctly, and prints out 31 33 68 4 8 14 instead:

def revprint(root,level):
    if root:
        if not level:
            print root.data,
        else:
            myprint(root.right,level-1)
            myprint(root.left,level-1)

Can anybody spot the error, and tell me how to rectify it? The code for initializing the tree is as follows:

class tree:
    def __init__(self,data):
        self.data = data
        self.successor,self.left,self.right = None,None,None
    def push(self,data):
        root = self
        while root:
            oldroot = root
            if root.data > data:
                root = root.left
            elif root.data < data:
                root = root.right
        if data > oldroot.data:
            oldroot.right = tree(data)
        else:
            oldroot.left = tree(data)

a = tree(26)
for x in [13,39,6,19,4,8,5,10,9,14,17,15,32,51,68,31,33,36,34]:
a.push(x)
  • 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-12T22:37:23+00:00Added an answer on June 12, 2026 at 10:37 pm

    You are calling myprint in revprint. The fixed version of revprint:

    def revprint(root,level):
        if root:
            if not level:
                print root.data,
            else:
                revprint(root.right,level-1)
                revprint(root.left,level-1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a code that converts a number to binary, and this
I'm trying to write a code which, given a path to an item in
I am trying to write some code that that will draw the line which
While trying to write code which returns 24 hours less than a given time,
I'm trying to write a code which stores strings in an array. I'm trying
I am trying to write some simple code which will read a text file
I'm trying to write some C code which is portable only so far as
I'm trying to write a piece of code which use the user choice in
I'm trying to write some Haskell code in which there are multiple data types,
I'm trying to write some C++ code for Windows 7 which will connect to

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.