• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 01:27
CET 07:27
KST 15:27
  • 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 Preview8RSL 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 15-21): Classic wins big, MaxPax & Clem take weeklies3ComeBackTV's documentary on Byun's Career !11Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2
StarCraft 2
General
ComeBackTV's documentary on Byun's Career ! Team TLMC #5: Winners Announced! What's the best tug of war? The Grack before Christmas Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies
Tourneys
OSC Season 13 World Championship $5,000+ WardiTV 2025 Championship $100 Prize Pool - Winter Warp Gate Masters Showdow Sparkling Tuna Cup - Weekly Open Tournament Winter Warp Gate Amateur Showdown #1
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 505 Rise From Ashes Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement
Brood War
General
How soO Began His ProGaming Dreams Klaucher discontinued / in-game color settings BGH Auto Balance -> http://bghmmr.eu/ Recommended FPV games (post-KeSPA) BW General Discussion
Tourneys
[BSL21] WB & LB Finals - Sunday 21:00 CET [BSL21] LB SemiFinals - Saturday 21:00 CET Small VOD Thread 2.0 [Megathread] Daily Proleagues
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Current Meta Fighting Spirit mining rates
Other Games
General Games
Mechabellum Nintendo Switch Thread Stormgate/Frost Giant Megathread Beyond All Reason Path of Exile
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
12 Days of Starcraft US Politics Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread How Does UI/UX Design Influence User Trust?
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List TL+ Announced Where to ask questions and add stream?
Blogs
National Diversity: A Challe…
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: 2082 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
Replay Cast
01:00
StarCraft Evolution League #17
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft615
Nina 187
StarCraft: Brood War
Rain 7088
Shuttle 444
Leta 121
scan(afreeca) 60
Mong 52
Dota 2
monkeys_forever558
NeuroSwarm188
LuMiX1
League of Legends
JimRising 808
C9.Mang0467
Counter-Strike
summit1g8333
minikerr29
Heroes of the Storm
Khaldor101
Other Games
fl0m675
RuFF_SC2147
Organizations
Other Games
gamesdonequick832
BasetradeTV46
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• practicex 34
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Diggity7
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Rush1375
• Lourlo1310
• HappyZerGling115
Upcoming Events
BSL 21
13h 33m
Sziky vs eOnzErG
Sparkling Tuna Cup
1d 3h
Krystianer vs TBD
TriGGeR vs SKillous
Percival vs TBD
ByuN vs Nicoract
OSC
1d 11h
BSL 21
1d 13h
Cross vs Dewalt
Replay Cast
2 days
Wardi Open
2 days
OSC
3 days
Solar vs MaxPax
ByuN vs Krystianer
Spirit vs TBD
OSC
6 days
Korean StarCraft League
6 days
Liquipedia Results

Completed

Escore Tournament S1 - W1
WardiTV 2025
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL Season 21
CSL Season 19: Qualifier 2
eXTREMESLAND 2025
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

Upcoming

CSL 2025 WINTER (S19)
Escore Tournament S1 - W2
Escore Tournament S1 - W3
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
Nations Cup 2026
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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.