annotate src/treecms/proto/test/PreOrderTreeWalker.java @ 0:f815c7c1fb38

hg init
author shoshi
date Fri, 27 Aug 2010 15:26:20 +0900
parents
children 9d7863f367bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
1 package treecms.proto.test;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
2
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
3 import java.util.Iterator;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
4 import java.util.LinkedList;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
5
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
6 import treecms.proto.api.NodeAPI;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
7
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
8 public class PreOrderTreeWalker implements Iterator<NodeAPI> , Iterable<NodeAPI>
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
9 {
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
10 private NodeAPI m_root;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
11 private LinkedList<Iterator<NodeAPI>> m_childs;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
12
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
13 private int m_pos;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
14 public PreOrderTreeWalker(NodeAPI _root)
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
15 {
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
16 m_root = _root;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
17 m_childs = new LinkedList<Iterator<NodeAPI>>();
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
18
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
19 for(NodeAPI child : _root.getChildList()){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
20 m_childs.add((new PreOrderTreeWalker(child)).iterator());
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
21 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
22
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
23 m_pos = -2;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
24 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
25
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
26 @Override
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
27 public Iterator<NodeAPI> iterator() {
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
28 // TODO Auto-generated method stub
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
29 return this;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
30 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
31
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
32 @Override
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
33 public boolean hasNext() {
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
34 // TODO Auto-generated method stub
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
35 int next = m_pos + 1;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
36
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
37 if(next < 0){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
38 return true;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
39 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
40
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
41 for(;next < m_childs.size();next ++){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
42 System.out.println(m_pos);
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
43 if(m_childs.get(next).hasNext()){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
44 return true;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
45 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
46 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
47
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
48 return false;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
49 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
50
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
51 @Override
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
52 public NodeAPI next() {
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
53 // TODO Auto-generated method stub
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
54 m_pos++;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
55
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
56 if(m_pos < 0){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
57 return this.m_root;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
58 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
59
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
60 for(;m_pos < m_childs.size();m_pos ++){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
61 NodeAPI nextNode = m_childs.get(m_pos).next();
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
62 if(nextNode != null){
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
63 return nextNode;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
64 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
65 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
66
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
67 return null;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
68 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
69
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
70 @Override
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
71 public void remove() {
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
72 // TODO Auto-generated method stub
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
73
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
74 }
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
75 }