• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 11:29
CET 17:29
KST 01:29
  • 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 !10Weekly 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
The Grack before Christmas Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies ComeBackTV's documentary on Byun's Career ! Micro Lags When Playing SC2? When will we find out if there are more tournament
Tourneys
$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 RSL Offline Finals Info - Dec 13 and 14!
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
BGH Auto Balance -> http://bghmmr.eu/ Recommended FPV games (post-KeSPA) BW General Discussion FlaSh on: Biggest Problem With SnOw's Playstyle soO on: FanTaSy's Potential Return to StarCraft
Tourneys
[Megathread] Daily Proleagues [BSL21] LB QuarterFinals - Sunday 21:00 CET Small VOD Thread 2.0 [BSL21] WB SEMIFINALS - Saturday 21:00 CET
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Current Meta Fighting Spirit mining rates
Other Games
General Games
Nintendo Switch Thread 2025 POECurrency Christmas POE 2 Update 0.4.0 Curr 2025 IGGM Merry Christmas ARC Raiders Items Sale 2025 IGGM Christmas Diablo 4 Season 11 Items Sale 2025 IGGM Monopoly Go Christmas Sale
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
The Games Industry And ATVI Russo-Ukrainian War Thread How Does UI/UX Design Influence User Trust? US Politics Mega-thread Things Aren’t Peaceful in Palestine
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
The (Hidden) Drug Problem in…
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: 1661 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 Invitational
12:00
Christmas Day Games
Reynor vs MaxPaxLIVE!
Solar vs Classic
herO vs TBD
WardiTV1972
TaKeTV 598
IndyStarCraft 283
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
IndyStarCraft 283
trigger 86
ProTech55
StarCraft: Brood War
Britney 36371
Rain 4978
Sea 1530
Shuttle 1140
EffOrt 621
actioN 491
Larva 397
ggaemo 271
Aegong 230
firebathero 192
[ Show more ]
Mini 186
Last 147
hero 85
Mind 85
Hyun 62
Sharp 43
PianO 40
[sc1f]eonzerg 34
ToSsGirL 33
Terrorterran 30
Shinee 27
Sexy 25
soO 16
Noble 12
910 12
SilentControl 11
HiyA 8
Dewaltoss 7
Dota 2
420jenkins1341
LuMiX1
League of Legends
C9.Mang0451
Other Games
singsing2385
crisheroes413
DeMusliM401
Mlord391
FrodaN347
ArmadaUGS170
Mew2King118
Organizations
Other Games
gamesdonequick591
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• Adnapsc2 5
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• 80smullet 7
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis5831
Upcoming Events
Big Brain Bouts
1d
Elazer vs Nicoract
Reynor vs Scarlett
Replay Cast
1d 7h
Sparkling Tuna Cup
2 days
Krystianer vs TBD
TriGGeR vs SKillous
Percival vs TBD
ByuN vs Nicoract
Replay Cast
3 days
Wardi Open
3 days
Liquipedia Results

Completed

KCM Race Survival 2025 Season 4
WardiTV 2025
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL Season 21
Slon Tour Season 2
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
ESL Pro League S22

Upcoming

CSL 2025 WINTER (S19)
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.