Академический Документы
Профессиональный Документы
Культура Документы
// Contenido:
// Clase abstracta para arboles n-arios nTree<T, TNode>.
//
// Clase de arboles n-arios basada en nodos binarios nTreeNode<T>
//
//-----------------------------------------------------------------------
------
#ifndef __EDI_N_TREE_H
#define __EDI_N_TREE_H
#include <iostream>
#include <stdexcept>
#include <stdarg.h>
#include <_iterator.h>
#include <_stack.h>
#include <_binaryNode.h>
//----------------------------------------------------------------
//
// Definicion de la clase nTree
//
//----------------------------------------------------------------
//----------------------------------------------------------------
//
// Definicion de la clase nTreeNode
//
//----------------------------------------------------------------
//
// Clase nTreeNode
// Clase de los arboles n-arios basada en nodos binarios
//
public:
// constructores
nTreeNode (const T & value);
nTreeNode (binaryNode<T> * root = (binaryNode<T> *) 0);
nTreeNode (const T & value, unsigned int grade, nTreeNode<T> * first
...);
/* Necesita: la etiqueta de la raz del rbol, el grado de
la raz del rbol (>0) y las direcciones de los
subrboles hijos de la raz.
Produce: un nuevo rbol n-ario con la raz etiquetada
con el valor value y que tiene como hijos las
races de los subrboles dados. */
// destructor
virtual ~nTreeNode ();
// operaciones
virtual T label (binaryNode<T> * node) const;
//0(1)
virtual binaryNode<T> * root () const;
//0(1)
virtual binaryNode<T> * parent (binaryNode<T> * node) const;
//0(n)
virtual binaryNode<T> * leftMostChild(binaryNode<T> * node) const;
//0(1)
virtual binaryNode<T> * rightSibling (binaryNode<T> * node) const;
//0(1)
virtual nTreeNode<T> & subTree (binaryNode<T> * node) const;
//0(1)
virtual bool isLeaf (binaryNode<T> * node) const;
//0(1)
protected:
// area de datos
binaryNode<T> * theRoot;
// operaciones internas
binaryNode<T> * search (binaryNode<T> * initial, binaryNode<T> * node)
const;
/* Necesita: las direcciones de dos nodos binarios, node e
initial. El nodo initial es el nodo inicial a partir del
cual se busca el nodo node.
Produce: el nodo padre del nodo node.
Excepcin: si no se encuentra node retorna el nodo nulo.
Coste t.: O(n) */
//----------------------------------------------------------------
//
// Implementacion de la clase nTreeNode
//
//----------------------------------------------------------------
childs.push(first);
for (unsigned int i = 1; i != grade; i++) {
nTreeNode<T> * t = va_arg(lArgs, nTreeNode<T> *);
childs.push(t);
}
theRoot = tree->theRoot;
}
while (! childs.empty()) {
binaryNode<T> * root = childs.top()->theRoot;
childs.pop();
binaryNode<T> * left = root->left();
if (left)
left = left->clone();
if (rhs.theRoot)
theRoot = rhs.theRoot->clone();
else
theRoot = 0;
}
if (theRoot) {
theRoot->free();
delete theRoot;
}
if (rhs.theRoot)
theRoot = rhs.theRoot->clone();
else
theRoot = 0;
return *this;
}
if (theRoot) {
theRoot->free();
delete theRoot;
theRoot = 0;
}
}
return node->label();
}
return theRoot;
}
assertNullNode(node);
if (theRoot == node)
return 0;
assertNullNode(node);
return node->left();
}
template<class T>
binaryNode<T> * nTreeNode<T>::rightSibling (binaryNode<T> * node) const
{
// retorna el hermano mas cercano de un nodo
assertNullNode(node);
return node->right();
}
assertNullNode(node);
return *tree;
}
assertNullNode(node);
return node->left() == 0;
}
while (child) {
binaryNode<T> * parent = search(child, node);
if (parent)
return parent;
if (child == node)
return initial;
child = rightSibling(child);
}
return 0;
}
//-------------------------------------------------------------------
// Operadores de Entrada/Salida
//-------------------------------------------------------------------
return in;
}
in >> p->theRoot;
t = p;
return in;
}
return out;
}
return out;
}
#endif