• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 11:05
CEST 17:05
KST 00:05
  • 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
Serral wins EWC 202543Tournament Spotlight: FEL Cracow 202510Power Rank - Esports World Cup 202580RSL Season 1 - Final Week9[ASL19] Finals Recap: Standing Tall15
Community News
Weekly Cups (Jul 28-Aug 3): herO doubles up6LiuLi Cup - August 2025 Tournaments3[BSL 2025] H2 - Team Wars, Weeklies & SB Ladder10EWC 2025 - Replay Pack4Google Play ASL (Season 20) Announced58
StarCraft 2
General
Weekly Cups (Jul 28-Aug 3): herO doubles up Clem Interview: "PvT is a bit insane right now" Serral wins EWC 2025 TL Team Map Contest #5: Presented by Monster Energy Would you prefer the game to be balanced around top-tier pro level or average pro level?
Tourneys
Global Tourney for College Students in September Sparkling Tuna Cup - Weekly Open Tournament WardiTV Mondays $5,000 WardiTV Summer Championship 2025 LiuLi Cup - August 2025 Tournaments
Strategy
Custom Maps
External Content
Mutation # 485 Death from Below Mutation # 484 Magnetic Pull Mutation #239 Bad Weather Mutation # 483 Kill Bot Wars
Brood War
General
BW General Discussion Help, I can't log into staredit.net How do the new Battle.net ranks translate? Which top zerg/toss will fail in qualifiers? Google Play ASL (Season 20) Announced
Tourneys
[CSLPRO] It's CSLAN Season! - Last Chance [Megathread] Daily Proleagues [ASL20] Online Qualifiers Day 2 Cosmonarchy Pro Showmatches
Strategy
Simple Questions, Simple Answers [G] Mineral Boosting Muta micro map competition Does 1 second matter in StarCraft?
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Total Annihilation Server - TAForever Beyond All Reason [MMORPG] Tree of Savior (Successor of Ragnarok)
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
European Politico-economics QA Mega-thread US Politics Mega-thread Things Aren’t Peaceful in Palestine Bitcoin discussion thread 9/11 Anniversary
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread [\m/] Heavy Metal Thread Korean Music Discussion
Sports
2024 - 2025 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Gtx660 graphics card replacement Installation of Windows 10 suck at "just a moment" Computer Build, Upgrade & Buying Resource Thread
TL Community
TeamLiquid Team Shirt On Sale The Automated Ban List
Blogs
[Girl blog} My fema…
artosisisthebest
Sharpening the Filtration…
frozenclaw
ASL S20 English Commentary…
namkraft
The Link Between Fitness and…
TrAiDoS
momentary artworks from des…
tankgirl
from making sc maps to makin…
Husyelt
Customize Sidebar...

Website Feedback

Closed Threads



Active: 809 users

Need help with programming homework.

Blogs > supernovamaniac
Post a Reply
supernovamaniac
Profile Blog Joined December 2009
United States3046 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
Australia6259 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
Hyrule19057 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
Stormgate Nexus
14:00
Stormgate Launch Days
BeoMulf221
TKL 194
IndyStarCraft 179
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Reynor 360
SpeCial 81
StarCraft: Brood War
Britney 50665
Bisu 3971
Shuttle 2425
Mini 930
Soulkey 575
ggaemo 463
Snow 365
actioN 350
ZerO 277
Soma 221
[ Show more ]
Hyuk 197
sSak 157
Leta 91
ToSsGirL 76
sorry 74
Nal_rA 57
Sharp 56
Aegong 40
soO 39
[sc1f]eonzerg 33
sas.Sziky 29
zelot 27
scan(afreeca) 20
Rock 17
ajuk12(nOOB) 17
Sacsri 16
Terrorterran 13
Backho 12
IntoTheRainbow 10
SilentControl 9
JulyZerg 8
ivOry 4
Stormgate
BeoMulf221
TKL 194
IndyStarCraft 179
Dota 2
Gorgc5777
Dendi1606
XcaliburYe381
Counter-Strike
pashabiceps504
flusha368
byalli292
kRYSTAL_56
Heroes of the Storm
XaKoH 112
Other Games
gofns1900
hiko891
Beastyqt480
crisheroes383
KnowMe370
Hui .365
RotterdaM261
DeMusliM253
Fuzer 212
ArmadaUGS92
QueenE71
ZerO(Twitch)18
Trikslyr14
Organizations
StarCraft 2
WardiTV1060
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• StrangeGG 84
• poizon28 23
• davetesta16
• Kozan
• AfreecaTV YouTube
• intothetv
• sooper7s
• IndyKCrew
• LaughNgamezSOOP
• Migwel
StarCraft: Brood War
• FirePhoenix10
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV791
League of Legends
• Nemesis2797
• Jankos1054
Upcoming Events
uThermal 2v2 Circuit
55m
DaveTesta Events
8h 55m
The PondCast
18h 55m
WardiTV Summer Champion…
19h 55m
Replay Cast
1d 8h
LiuLi Cup
1d 19h
uThermal 2v2 Circuit
1d 23h
RSL Revival
2 days
RSL Revival
2 days
uThermal 2v2 Circuit
2 days
[ Show More ]
CSO Cup
3 days
Sparkling Tuna Cup
3 days
uThermal 2v2 Circuit
3 days
Wardi Open
4 days
RotterdaM Event
5 days
RSL Revival
6 days
Liquipedia Results

Completed

ASL Season 20: Qualifier #2
FEL Cracow 2025
CC Div. A S7

Ongoing

Copa Latinoamericana 4
Jiahua Invitational
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
HCC Europe
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025

Upcoming

ASL Season 20
CSLPRO Chat StarLAN 3
BSL Season 21
BSL 21 Team A
RSL Revival: Season 2
Maestros of the Game
SEL Season 2 Championship
WardiTV Summer 2025
uThermal 2v2 Main Event
Thunderpick World Champ.
MESA Nomadic Masters Fall
CS Asia Championships 2025
Roobet Cup 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 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.