Need help printing out a BinarySearchTree

I am trying to write a method to print out a BinarySearchTree, so I can better debug my code. For instance, if the binarysearchtree is in this form:
6
3 9
2 4 12 8
Then I want to print out this exact form using System.out.println. The recursive traversal techniques (inorder, postorder, preorder) do not accomplish this. When I tried to use one of the technique, I got a weird looking tree that either rotate 90 degree or upside down. Any ideas? Thanks

I agree, but I would like to take this opportunity to
learn some new trick too.There isn't any trick. There's just a lot of grungy programming. You have to keep track of how many nodes you're going to display on line 1, line 2, line 3, and so on. And how wide those nodes are. (And "how wide a node is" depends on how wide its children are and whether its neighbours have children or not.) You can't display line 1 until you have laid out the entire tree conceptually, because you don't know how much space to leave to its left so that it is centred above its children.
Now if you've got a complete binary tree (as uj suggested earlier) then this is simple. But if you don't, then it isn't simple at all. Unless, of course, you want to pretend that you do, and just leave space where the missing nodes are. That would be pretty simple. Find the maximum depth of your tree, call that D, and find the maximum width of a node, call that N. Then the bottom level of your tree would be N * 2^D wide (I probably have an off-by-one error in D there so fix that if necessary).
So line 1 has one node, which automatically goes in the middle. If line 2 is the last line then its two nodes go at the right and left ends. If it isn't, but line 3 is going to be the last line, then line 2's nodes go 1/6 and 5/6 of the way across. If there's going to be four lines the line 2's nodes go 3/14 and 11/14 of the way across. (I figured those out by drawing pictures like the examples you posted.) Find the general formula that yields these numbers. Then do the same for line 3 and line N.
(If you want to draw the tree upside-down, it's quite a bit easier. Draw the last line, keeping track of where you draw each node. Then when you draw the next line, each node gets drawn between where you drew its children, recursively.)
This is the simple version we are doing here, remember. You will probably learn a lot by finishing it off, but not a lot about tree structures. The general version is not a generalization of this algorithm, but a completely different algorithm.

Similar Messages

  • I need to print out my address contacts

    I need to print out my address contacts

    You'll need to do it from the contact manager you sync your contscts with on your computer.

  • TS3798 I get this error message"your operation could not be completed" I need help figuring out why I can not access the web page.

    I get this error message"your operation could not be completed" I need help figuring out why I can not access the web page.

    amarilysfl wrote:
    "Your disk could not be partitioned. An error occurred while partitioning the disk".
    https://www.apple.com/support/bootcamp/
    If you were using Apple's BootCamp and received this message, quit it and open Disk Uility in your Applicaitons/Utilities folder.
    Select the Macintosh HD partition on the left and select Erase and Erase Free Space > Zero option and let it complete (important) this will check the spare space for bad sectors that can cause issues formatting partitions.
    Once it's completed, try creating a partiton again in BootCamp.
    If that doesn't work, then hold command option r keys down while connected to a fast internet connection, Internet Recovery should load (spinning globe) and then in that Disk Utility, select your entire internal drive and click > First Aid > Repair Disk and Permissions.
    reboot and attempt Bootcamp again.
    If you still get a error, it might be that you have OS X data on the bottom area where BootCamp partition needs to go. This would occur if you had the drive or computer for a long time or wrote a large amount of files to the drive and nearly filling it up and then reduced some, but it left traces in the area BootCamp needs to go.
    To fix this
    BootCamp: "This disc can not be partitioned/impossible to move files."
    How to safely defrag a Mac's hard drive

  • I need to print out the response forms.  How do I do it?

    I need to print out the response forms.  How do I do it?

    They are saved as PDF files, so you just need to install Adobe Reader, which you can download for free: http://get.adobe.com/reader/

  • I have a mac 10.5 and need help figuring out how to change my email settings so it does not automatically delete my inbox every 30 days.  How to I adjust the mail settings?

    I have a mac 10.5 and need help figuring out how to change my email settings so it does not automatically delete my inbox every 30 days.  How to I adjust the mail settings?

    I think it must be an IMAP account then, & in Mail>Preferences>Accounts>Advanced>Keep copies for Offline viewing:>Don't keep copies, then on the Server, or maybe it's just this one itself, but on the Server you have a setting to remove eMails after 30 days.
    If it's a POP account we'd have to investigate that... but you didn't say so I'm guessing here.

  • Need Help figuring out how to alphabetize itunes artist.

    I need help figuring out one more thing. I have my artist tab in my itunes library and I need to figure out how to get all the artists grouped together instead of being scattered all around.
    This is how it looks now:
    Justin Timberlake
    Linkin Park
    Linkin Park
    Linkin Park
    Justin Bieber
    Daughtry
    Linkin Park
    Linkin Park
    JoJo
    Linkin Park
    This is how I want it to look:
    Daughtry
    Justin Timberlake
    Justin Bieber
    JoJo
    Linkin Park
    Linkin Park
    Linkin Park
    Linkin Park
    Linkin Park
    Please help! Thanks!

    Enable the Sort Artist, Album Artist and Sort Album Artist columns so you can see what is going on. Album Artist normally takes precedence over Artist, with the Sort fields further controlling the order.
    See also Grouping tracks into albums.
    tt2

  • How do I get my previous contacts from mobileme now to the icloud? I need help figuring out how to make that transition?

    How do I get my previous contacts from mobileme now to the icloud? I need help figuring out how to make that transition?

    Try using the app My Contacts Backup, which will back up your contacts as an attachment to an email.  Send this email to yourself, open it on your Mac and double-click the attachment to import them to Address Book (or Contacts if you have Mountain Lion).

  • Need help picking out a printer...

    I am trying to pick out a good printer. My laser (2600n) finally died. I'm looking for something with a low cost per page. I am in grad school and print out a lot of simple word docs, and class notes.
    I'm not sure if any of the inkjets can give me a low cost per page. Any ideas on this? I don't need to do pictures and all of that, just allot of pages. Maybe around 500-1000 pages a month.

    Hi,
    my hp photosmart C6180 all in one works flawlessly with it plugged in to my time capsule all functions.
    I used to have it set up using it wirelessly from the printer however for some reason after a update (i think) it stopped working & would not print so had to plug in to time capsule.
    Dave

  • Need helping printing to networked printers.

    I need help setting up network printers that have a code on them for each individual. There is no place to put a code so the printer will know who is printing what. I have installed 3 printers and they will not print, they just go to a paused mode and when you hit resume in reverts back to pause because it is looking for a code. I have tried it another way but it asks for a user name and password and all we have are codes. The workplace is using a windows based server and the copiers are listed as followed.
    Konica biz hub c450
    Sharp ar m700n
    sharp mx700n
    I need help to be able to print from my mac. I also have parallels running with xp installed and all the printers are installed on that side and printing fine. Hopefully someone can help me out.
    Thanks in advance

    Hello and welcome to Apple Discussions.
    In order to provide the user with the ability to input a user code, the respective printer driver would have to provide the facility. If this feature did exist on a previous version of OS X, then you may have to check the vendors web site to see if there is a driver for 10.6.
    If you are not sure if the function was supported previously, then go through all the user menus for the driver. The function may be present but located in an unusual location. Or it could require an additional file (aka plugin) that could be missing from the driver installation or not compatible with 10.6.
    The other thing to note is that if you have the Mac's printing via a Windows queue, you will have to provide user credentials for SMB print queues. This is typically a Windows user account - not the Mac's account details. If you don't want to create accounts for the Mac users on the Windows server, then you can use LPD rather than SMB to connect to the Windows queues. This does require UNIX Printing Services to be enabled on the server.
    Pahu

  • I need a print out of initial activation!

    How can I get a print out of my initial activation of my cell phone?

    You can log in to your MyVerizon account (if you are the account owner), then go to View Bill, and on that screen is a link to View Account Documents. There you can view contracts and other account documents.
    If that isn't the information you need, a call to customer service should help.

  • I need help printing with HP laserjet 2200 and OS 10.6

    I need help setting up my HP laserjet 2200 and OS 10.6 -- I've downloaded and installed all the software and still can't print.

    Hey jeo1951,
    If you haven't already, try running through the recommended steps in the following article:
    Mac 101: Printing (Mac OS X v10.6)
    http://support.apple.com/kb/HT3771
    It should help you set up your printer.
    Welcome to Apple Support Communities!
    All the best,
    Delgadoh

  • I need help figuring out how to Link Data from one table in Sharepoint to another table in Sharepoint and make it dynamic

    Hi Everyone,
    I am working on a project that uses SharePoint 2010 for repository and we have multiple tables that we are using.
    One table is a list of the various projects we are working on (workstreams)
    The other is a table with various notes and such, to include meeting notes for the various projects.
    Currently we are just copying the shortcut from the meeting notes table and adding it in as a link in the workstreams status table.
    I need to find out if there is some sort of way to make this process dynamic so that when a new note is added for, lets say workstream #1 in the notes table, it will automatically propogate into the notes link section for workstream #1.
    Is this even possible??
    And if so how would I do it
    I haven't done much with modifying SharePoint in several years, and would Truly appreciate any and all help with this.
    Thanks in advance
    IKE

    Hi IKE,
    According to your description, my understanding is that you want to create a link in the workstreams list linked to the associated item of the notes list.
    Assuming the workstreams list has  Title column and another column called “Note” used to linked to notes list, and the notes list has Title column and another column used to store note.
    Then you can create a workflow on the notes list with SharePoint Designer 2010, and in the workflow, using “Update ListItem” action. And in the workflow Start Options, select “Start workflow automatically when an item is created”.
    For the workflow action, you can set as the screenshot:
    For the string in the String Builder, you need to open an item of notes list, then copy the URL of the display form and paste here, the URL will look something like this:
    http://sp/Lists/notes/DispForm.aspx?ID=1&Source=http%3A%2F%2Fsp%2FLists%2Fnotes%2FAllItems%2Easpx&ContentTypeId=0x0100300025278693CE49A5E3A394B23AD9E1&IsDlg=1
    Now, you need to replace the ID of the item using [%Current Item:ID%], for example in the above link, delete the number 1 and add [%Current Item:ID%].
    Best Regards,
    Wendy
    Wendy Li
    TechNet Community Support

  • I need help figuring out GRUB [SOLVED]

    So, I'm having a bit of trouble figuring out how grub's menu.lst should be configured on my system. I've scoured the internet and wiki, but I can't quite get it. Hopefully you can help me!
    Here's some info:
    joe@Snow-Crash:~$ sudo fdisk -l
    [sudo] password for joe:
    Disk /dev/sda: 160.0 GB, 160041885696 bytes
    255 heads, 63 sectors/track, 19457 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x1e721e72
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1               1       16554   132969973+  83  Linux
    /dev/sda2   *       16555       19104    20482875   83  Linux
    /dev/sda3           19105       19457     2835472+   5  Extended
    /dev/sda5           19105       19457     2835441   82  Linux swap / Solaris
    I have HH 8.04 on my main partition [sda1], and I'm trying to put Arch on my 20gb sda2 partition. I followed the beginner's guide up to the install grub section. When it had me review menu.lst the pertinent Arch section was
    "title Arch Linux
    root (hd0,0)
    kernel /vmlinuz26 root=/dev/hda3 ro
    initrd /kernel26.img"
    Now, I was planning on just copying this bit and pasting it into HH's menu.lst (and not installing grub in arch's partition), but I thought that it didn't seem quite right. It turns out I was right, since I get an "error 15: File not found" when I choose to boot Arch.
    So what should it be? This is how I understand it, in my admittedly newbie way.
    Am I mistaken in that it should be "root (hd0,0)"   Since the root command points to where grub is residing (and I'm using HH's grub in sda1)?
    And "kernel (hd0,1)/vmlinuz26 root=/dev/sda2" Since I need to designate that the kernel is on a different partition than (hd0,0), and my Arch partition is sda2?
    and as far as I can tell it should be "initrd (hd0,1)/kernel26.img" since it's on a different partition.
    I know I'm doing something wrong here, since I still get the same "file not found" error message. Can someone point out the error in my ways?
    PS: Since I was having trouble with this I decided to try to to install GRUB in sda2 and use chainloader to point to it from HH's menu.lst. But when I tried to install grub via my Arch cd it gave me a generic "unable to install Grub" message. So I'm up a creek!
    I hope you guys can help me out! Thanks.
    Last edited by Joe_Arch (2008-11-27 07:51:13)

    Welcome Joe_Arch,
    You have to set the root to the partition that contains the "OS image" (the kernel).  Also, you apparently don't have a separate /boot partition, so you have to add /boot/ to the beginning of the file names.
    root (hd0,1)
    kernel /boot/vmlinuz26 root=/dev/sda2 ro
    initrd /boot/kernel26.img

  • I need to print out a 2'' by 11/2'' photo from a 8

    I would like to reduce a full size photo to 2 inches by 11/2 inches.  My priinter is a jetset 4500 and Im woriing with windows 8 OS on a Acer Laptop.  Is the possible?  If so how do I go about it.

    Hello there @BobC3 , welcome to the HP community!
    I understand you have an Officejet 4500 printer, and are looking to print out a photo that is 2 x 11.5 inches. The printer would have to support the size you want to print the photo on.
    There are three 4500 models, please let me know which one you have and I can check the supported photo sizes for you.
    If you wish to check, click on which printer you have and select Product Information, Product Specifications, Printer Specifications, Paper Handling Specifications, Supported Photo Paper.
    HP Officejet 4500 All-in-One Printer - G510g
     HP Officejet 4500 Desktop All-in-One Printer - G510a
     HP Officejet 4500 Wireless All-in-One Printer - G510n
    Have a great day!
    R a i n b o w 7000I work on behalf of HP
    Click the “Kudos Thumbs Up" at the bottom of this post to say
    “Thanks” for helping!
    Click “Accept as Solution” if you feel my post solved your issue, it will help others find the solution!

  • Need Help Printing Text Messages From E71

    I need to print some saved text messages that are on my e71 but cannot ge **bleep** to hook up to my bluetooth printer it just never finds it. Is there another way to print these messages fromt he phone i really need them

    connect to pc and use ovi suite
    If  i have helped at all a click on the white star below would be nice thanks.
    Now using the Lumia 1520

Maybe you are looking for