/// <summary> /// Método para buscar uma chave na estrutura /// </summary> /// <param name="key">Chave a ser buscada</param> /// <returns>Árvore contendo um nó com a chave, em caso de sucesso na busca, /// ou um nó com chave nula (nó onde a chave deveria estar)</returns> public override AbstractTree searchKey(Key key) { // Árvore a ser retornada. AbstractTree tree = null; // Verifica se a chave passada é nula. if (KEY != null) { // Compara o valor da chave passada com a do nó atual. switch (key.VALUE.CompareTo(KEY.VALUE)) { // Caso seja igual, retorna a própria referência. case 0: tree = this; break; // Caso seja maior, realiza a busca no filho da direita. case 1: tree = getRightChild().searchKey(key); break; // Caso seja menor, realiza a busca no filho da esquerda. case -1: tree = getLeftChild().searchKey(key); break; } } // Caso a chave seja nula, retorna a prorpia referência. else { tree = this; } // Retorna a árvole return(tree); }
/// <summary> /// Método para adicioanar uma árvore ao manipulador. /// </summary> /// <param name="treeName">Tipo de árvore a ser adicionada</param> public AbstractTree addTree(StructureType.Trees treeName, int ordem) { // Nova árvore criada AbstractTree newTree = factory.createObject(treeName); // Inicializa ordem, caso precise. newTree.init(ordem); // Flag de inserção. bool inserted = false; // Varre array para saber se já existe foreach (AbstractTree tree in trees) { // Verifica se já existe uma árvore deste tipo no array if (tree.GetType().FullName.Equals(newTree.GetType().FullName)) { // Caso haja, a antiga referência é removida. trees.Remove(tree); // E a nova inserida. trees.Add(newTree); inserted = true; break; } } // Se não foi inserido, adciona. if (!inserted) { // Adiciona nova entidade. trees.Add(newTree); } return(newTree); }
private AbstractTree searchKey(Key k, AbstractTree _subarvore) { B subarvore = (B)_subarvore; AbstractTree retorno = null; //Verificar se arvore vazia if (!subarvore.key.Count.Equals(0)) { //Procura no no atual e retorna o indice onde a chave está ou deveria estar int find = subarvore.searchNo(k, subarvore.key); switch (k.VALUE.CompareTo(((Key)subarvore.key[find]).VALUE)) { case 0: retorno = this; break; case 1: if (subarvore.pointer.Count > 0) { //Procura no nó da direita referenciado pelo ponteiro da direita retorno = searchKey((Key)k, (AbstractTree)subarvore.pointer[find + 1]); } break; case -1: if (subarvore.pointer.Count > 0) { retorno = searchKey((Key)k, (AbstractTree)subarvore.pointer[find]); } break; } //switch } //if no diferente de vazio return(retorno); }
/// <summary> /// Método para inserir um valor nas árvores. /// </summary> /// <param name="key">Chave a ser inserida.</param> public void insert(String key) { // Varre o array inserido em todas as estruturas. for (int i = 0; i < trees.Count; i++) { AbstractTree tree = (AbstractTree)trees[i]; // Insere a chave em uma determinada árvore. tree.insertKey(new Key(key)); } }
public void DecomposeTree(AbstractTree parentNode, AbstractTree node, TreeBranch branch, TreePath path) { if (!path.IsAdded) { Possibilities.Add(path); path.IsAdded = true; } // Recursive browse if (node is TreeConnector) { TreeConnector treeConnector = (TreeConnector)node; if (treeConnector.Connection == "&") { DecomposeTree(treeConnector, treeConnector.LeftTree, TreeBranch.Left, path); DecomposeTree(treeConnector, treeConnector.RightTree, TreeBranch.Right, path); } else if (treeConnector.Connection == "|") { // In this case, parentNode is a TreeOperator if (parentNode != null) { // Left distribution TreePath clonedPathLeftDistribution = (TreePath)path.Clone(); TreeConnector parentTreeConnectorLeftDistribution = (TreeConnector)parentNode.Clone(); // Right distribution TreePath clonedPathRightDistribution = (TreePath)path.Clone(); TreeConnector parentTreeConnectorRightDistribution = (TreeConnector)parentNode.Clone(); if (branch == TreeBranch.Left) { parentTreeConnectorLeftDistribution.LeftTree = treeConnector.LeftTree; parentTreeConnectorRightDistribution.LeftTree = treeConnector.RightTree; } else if (branch == TreeBranch.Right) { parentTreeConnectorLeftDistribution.RightTree = treeConnector.LeftTree; parentTreeConnectorRightDistribution.RightTree = treeConnector.RightTree; } // Remove obsolete path Possibilities.Remove(path); // Browse recursively distributed tree ; the path must be different (by ref) if the parent operator is 'OR' DecomposeTree( parentTreeConnectorLeftDistribution, parentTreeConnectorLeftDistribution.LeftTree, TreeBranch.Left, parentTreeConnectorLeftDistribution.Connection == "|" ? (TreePath)clonedPathLeftDistribution.Clone() : clonedPathLeftDistribution ); DecomposeTree( parentTreeConnectorLeftDistribution, parentTreeConnectorLeftDistribution.RightTree, TreeBranch.Right, clonedPathLeftDistribution ); DecomposeTree( parentTreeConnectorRightDistribution, parentTreeConnectorRightDistribution.LeftTree, TreeBranch.Left, parentTreeConnectorLeftDistribution.Connection == "|" ? (TreePath)clonedPathRightDistribution.Clone() : clonedPathRightDistribution ); DecomposeTree( parentTreeConnectorRightDistribution, parentTreeConnectorRightDistribution.RightTree, TreeBranch.Right, clonedPathRightDistribution ); } // The operator is the root of the tree; we simply divide the path else { TreePath clonedLeftPath = (TreePath)path.Clone(); TreePath clonedRightPath = (TreePath)path.Clone(); // Remove obsolete path Possibilities.Remove(path); DecomposeTree(treeConnector, treeConnector.LeftTree, TreeBranch.Left, clonedLeftPath); DecomposeTree(treeConnector, treeConnector.RightTree, TreeBranch.Right, clonedRightPath); } } break; } // Leaf else if (node is TreeValue) { TreeValue treeValue = (TreeValue)node; path.Add(treeValue); } }
public TreeDecomposer(AbstractTree tree) { DecomposeTree(null, tree, TreeBranch.Unknown, new TreePath()); RemoveDuplicatePaths(); }