class DecisionNode {

	private String question;
	
	private DecisionNode yes, no;
	
	public DecisionNode ( String q ) {
		question = q;
		yes = null;
		no = null;
	}
	
	public DecisionNode getYes() {
		return yes;
	}
	
	public DecisionNode getNo() {
		return no;
	}
	
	public void setYes( DecisionNode yes ) {
		this.yes = yes;
	}
	
	public void setNo( DecisionNode no ) {
		this.no = no;
	}	
	
	public boolean isLeaf() {
		return ( yes == null ) && ( no == null );
	}
	
	public String toString() {
		return question;
	}	
	
	public boolean equals( DecisionNode d ) {
		return question.equals( d.toString());
	}	
}

public class DecisionTree {
	
	private DecisionNode root, current, parent;
	private boolean lastdecision;
	
	public DecisionTree () {
		root = new DecisionNode( new String("Does it have legs?"));
		root.setYes( new DecisionNode( new String("Is it a cat?")));
		root.setNo( new DecisionNode( new String("Is it a snake?")));
		
		current = root;
		lastdecision = true;
	}
	
	public DecisionNode peek() {
		return current;
	}	
	
	public DecisionNode nextYes() {
		parent = current;
		current = current.getYes();
		lastdecision = true;
		return current;
	}
	
	public DecisionNode nextNo() {
		parent = current;
		current = current.getNo();
		lastdecision = false;
		return current;
	}	
	
	public boolean hasNext() {
		return !(current.isLeaf());
	}
	
	public void resetIterator() {
		current = root;
	}	

	// adds a question to the decision tree at the location of last bad guess
	// takes two strings the new question and its right answer
	// the current node is assume dto be the wrong answer to the question
	
	public void addDecision( String q, String a ) {
		DecisionNode subTree = new DecisionNode( q );
		DecisionNode animal = new DecisionNode( a );
		subTree.setYes( animal );
		subTree.setNo( current );
		if( lastdecision == true ) {
			parent.setYes( subTree );
		} else {
			parent.setNo( subTree );
		}
	}
	
	public String toString() {
		return printTree( root, 0 );
	}	
	
	protected String pad( int i ) {
	
	  StringBuffer b = new StringBuffer();
	  for( int j = i; j > 0; j-- ) {
	    b.append("  ");
	  }
	  return b.toString();
	}
	
	public String printTree( DecisionNode cursor, int i ) {
	
	  StringBuffer b = new StringBuffer();
	  if( cursor == null ) {
	    return b.toString();
	  }
		if( cursor != null ) {
		  b.append( pad( i )).append( "<question>");
		  b.append( cursor.toString());
		  b.append( "</question>\n");
		  b.append( pad( i )).append( "<yes>\n");
		  b.append( printTree( cursor.getYes(), i + 1 ));
		  b.append( pad( i )).append( "</yes>\n");
		  b.append( pad( i )).append( "<no>\n");
		  b.append( printTree( cursor.getNo(), i + 1 ));
		  b.append( pad( i )).append( "</no>\n");
		}
		return b.toString();
	}
	
	public static void main ( String [] argsv ) {
		DecisionTree d = new DecisionTree();
		System.out.println( d );
	}
}
