• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 11:42
CEST 17:42
KST 00:42
  • 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 HomeStory Cup 2915Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
GSTL Returns in 2026!20Weekly Cups (Aug 3-9): Protoss get shut out7RSL goes to London! 2026 Offline Finals Nov 21-2212Weekly Cups (July 27-Aug 2): SHIN's big week0SC4ALL II: Brood War - $2500 - Dec 5-611
StarCraft 2
General
GSTL Returns in 2026! SC4ALL: II Talent Announcement! Balance hotfix patch 5.0.16b (July 16) Protoss AoE skill expression Weekly Cups (Aug 3-9): Protoss get shut out
Tourneys
PIG STY FESTIVAL 8.0! (13 - 23 August) 2026 KungFu Cup Announcement Sparkling Tuna Cup - Weekly Open Tournament RSL goes to London! 2026 Offline Finals Nov 21-22 RSL Revival: Season 6 - Qualifiers and Main Event
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 538 Media Blackout The PondCast: SC2 News & Results Mutation # 537 Hostile Territory Mutation # 536 Railroad Switch
Brood War
General
ASL22 General Discussion Best Games Kespa era BW General Discussion StarCraft 64 is coming to Philly at SC4ALL II [Personal Project Share] Terran Defense v0.60
Tourneys
Escore Tournament - Season 3 Small VOD Thread 2.0 [Megathread] Daily Proleagues SC4ALL II: Brood War - $2500 - Dec 5-6
Strategy
Odyssey Mineral Stack Saturation Fighting Spirit mining rates Any training maps people recommend? Simple Questions, Simple Answers
Other Games
General Games
Nintendo Switch Thread Anyone here play Quakeworld back in the day? Stormgate/Frost Giant Megathread EVE Corporation General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank TL Mafia Community Thread NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
US Politics Mega-thread European Politico-economics QA Mega-thread Russo-Ukrainian War Thread Artificial Intelligence Thread The Letting Off Steam Thread
Fan Clubs
MarineLorD Fan Club The ShoWTimE Fan Club The herO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread Series you have seen recently... [Req][Books] Good Fantasy/SciFi books
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Simple Questions Simple Answers FPS when play League Of Legend on laptop
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
LOCKPICKING NOOB
LUCKY_NOOB
Chinese Gen Z: Between Dream…
TrAiDoS
Cathedral Of CS And NY pizza a…
FuDDx
Please support my new stand…
Peanutsc
Hello guys!
LIN1s
Customize Sidebar...

Website Feedback

Closed Threads



Active: 6880 users

[!] Help with binary search trees.

Blogs > Sinensis
Post a Reply
Sinensis
Profile Blog Joined April 2009
United States2513 Posts
November 15 2009 18:12 GMT
#1
I'm using java to create iterative traversals through binary search trees.

I have a NLR traversal here that I made: http://pastebin.com/m5c01b50

I need a RNL traversal next and I just can't figure it out. Once again, it has to be iterative, no recursion allowed. Please help.

*
onmach
Profile Blog Joined March 2009
United States1241 Posts
November 15 2009 19:14 GMT
#2
Here is some psuedocode. You will have to turn it into java yourself. Start with a variable currentNode that is set at the root and an empty node stack.

1. If there is a node to the right, push current node onto stack, follow right node (by setting it to currentNode).
2. If there is no right node, print current node.
3. If there is a left node push it onto stack. follow left node.
4. If there is no left node pop your current node from the stack. If you can't pop anything because your stack is empty, you are done.

That _should_ do it.
DeathByMonkeys
Profile Blog Joined March 2008
United States742 Posts
November 15 2009 22:22 GMT
#3
Do they have linked lists in java?
Sinensis
Profile Blog Joined April 2009
United States2513 Posts
Last Edited: 2009-11-15 23:17:49
November 15 2009 22:23 GMT
#4
edit:

The code I'm trying to implement this with is giving me nonsense output (only displays 3 nodes out of many).

I've got the pseudocode and loop to accomplish it here: http://pastebin.com/m23f256a

Any other suggestions?
evanthebouncy!
Profile Blog Joined June 2006
United States12796 Posts
November 16 2009 05:18 GMT
#5
you can turn recursive program to iterative program using a stack easily.

So normally if you want to write iter, or at least this is what I do, is I write a recursive one first, then try to convert it into an iterative one.

For trees, the idea of a "fringe" is crusial, make sure you understand what it is and how to use it.
Here's an overview:

A fringe is where you put your unexpanded nodes on. For instance, if you want to do a Dpth first search on a tree and print everything, you can do:

1) recursively:
define printTree(node):
......if leaf?(node)
.........print node.content
......else:
..........printTree(node.left)
..........print node.content
..........printTree(node.right)

2) iteratively:
define printTree(node):
......fringe = new Stack()
......visited = new Set()
......fringe.push(node)
......while(fringe.notEmpty())
............current = fringe.pop
............if current in visited:
..................print current.content
............else:
..................visited.add(current)
..................fringe.push(current.left)
..................fringe.push(current)
..................fringe.push(current.right)
remark: there's bit bug here where what if a node doesn't have children but whatever, the general idea is same.

So then, breadth first search is EXACTLY THE SAME except you use a Que for your fringe, and a whole sort of tree search is done with different fringe implementation, and how to use it.

In short, all tree search is done by maintaining a fringe, and deciding what/when to put in it, and how/when to take it out
Life is run, it is dance, it is fast, passionate and BAM!, you dance and sing and booze while you can for now is the time and time is mine. Smile and laugh when still can for now is the time and soon you die!
onmach
Profile Blog Joined March 2009
United States1241 Posts
Last Edited: 2009-11-16 21:09:27
November 16 2009 21:02 GMT
#6
Looking at your pastebin, you are close, but not quite.
Since you made a good faith attempt, here is my attempt at solving this. I have no compiled this, there may still be errors, but you can see that this is pretty close.

http://pastebin.com/d3e9d837a

The idea here is you have to go right as far as you can saving each node along the way into your stack, print the node, then go left once, right as far as you can again, print node, etc. When you can no longer go right or left, you go back up (that is where the pop comes in).

I'm not a java programmer and I don't have an environment for it at work, so you'll undoubtedly have to tweak that a bit. I may have the termination condition slightly wrong, but otherwise I think that is pretty close.

Edit: Actually this will have an infinite loop. You'll have to do what the guy above me said, you will have to keep a list of nodes you have visited so that you don't revisit them. At each step of the way, if the node you want to go to is in your list of visited nodes, you pretend it is null.

Another way is to save the last node you worked on and compare that to the nodes of your current node and if it is on the right, only then do you go left and if it is on the left, you go up. That is way more efficient, but more difficult.
Please log in or register to reply.
Live Events Refresh
PiG Sty Festival
12:00
PiGfest 8.0 Group B
ByuN vs HeRoMaRinE
ShoWTimE vs Zoun
PiGStarcraft1172
BRAT_OK 94
3DClanTV 63
LiquipediaDiscussion
Escore
10:00
Season 3 Week 7
escodisco2633
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
PiGStarcraft1172
mouzHeroMarine 379
Rex 136
BRAT_OK 94
RushiSC 30
StarCraft: Brood War
Britney 47201
EffOrt 864
actioN 342
Rush 149
Soma 124
Sexy 88
Pusan 72
Mong 69
Liquid`Ret 56
soO 44
[ Show more ]
ToSsGirL 42
HiyA 37
ZZZero.O 34
scan(afreeca) 22
Terrorterran 21
Rock 17
Sacsri 14
Dota 2
qojqva3962
BananaSlamJamma270
XaKoH 243
canceldota84
Counter-Strike
fl0m4955
zeus413
edward84
Other Games
singsing2126
hiko727
Beastyqt510
Liquid`VortiX220
Hui .175
ArmadaUGS94
Livibee80
Mew2King69
KnowMe53
QueenE43
Trikslyr2
Organizations
StarCraft 2
IntoTheiNu 1459
ComeBackTV 436
[ Show 17 non-featured ]
StarCraft 2
• mYiSmile118
• StrangeGG 4
• intothetv
• Kozan
• Migwel
• AfreecaTV YouTube
• IndyKCrew
StarCraft: Brood War
• Michael_bg 6
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• lizZardDota2183
League of Legends
• Jankos1760
• Nemesis1446
• TFBlade606
• Shiphtur290
Other Games
• WagamamaTV275
Upcoming Events
Big Brain Bouts
18m
Nicoract vs Neeb
TriGGeR vs ShoWTimE
CranKy Ducklings
18h 18m
PiG Sty Festival
20h 18m
herO vs Krystianer
Solar vs Cure
IPSL
1d
Sparkling Tuna Cup
1d 18h
PiG Sty Festival
1d 20h
Maru vs Shameless
Reynor vs TriGGeR
IPSL
2 days
OSC
2 days
Afreeca Starleague
2 days
Rush vs Hm
Bisu vs Shuttle
WardiTV Weekly
2 days
[ Show More ]
The Patches Monday
3 days
Afreeca Starleague
3 days
Sharp vs Shinee
Action vs Shine
PiGosaur Cup
4 days
Replay Cast
4 days
Afreeca Starleague
4 days
BeSt vs Paralyze
Jaedong vs Speed
Kung Fu Cup
4 days
The PondCast
5 days
KCM Race Survival
5 days
Replay Cast
6 days
PiG Sty Festival
6 days
Liquipedia Results

Completed

Proleague 2026-08-13
CranK Gathers Season 4: BW vs SC2 Team League
Eternal Conflict S2 Finale

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
Acropolis #5
Escore Tournament S3: W7
RSL Revival: Season 6
PiG Sty Festival 8.0
Esports World Cup 2026
Esports World Cup 2026: LCQ
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026

Upcoming

CSLAN 4
ASL Season 22
Escore Tournament S3: W8
Acropolis #5 - TRS
Blizzard Classic Cup 2026
Acropolis #5 - GSA
Acropolis #5 - GSB
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
Big Dog Cup 2026 Div 1
META DYMY #4
Stake Ranked Episode 5
PGL Masters Bucharest 2026
Thunderpick World Champ.
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
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 © 2026 TLnet. All Rights Reserved.