• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 11:02
CET 17:02
KST 01:02
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Revival - 2025 Season Finals Preview7RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3
StarCraft 2
General
RSL Revival - 2025 Season Finals Preview Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced
Tourneys
RSL Offline Finals Info - Dec 13 and 14! Tenacious Turtle Tussle 2025 RSL Offline Finals Dates + Ticket Sales! Sparkling Tuna Cup - Weekly Open Tournament StarCraft2.fi 15th Anniversary Cup
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress Mutation # 500 Fright night
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ [BSL21] RO8 Bracket & Prediction Contest BW General Discussion FlaSh on: Biggest Problem With SnOw's Playstyle Let's talk about Metropolis
Tourneys
[ASL20] Grand Finals [BSL21] RO8 - Day 2 - Sunday 21:00 CET [BSL21] RO8 - Day 1 - Saturday 21:00 CET Small VOD Thread 2.0
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Fighting Spirit mining rates Current Meta
Other Games
General Games
Dawn of War IV Path of Exile Stormgate/Frost Giant Megathread Awesome Games Done Quick 2026! Nintendo Switch Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine US Politics Mega-thread YouTube Thread European Politico-economics QA Mega-thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream?
Blogs
How Sleep Deprivation Affect…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1538 users

Need help with programming homework.

Blogs > supernovamaniac
Post a Reply
supernovamaniac
Profile Blog Joined December 2009
United States3047 Posts
Last Edited: 2010-11-17 05:24:10
November 17 2010 05:23 GMT
#1
(This has to be coded in C++)

Given a string and the node to a tree (top most), we have to print out the tree such that it looks nice.

Say, if there are 1, 2, 3, 4, 5, 6, 7 in the tree (in that order), the it should look like
___1___
_2___3_
4_5_6_7

(looks nice eh?)

Now, the problem is that I don't know how to use nodes. Apparently, when we're given the first node, my friends told me that I have to travel down the node using left, right.

Simply put, I'm lost. If I can have the order of the elements of the tree in in-order, then I can finish the program. Unfortunately, I can't even start.

I can post more details about this tomorrow if anyone is willing to help out.

Also, I think it has to run in O(n). Not sure if it was O(log n) or O(n)

ppp
rauk
Profile Blog Joined February 2009
United States2228 Posts
Last Edited: 2010-11-17 05:56:41
November 17 2010 05:55 GMT
#2
so the first thing you wanna do is put the elements into in the tree, breadth-first.

1 procedure BFS(Graph,source):
2 create a queue Q
3 enqueue source onto Q
4 mark source
5 while Q is not empty:
6 dequeue an item from Q into v
7 for each edge e incident on v in Graph:
8 let w be the other end of e
9 if w is not marked:
10 mark w
11 enqueue w onto Q

i just grabbed that from the wiki on http://en.wikipedia.org/wiki/Breadth-first_search

that is a search, but you apply the same principle in populating your tree graph
Azzur
Profile Blog Joined July 2010
Australia6260 Posts
November 17 2010 06:07 GMT
#3
You need to use recursion to travel down the nodes.

Something like:

travel(Node node) {
if (node.left != null) travel(node.left);
if (node.right != null) travel(node.right);
}

There are probably a few ways to solve this. I had a quick think and it's not that trivial, but here's a possible solution:
1. Determine the depth of the tree (this is pretty tricky). The depth will help you format your printout.
2. Travel through the node again, but this time print out the numbers. Use the depth to help you format it.


AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
November 17 2010 06:14 GMT
#4
First you'll need to get the depth of the tree. There's probably a routine available for you to do this. Then you need to do a breath-order search as rauk mentioned. As you retrieve each item, you'll need to print out the appropriate number of underscores, which should be a function of what depth you're at and the total depth.

Some hints:

You can detect whether you've reached the end of a particular row by checking whether the total number of elements you've printed out in the current row is a power of 2 and more than you printed out in the above row, at which point you'll print a newline.

There are two types of underscore separations: beginning/end of a row, and between elements

Notice that the number of underscores in each corresponding separation section as you move down the tree decreases exponentially.
tofucake
Profile Blog Joined October 2009
Hyrule19177 Posts
November 17 2010 12:54 GMT
#5
Using the power of 2 check assumes it's a balanced binary tree. If it's unbalanced, that won't work.
Liquipediaasante sana squash banana
Please log in or register to reply.
Live Events Refresh
WardiTV 2025
13:00
Seeding Matches
SHIN vs herO
Clem vs herO
Clem vs ShoWTimELIVE!
WardiTV1727
ComeBackTV 1023
TaKeTV 335
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
MindelVK 45
BRAT_OK 40
Railgan 31
DivinesiaTV 17
StarCraft: Brood War
Britney 33714
Calm 3312
Bisu 1268
Jaedong 877
Horang2 813
Stork 593
Hyuk 417
actioN 206
Last 156
EffOrt 145
[ Show more ]
Hyun 125
Sharp 92
Zeus 80
Killer 56
Sea.KH 54
Mong 52
Aegong 50
scan(afreeca) 36
zelot 29
Movie 25
Noble 12
ajuk12(nOOB) 12
Dota 2
Gorgc5091
singsing4078
qojqva2477
syndereN491
XcaliburYe196
LuMiX0
Counter-Strike
fl0m2779
allub234
oskar82
Heroes of the Storm
Khaldor506
Liquid`Hasu316
Other Games
FrodaN2856
B2W.Neo951
Beastyqt694
Mlord541
Fuzer 197
XaKoH 117
KnowMe104
Mew2King38
ArmadaUGS34
OptimusSC22
ceh91
Organizations
Other Games
EGCTV1260
StarCraft: Brood War
Kim Chul Min (afreeca) 15
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• HeavenSC 20
• Adnapsc2 17
• poizon28 17
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis2822
Other Games
• Scarra1213
Upcoming Events
IPSL
3h 58m
Sziky vs JDConan
BSL 21
3h 58m
Tech vs Cross
Bonyth vs eOnzErG
Replay Cast
16h 58m
Wardi Open
19h 58m
Monday Night Weeklies
1d
Sparkling Tuna Cup
1d 17h
Replay Cast
3 days
The PondCast
3 days
CranKy Ducklings
5 days
SC Evo League
5 days
[ Show More ]
BSL 21
6 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

Acropolis #4 - TS3
RSL Revival: Season 3
Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
Slon Tour Season 2
WardiTV 2025
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.