• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 13:16
CET 19:16
KST 03:16
  • 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
TL.net Map Contest #21: Winners11Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13TL.net Map Contest #21: Voting12[ASL20] Ro4 Preview: Descent11
Community News
StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7[BSL21] RO32 Group Stage4Weekly Cups (Oct 26-Nov 2): Liquid, Clem, Solar win; LAN in Philly2Weekly Cups (Oct 20-26): MaxPax, Clem, Creator win10
StarCraft 2
General
SC: Evo Complete - Ranked Ladder OPEN ALPHA Mech is the composition that needs teleportation t TL.net Map Contest #21: Winners StarCraft, SC2, HotS, WC3, Returning to Blizzcon! RotterdaM "Serral is the GOAT, and it's not close"
Tourneys
Constellation Cup - Main Event - Stellar Fest Sparkling Tuna Cup - Weekly Open Tournament $5,000+ WardiTV 2025 Championship Merivale 8 Open - LAN - Stellar Fest Sea Duckling Open (Global, Bronze-Diamond)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
FlaSh on: Biggest Problem With SnOw's Playstyle BGH Auto Balance -> http://bghmmr.eu/ [ASL20] Ask the mapmakers — Drop your questions BW General Discussion Where's CardinalAllin/Jukado the mapmaker?
Tourneys
[Megathread] Daily Proleagues [ASL20] Grand Finals [BSL21] RO32 Group A - Saturday 21:00 CET [BSL21] RO32 Group B - Sunday 21:00 CET
Strategy
PvZ map balance Current Meta How to stay on top of macro? Soma's 9 hatch build from ASL Game 2
Other Games
General Games
Stormgate/Frost Giant Megathread Should offensive tower rushing be viable in RTS games? Nintendo Switch Thread Path of Exile Dawn of War IV
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
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread US Politics Mega-thread The Games Industry And ATVI
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread Movie Discussion! Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Learning my new SC2 hotkey…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Our Last Hope in th…
KrillinFromwales
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1398 users

C++ help? (part deux)

Blogs > tossinYoSalad
Post a Reply
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-11-11 02:06:42
November 11 2009 02:00 GMT
#1
Howdy guys, you all helped me a ton last time I had a problem, so I'm coming back for more! This one falls under more general algorithms then c++ in general. Anyway...

For this project i have a medium-sized database implemented as a Binary Search Tree using the author of the books implementation (it sucks). Anyway, I got everything working fine, except one thing. The BST has a "keyed item" that it uses to keep the tree sorted etc. The problem is, I have to display a report based on a non-keyed item. I can't just change the keyed item and resort the tree, and I can't move the items into a new data structure. Is there any remotely efficient way to do this? Am I just missing something?

tl;dr version: how to output from a binary search tree based on a NON keyed entry.

thanks a ton in advance guys.

edit:
I suppose I should elaborate a bit more. I know that I could just write a findMin method(function?) based on the non-keyed item, print it, delete it, and keep going through the tree. But this is horribly inefficient because of all the loop nesting. I'm asking if there's a better way to do it.

tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-11-11 02:06:37
November 11 2009 02:06 GMT
#2
double post.
imDerek
Profile Blog Joined August 2007
United States1944 Posts
November 11 2009 02:29 GMT
#3
So you need to display the non-keyed items in sorted order?
Least favorite progamers: Leta, Zero, Mind, Shine, free, really <-- newly added
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-11-11 02:46:03
November 11 2009 02:45 GMT
#4
On November 11 2009 11:29 imDerek wrote:
So you need to display the non-keyed items in sorted order?



yes..well..

the tree is made up of objects that contain like 25 fields each or so. One of the fields is the keyed item. I have to display the objects sorted by a different item lol.
allluckysevens7777
Profile Joined February 2009
United States53 Posts
November 11 2009 03:03 GMT
#5
Your idea isn't necessarily horribly inefficient (O(n*n)), but not fantastic I guess? Depends on what you're shooting for.

Why aren't you able to re-sort or use an additional data structure? Just a stipulation of the problem?

When you say "can't move the items into a new data structure": do you mean you aren't allowed any additional storage, or does this mean "can't create a new BST"?
If you can create just an additional array, then toss all of the items in there and do a quicksort for O(n lg n) performance, but it doesn't really look like you can squeeze anything better out. Will look at it a little more.
tarpman
Profile Joined February 2009
Canada719 Posts
Last Edited: 2009-11-11 03:04:02
November 11 2009 03:03 GMT
#6
why exactly can't you just read the fields you're interested in into an array and sort that? without copying stuff (or at least references to stuff) into another data structure I don't think there's really an efficient way to do this.


poster above me: sorry, but n^2 does in fact count as 'horribly inefficient'
Saving the world, one kilobyte at a time.
imDerek
Profile Blog Joined August 2007
United States1944 Posts
November 11 2009 03:12 GMT
#7
I suppose I should elaborate a bit more. I know that I could just write a findMin method(function?) based on the non-keyed item, print it, delete it, and keep going through the tree. But this is horribly inefficient because of all the loop nesting. I'm asking if there's a better way to do it.


You can consider using heapsort if you're going to do it this way, O(n lg n) performance, just need to heapify the tree, then keep removing the min element

http://en.wikipedia.org/wiki/Heapsort
Least favorite progamers: Leta, Zero, Mind, Shine, free, really <-- newly added
allluckysevens7777
Profile Joined February 2009
United States53 Posts
Last Edited: 2009-11-11 03:19:22
November 11 2009 03:18 GMT
#8
Wouldn't that count as re-sorting the tree? Works as an in-place alternative to the array solution though.
imDerek
Profile Blog Joined August 2007
United States1944 Posts
November 11 2009 03:19 GMT
#9
oh yeah but since you're altering the tree anyway u might as well re-sort it haha
Least favorite progamers: Leta, Zero, Mind, Shine, free, really <-- newly added
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-11-11 03:50:37
November 11 2009 03:49 GMT
#10
lol thanks for all the input guys. I cant re-sort the tree, and I can't move the elements into another data structure (including arrays). just a stipulation of the problem.

btw the findMin solution is worse than O(n^2).

findMin would have two nested loops, and findMin itself would be inside 2 nested loops.

Ill just go ahead and do it anyway. this class is fucking stupid. this is a very small part of a much larger project, and is the only part thats giving me headaches lol.
wok
Profile Blog Joined July 2009
United States504 Posts
November 11 2009 03:49 GMT
#11
heapsort. Since your items aren't indexed for the value you're looking for, you pretty much have to resort it. Heapsort is O(nlogn) as opposed to the min-first sorting you mentioned, which is O(n^2).
I'll race you to defeatism... you win.
wok
Profile Blog Joined July 2009
United States504 Posts
November 11 2009 03:53 GMT
#12
On November 11 2009 12:49 tossinYoSalad wrote:
lol thanks for all the input guys. I cant re-sort the tree, and I can't move the elements into another data structure (including arrays). just a stipulation of the problem.

btw the findMin solution is worse than O(n^2).

findMin would have two nested loops, and findMin itself would be inside 2 nested loops.


It should be better than n^2.
You look through n elements to find the smallest, then n-1 for the 2nd, etc...

You thus have O(n * (n-1)/2) = O(n^2). If your findMin method is > n^2 you're doing something wrong in your looping implementation.
I hypothesize your issue is that your tree traversal is shit. Pre-order, in-order, post-order are all O(n). You should be fine in all those cases.
I'll race you to defeatism... you win.
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
November 11 2009 04:37 GMT
#13
On November 11 2009 12:53 wok wrote:
Show nested quote +
On November 11 2009 12:49 tossinYoSalad wrote:
lol thanks for all the input guys. I cant re-sort the tree, and I can't move the elements into another data structure (including arrays). just a stipulation of the problem.

btw the findMin solution is worse than O(n^2).

findMin would have two nested loops, and findMin itself would be inside 2 nested loops.


It should be better than n^2.
You look through n elements to find the smallest, then n-1 for the 2nd, etc...

You thus have O(n * (n-1)/2) = O(n^2). If your findMin method is > n^2 you're doing something wrong in your looping implementation.
I hypothesize your issue is that your tree traversal is shit. Pre-order, in-order, post-order are all O(n). You should be fine in all those cases.


yeah i was just going through it in my head and I counted loops wrong. it would be n^2. in any case its all done and off to be graded.
tree traversal was implemented by the author of the book so I had nothing to do with that lol.
JohnColtrane
Profile Blog Joined July 2008
Australia4813 Posts
November 11 2009 06:06 GMT
#14
sorry i cant help but laugh whenever i see your name lmao
HEY MEYT
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
November 11 2009 07:22 GMT
#15
On November 11 2009 15:06 JohnColtrane wrote:
sorry i cant help but laugh whenever i see your name lmao



haha thanks . i like it. the full name is proTossinYoSalad.. has an extra ring to it.
Please log in or register to reply.
Live Events Refresh
Wardi Open
12:00
#60
WardiTV2250
IndyStarCraft 235
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
IndyStarCraft 235
UpATreeSC 87
StarCraft: Brood War
Rain 3549
Horang2 1587
Shuttle 779
firebathero 160
scan(afreeca) 52
sSak 37
Mong 30
Aegong 22
JulyZerg 17
ivOry 6
[ Show more ]
SilentControl 5
Dota 2
Gorgc5362
qojqva3580
420jenkins277
BananaSlamJamma178
XcaliburYe153
League of Legends
rGuardiaN20
Counter-Strike
fl0m572
byalli525
Other Games
FrodaN1033
Beastyqt715
ceh9567
KnowMe280
Lowko263
Sick257
Hui .188
Mew2King150
Liquid`VortiX147
ArmadaUGS79
QueenE48
Trikslyr46
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• kabyraGe 105
• Reevou 1
• IndyKCrew
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• Kozan
StarCraft: Brood War
• Michael_bg 9
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV218
League of Legends
• Nemesis3571
• TFBlade957
• imaqtpie471
Other Games
• Shiphtur284
Upcoming Events
Replay Cast
4h 44m
WardiTV Korean Royale
17h 44m
OSC
22h 44m
Replay Cast
1d 4h
Replay Cast
1d 14h
Kung Fu Cup
1d 17h
Classic vs Solar
herO vs Cure
Reynor vs GuMiho
ByuN vs ShoWTimE
Tenacious Turtle Tussle
2 days
The PondCast
2 days
RSL Revival
2 days
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
2 days
[ Show More ]
WardiTV Korean Royale
2 days
PiGosaur Monday
3 days
RSL Revival
3 days
Classic vs Creator
Cure vs TriGGeR
Kung Fu Cup
3 days
CranKy Ducklings
4 days
RSL Revival
4 days
herO vs Gerald
ByuN vs SHIN
Kung Fu Cup
4 days
BSL 21
5 days
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
Sparkling Tuna Cup
5 days
RSL Revival
5 days
Reynor vs sOs
Maru vs Ryung
Kung Fu Cup
5 days
WardiTV Korean Royale
5 days
BSL 21
6 days
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
Wardi Open
6 days
Monday Night Weeklies
6 days
Liquipedia Results

Completed

Proleague 2025-11-07
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual

Upcoming

SLON Tour Season 2
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
META Madness #9
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals 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.