• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 11:16
CEST 17:16
KST 00: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
Weekly Cups (July 13-19): Terran & Protoss rise; Zerg falters0Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7
Community News
Balance hotfix patch 5.0.16b (July 16)65Reynor: GSL Loss Wasn't About Preparation Format16[IPSL] Spring 2026 Grand Finals - This Weekend!18Weekly Cups (July 6 - 12): Protoss strike back12BSL Season 22 Full Overview & Conclusion8
StarCraft 2
General
Balance hotfix patch 5.0.16b (July 16) How would you feel about frequent/monthly balance patches for SC2? Weekly Cups (July 13-19): Terran & Protoss rise; Zerg falters Clem: "I don't have that much hope in Blizzard" [D] Wireframe Casting Removed
Tourneys
RSL Revival: Season 6 - Qualifiers and Main Event Master Swan Open (Global Bronze-Master 2) WardiTV Summer Cup 2026 GSL CK #5 Race War HomeStory Cup 29
Strategy
[G] Having the right mentality to improve
Custom Maps
New Map Maker - Looking for Advice - Love or Hate Work In Progress Melee Maps [D]RTS in all its shapes and glory <3
External Content
Mutation # 535 Assembly of Vengeance The PondCast: SC2 News & Results Mutation # 534 Burning Evacuation Mutation # 533 Die Together
Brood War
General
screpdb: new Starcraft reporting tool ASL22 General Discussion Animated Gateway NaDa's Body BW General Discussion
Tourneys
[IPSL] Spring 2026 Grand Finals - This Weekend! [Megathread] Daily Proleagues Escore Tournament - Season 3 Small VOD Thread 2.0
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Creating a full chart of Zerg builds Relatively freeroll strategies
Other Games
General Games
ZeroSpace at Steam NextFest - Last free demo Path of Exile Nintendo Switch Thread Stormgate/Frost Giant Megathread General RTS Discussion Thread
Dota 2
Looking for a Dota Mentor 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
TL Mafia
TL Mafia Power Rank NeO.D_StephenKing vs This Guy From 1 Million Dance TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread How to buy a book - shipping from Korea to Europe The Games Industry And ATVI UK Politics Mega-thread
Fan Clubs
The IdrA Fan Club The HerO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023 McBoner: A hockey love story Tennis[sport]
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
Northern Ireland Global Starcraft The Automated Ban List
Blogs
Role of Gaming on Mental Hea…
TrAiDoS
ASL S22 English Commentary…
namkraft
Poker (part 2)
Nebuchad
An Exploration of th…
waywardstrategy
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 6763 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
WardiTV Weekly
11:00
WardiTV Mondays #96
WardiTV1523
IntoTheiNu 723
OGKoka 565
Rex146
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
OGKoka 565
Ryung 240
Rex 146
SHIN 120
RushiSC 41
StarCraft: Brood War
Rain 3742
Shuttle 2291
Bisu 1981
Soulkey 847
EffOrt 784
Soma 764
Mini 704
BeSt 494
Light 364
firebathero 352
[ Show more ]
ZerO 293
Larva 263
Stork 239
actioN 202
Rush 176
Snow 118
Dewaltoss 116
Hyun 105
Pusan 94
ggaemo 74
soO 44
ToSsGirL 37
Free 34
scan(afreeca) 34
910 27
Terrorterran 26
Sexy 25
JulyZerg 25
yabsab 24
HiyA 23
Hm[arnc] 18
Aegong 18
Sacsri 17
Noble 12
ajuk12(nOOB) 11
Bale 9
IntoTheRainbow 7
Dota 2
qojqva2667
syndereN393
420jenkins84
League of Legends
Trikslyr31
Counter-Strike
x6flipin537
Heroes of the Storm
Khaldor171
Other Games
singsing2913
B2W.Neo988
Lowko434
crisheroes315
Liquid`VortiX228
Hui .186
KnowMe138
ToD136
QueenE41
Livibee31
ZerO(Twitch)18
Organizations
Other Games
gamesdonequick2353
BasetradeTV254
StarCraft: Brood War
Kim Chul Min (afreeca) 8
lovetv 8
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 16 non-featured ]
StarCraft 2
• mYiSmile129
• Adnapsc2 10
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• blackmanpl 15
• HerbMon 12
• Michael_bg 6
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis3946
Upcoming Events
Monday Night Weeklies
44m
PiGosaur Cup
1d 8h
The PondCast
1d 18h
Kung Fu Cup
1d 19h
OSC
2 days
CrankTV Team League
2 days
Replay Cast
3 days
CrankTV Team League
3 days
Korean StarCraft League
4 days
RSL Revival
4 days
Clem vs ByuN
Serral vs SHIN
[ Show More ]
Online Event
4 days
Replay Cast
5 days
RSL Revival
5 days
herO vs Solar
Rogue vs Lambo
WardiTV Weekly
6 days
Liquipedia Results

Completed

IPSL Spring 2026
HSC XXIX
Eternal Conflict S2 E3

Ongoing

Acropolis #4
CSL 2026 Summer (S21)
KCM Race Survival 2026 Season 3
RSL Revival: Season 6
CranK Gathers Season 4: BW vs SC2 Team League
SCTL 2026 Spring
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026

Upcoming

Escore Tournament S3: W4
ASL S22 SEASON OPEN Day 2
Escore Tournament S3: W5
CSLAN 4
Blizzard Classic Cup 2026
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
Light Tournament 2026
Eternal Conflict S2 Finale
Stake Ranked Episode 4
Logitech G Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer 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 © 2026 TLnet. All Rights Reserved.