traverser.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2004-2005 by Daniel Clarke                              *
00003  *   daniel.jc@gmail.com                                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "traverser.h"
00022 #include "pic14.h"
00023 
00024 Traverser::Traverser(BTreeNode *root)
00025 {
00026         m_root = root;
00027         m_current = root;
00028 }
00029 
00030 Traverser::~Traverser()
00031 {
00032 }
00033 
00034 BTreeNode * Traverser::start()
00035 {
00036         /* To find the start we will iterate, or possibly recurse
00037         down the tree, each time turning down the node that has children,
00038         if they both have no children we have reached the end and it shouldn't
00039         really matter which we pick (check this algorithm) */
00040         
00041         BTreeNode *n = m_root;
00042         bool found = false;
00043         
00044         while(!found)
00045         {
00046                 if( !n->hasChildren() ) found = true;
00047                 else
00048                 {
00049                         if( !n->left()->hasChildren() )
00050                         {
00051                                 if( !n->right()->hasChildren() ) found = true;
00052                                 n = n->right();
00053                         }
00054                         else n = n->left();
00055                 }
00056         }
00057         //if(n->parent()) m_current = n->parent();
00058         //else m_current = n;
00059         m_current = n;
00060         return m_current;
00061 }
00062 
00063 BTreeNode * Traverser::next()
00064 {
00065         // a.t.m we will just take the next thing to be the parent.
00066         if( m_current != m_root ) m_current = m_current->parent();
00067         return m_current;
00068 }
00069 
00070 bool Traverser::onLeftBranch()
00071 {
00072         return current()->parent()->left() == current();
00073 }
00074 
00075 BTreeNode * Traverser::oppositeNode()
00076 {
00077         if ( onLeftBranch() )
00078                 return current()->parent()->right();
00079         else
00080                 return current()->parent()->left();
00081 }
00082 
00083 void Traverser::descendLeftwardToTerminal()
00084 {
00085         bool done = false;
00086         while(!done)
00087         {
00088                 if( !current()->hasChildren() ) return;
00089                 else
00090                 {
00091                         m_current = current()->left();
00092                 }
00093         }
00094 }
00095 
00096 void Traverser::moveToParent()
00097 {
00098         if(current()->parent()) m_current = current()->parent();
00099 }
00100 

Generated on Tue May 8 17:05:33 2007 for KTechLab by  doxygen 1.5.1