• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 22:09
CET 04:09
KST 12:09
  • 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: Winners4Intel 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!21$5,000+ WardiTV 2025 Championship5[BSL21] RO32 Group Stage3Weekly Cups (Oct 26-Nov 2): Liquid, Clem, Solar win; LAN in Philly2Weekly Cups (Oct 20-26): MaxPax, Clem, Creator win9
StarCraft 2
General
TL.net Map Contest #21: Winners Starcraft, SC2, HoTS, WC3, returning to Blizzcon! RotterdaM "Serral is the GOAT, and it's not close" Weekly Cups (Oct 20-26): MaxPax, Clem, Creator win 5.0.15 Patch Balance Hotfix (2025-10-8)
Tourneys
Constellation Cup - Main Event - Stellar Fest $5,000+ WardiTV 2025 Championship Merivale 8 Open - LAN - Stellar Fest Sea Duckling Open (Global, Bronze-Diamond) $3,500 WardiTV Korean Royale S4
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection Mutation # 495 Rest In Peace
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ SnOw's ASL S20 Finals Review [BSL21] RO32 Group Stage Practice Partners (Official) [ASL20] Ask the mapmakers — Drop your questions
Tourneys
[Megathread] Daily Proleagues [BSL21] RO32 Group B - Sunday 21:00 CET [BSL21] RO32 Group A - Saturday 21:00 CET BSL21 Open Qualifiers Week & CONFIRM PARTICIPATION
Strategy
Current Meta How to stay on top of macro? PvZ map balance Soma's 9 hatch build from ASL Game 2
Other Games
General Games
Stormgate/Frost Giant Megathread Dawn of War IV Nintendo Switch Thread ZeroSpace Megathread General RTS Discussion Thread
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
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine YouTube Thread Dating: How's your luck?
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
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 Recent Gifted Posts
Blogs
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Why we need SC3
Hildegard
Career Paths and Skills for …
TrAiDoS
Reality "theory" prov…
perfectspheres
Our Last Hope in th…
KrillinFromwales
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1756 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
Replay Cast
23:00
WardiTV Mondays #58
LiquipediaDiscussion
OSC
22:00
Masters Cup 150 Open Qual
davetesta59
Liquipedia
LAN Event
18:00
Day 3: Ursa 2v2, FFA
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RuFF_SC2 192
NeuroSwarm 143
ProTech129
StarCraft: Brood War
Calm 7475
Artosis 671
Shuttle 609
actioN 389
Sharp 168
Noble 55
Icarus 2
Dota 2
LuMiX1
Other Games
tarik_tv12842
summit1g9733
JimRising 332
WinterStarcraft250
C9.Mang0227
ViBE165
FrodaN127
Organizations
Other Games
gamesdonequick890
Counter-Strike
PGL128
Other Games
BasetradeTV84
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Sammyuel 14
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota21440
League of Legends
• Stunt219
Other Games
• Scarra548
Upcoming Events
OSC
8h 51m
LAN Event
11h 51m
Korean StarCraft League
23h 51m
CranKy Ducklings
1d 6h
LAN Event
1d 11h
IPSL
1d 14h
dxtr13 vs OldBoy
Napoleon vs Doodle
BSL 21
1d 16h
Gosudark vs Kyrie
Gypsy vs Sterling
UltrA vs Radley
Dandy vs Ptak
Replay Cast
1d 19h
Sparkling Tuna Cup
2 days
WardiTV Korean Royale
2 days
[ Show More ]
LAN Event
2 days
IPSL
2 days
JDConan vs WIZARD
WolFix vs Cross
BSL 21
2 days
spx vs rasowy
HBO vs KameZerg
Cross vs Razz
dxtr13 vs ZZZero
Replay Cast
3 days
Wardi Open
3 days
WardiTV Korean Royale
4 days
Replay Cast
5 days
Kung Fu Cup
5 days
Classic vs Solar
herO vs Cure
Reynor vs GuMiho
ByuN vs ShoWTimE
Tenacious Turtle Tussle
5 days
The PondCast
6 days
RSL Revival
6 days
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
6 days
WardiTV Korean Royale
6 days
Liquipedia Results

Completed

BSL 21 Points
SC4ALL: StarCraft II
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
Stellar Fest: Constellation Cup
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

BSL Season 21
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: 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.