Created
May 12, 2014 13:22
-
-
Save adyatlov/643a14d1c9f6f7b4ad71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TreeItem findStack2(TreeItem root, Object obj) { | |
maxStackSize = 0; | |
Deque<Iterator<TreeItem>> stack = new ArrayDeque<Iterator<TreeItem>>(); | |
stack.push(Collections.singletonList(root).iterator()); | |
while (!stack.isEmpty()) { | |
Iterator<TreeItem> it = stack.peek(); | |
if (it.hasNext()) { | |
TreeItem item = it.next(); | |
if (Objects.equals(item.getData(), obj)) { | |
return item; | |
} | |
stack.push(item.getChildren().iterator()); | |
maxStackSize = Math.max(maxStackSize, stack.size()); | |
} else { | |
stack.pop(); | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment