Esempio n. 1
0
int BST::recursiveInsert(const int &val, BSTNode * node){
    // if(node == nullptr){
    // 	node->data = val;
    // 	return val;
    // }
    if(node->data == val){
	return 0;
    }
    if(node->data > val){
	if(node->left == nullptr){
	    node->left = new BSTNode();
	    node->left->data = val;
	    node->left->parent = node;
	    ++ size;
	    return val;
	}else{
	    return recursiveInsert(val, node->left);   
	}
    }else{
	if(node->right == nullptr){
	    node->right = new BSTNode();
	    node->right->data = val;
	    node->right->parent = node;
	    ++ size;
	    return val;	    
	}else{
	    return recursiveInsert(val, node->right);
	}
    }
}
Esempio n. 2
0
int BST::insert(int val){
    if(root == nullptr){
	root = new BSTNode();
    	root->data = val;
    	return val;
    }    
    recursiveInsert(val, root);
}
/* Recursive function to insert node into list */
int recursiveInsert (Node * traversalNode, int dataToInsert){
	if(traversalNode == 0){
		traversalNode = malloc(sizeof(Node));
		traversalNode->data = dataToInsert;
		traversalNode->leftNode = 0;
		traversalNode->rightNode = 0;
		return 1;
	}
	if(traversalNode->data > dataToInsert){
		return recursiveInsert(traversalNode->rightNode,dataToInsert);
	}else if(traversalNode->data < dataToInsert) {
		return recursiveInsert(traversalNode->leftNode,dataToInsert);
	}
	else {
		return 0;
	}
}
void C_galgas_type_descriptor::recursiveInsert (C_galgas_type_descriptor * & ioRootPtr,
                                                C_galgas_type_descriptor * inDescriptor,
                                                bool & ioExtension) {
  if (ioRootPtr == NULL) {
    ioExtension = true ;
    ioRootPtr = inDescriptor ;
  }else{
    const int32_t comparaison = strcmp (ioRootPtr->mGalgasTypeName, inDescriptor->mGalgasTypeName) ;
    if (comparaison > 0) {
      recursiveInsert (ioRootPtr->mPreviousType, inDescriptor, ioExtension) ;
      if (ioExtension) {
        ioRootPtr->mBalance++;
        if (ioRootPtr->mBalance == 0) {
          ioExtension = false;
        }else if (ioRootPtr->mBalance == 2) {
          if (ioRootPtr->mPreviousType->mBalance == -1) {
            rotateLeft (ioRootPtr->mPreviousType) ;
          }
          rotateRight (ioRootPtr) ;
          ioExtension = false;
        }
      }
    }else if (comparaison < 0) {
      recursiveInsert (ioRootPtr->mNextType, inDescriptor, ioExtension) ;
      if (ioExtension) {
        ioRootPtr->mBalance-- ;
        if (ioRootPtr->mBalance == 0) {
          ioExtension = false ;
        }else if (ioRootPtr->mBalance == -2) {
          if (ioRootPtr->mNextType->mBalance == 1) {
            rotateRight (ioRootPtr->mNextType) ;
          }
          rotateLeft (ioRootPtr) ;
          ioExtension = false;
        }
      }
    }else{
      ioExtension = false;
      C_String errorMessage ;
      errorMessage << "FATAL ERROR (type '@" << inDescriptor->mGalgasTypeName << "' already defined)" ;
      fatalError (errorMessage, __FILE__, __LINE__) ;
    }
  }
}
C_galgas_type_descriptor::C_galgas_type_descriptor (const char * inGalgasTypeName,
                                                    const C_galgas_type_descriptor * inSuperClassDescriptor) :
mNextType (NULL),
mPreviousType (NULL),
mBalance (0),
mGalgasTypeName (inGalgasTypeName),
mSlotID (gSlotID),
mSuperclassDescriptor (inSuperClassDescriptor) {
  gSlotID ++ ;
  bool extension = false ; // Unused here
  recursiveInsert (gGalgasTypeListRoot, this, extension) ;
}
int BTInsert (BinaryTreePtr btp, int a) {
	
	if (btp->head == 0) {
		Node * newNode = malloc(sizeof(Node));
		newNode->data = a;
		newNode->leftNode = 0;
		newNode->rightNode = 0;
		return 1;
	} else {
		Node * traversalNode = btp->head;
		recursiveInsert(traversalNode);
	}
}
Esempio n. 7
0
bool BPlusTree::add(Record* record) {

	/* Si el tamanio del registro que quiero insertar es mayor al porcentaje TreeConstraits::getPercentRecord() del nodo, lanzo una excepcion */
	if ((int)record->getSize() > (TreeConstraits::getEfectiveSizeNode() * TreeConstraits::getPercentRecord() / 100) || record->getKey()->getSize() > keyTopSize)
		throw new exception;

	Node* newChildNode = NULL;
	Key toPromoteKey;
	if (root == NULL) {
		root = createLeafNode();
		root->number = firstLeaf = 0;
		serializeDataConfig();
		(static_cast <LeafNode*> (root))->nextLeaf = 0;
		this->NodesMount = 1;
	}

	bool result = recursiveInsert(root, *(record->getKey()), *(record->getData()), &toPromoteKey, &newChildNode);

	if (newChildNode) {
		persistNode(newChildNode);
		InnerNode* newRoot = createInnerNode(root->level + 1);

		// Muevo la raiz a otra posicion y persisto la nueva raiz en la posicion cero
		root->number = getNodeNumberToSeize();
		if (root->isLeaf()) {
			firstLeaf = root->number;
			serializeDataConfig();
		}
		persistNode(root);

		newRoot->keys[0] = toPromoteKey;
		newRoot->sons[0] = root->number;
		newRoot->sons[1] = newChildNode->number;
		newRoot->keyMount = 1;
		newRoot->occupiedSpace += toPromoteKey.getSize() + TreeConstraits::getControlSizeRecord();
		newRoot->number = 0;
		persistNode(newRoot);

		freeNodeMemory(root);
		freeNodeMemory(newChildNode);
		root = newRoot;
	} else {
		persistNode(root);
	}
	return result;
}
Esempio n. 8
0
void FolderWizardTargetPage::slotUpdateDirectories(QStringList list)
{
    QString webdavFolder = QUrl(ownCloudInfo::instance()->webdavUrl()).path();

    QTreeWidgetItem *root = _ui.folderTreeWidget->topLevelItem(0);
    if (!root) {
        root = new QTreeWidgetItem(_ui.folderTreeWidget);
        root->setText(0, Theme::instance()->appNameGUI());
        root->setIcon(0, Theme::instance()->applicationIcon());
        root->setToolTip(0, tr("Choose this to sync the entire account"));
        root->setData(0, Qt::UserRole, "/");
    }
    foreach (QString path, list) {
        path.remove(webdavFolder);
        QStringList paths = path.split('/');
        if (paths.last().isEmpty()) paths.removeLast();
        recursiveInsert(root, paths, path);
    }
Esempio n. 9
0
void FolderWizardTargetPage::slotUpdateDirectories(QStringList list)
{
    QFileIconProvider prov;
    QIcon folderIcon = prov.icon(QFileIconProvider::Folder);

    QString webdavFolder = QUrl(ownCloudInfo::instance()->webdavUrl()).path();
    connect(_ui.folderTreeWidget, SIGNAL(itemExpanded(QTreeWidgetItem*)), SLOT(slotItemExpanded(QTreeWidgetItem*)));

    QTreeWidgetItem *root = _ui.folderTreeWidget->topLevelItem(0);
    if (!root) {
        root = new QTreeWidgetItem(_ui.folderTreeWidget);
        root->setText(0, tr("Root (\"/\")", "root folder"));
        root->setIcon(0, folderIcon);
        root->setToolTip(0, tr("Choose this to sync the entire account"));
        root->setData(0, Qt::UserRole, "/");
    }
    foreach (QString path, list) {
        path.remove(webdavFolder);
        QStringList paths = path.split('/');
        if (paths.last().isEmpty()) paths.removeLast();
        recursiveInsert(root, paths, path);
    }
Esempio n. 10
0
static void recursiveInsert(QTreeWidgetItem *parent, QStringList pathTrail, QString path)
{
    QFileIconProvider prov;
    QIcon folderIcon = prov.icon(QFileIconProvider::Folder);
    if (pathTrail.size() == 0) {
        if (path.endsWith('/')) {
            path.chop(1);
        }
        parent->setToolTip(0, path);
        parent->setData(0, Qt::UserRole, path);
    } else {
        QTreeWidgetItem *item = findFirstChild(parent, pathTrail.first());
        if (!item) {
            item = new QTreeWidgetItem(parent);
            item->setIcon(0, folderIcon);
            item->setText(0, pathTrail.first());
            item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
        }

        pathTrail.removeFirst();
        recursiveInsert(item, pathTrail, path);
    }
}
Esempio n. 11
0
bool BPlusTree::recursiveInsert(Node* nodoCorriente, Key clave, ByteString dato, Key* clavePromocion, Node** nuevoNodo) {

	if (!nodoCorriente->isLeaf()) {

		InnerNode *nodoInteriorCorriente = static_cast<InnerNode*> (nodoCorriente);
		Key nuevaClave;
		Node* nuevoNodoHijo = NULL;
		int posicion = getPosition(nodoInteriorCorriente, clave);
		Node* nodoHijo = hidratateNode(nodoInteriorCorriente->sons[posicion]);

		bool resultado = recursiveInsert(nodoHijo, clave, dato, &nuevaClave, &nuevoNodoHijo);

		if (nuevoNodoHijo) {

			if (nodoInteriorCorriente->isOverflow(nuevaClave.getSize() + TreeConstraits::getControlSizeRecord() + keyTopSize)) {

				splitInnerNode(nodoInteriorCorriente, clavePromocion, nuevoNodo, posicion);

				if (posicion == nodoInteriorCorriente->keyMount + 1
						&& nodoInteriorCorriente->keyMount < (*nuevoNodo)->keyMount) {

					InnerNode *nuevoNodoInterior = static_cast<InnerNode*> (*nuevoNodo);
					nodoInteriorCorriente->keys[nodoInteriorCorriente->keyMount] = *clavePromocion;
					nodoInteriorCorriente->sons[nodoInteriorCorriente->keyMount + 1] = nuevoNodoInterior->sons[0];
					nodoInteriorCorriente->keyMount++;
					nodoInteriorCorriente->occupiedSpace += (*clavePromocion).getSize() + TreeConstraits::getControlSizeRecord();
					nuevoNodoInterior->sons[0] = nuevoNodoHijo->number;
					*clavePromocion = nuevaClave;

					persistNode(nuevoNodoHijo);
					freeNodeMemory(nuevoNodoHijo);

					persistNode(nodoHijo);
					freeNodeMemory(nodoHijo);

					return resultado;

				} else {

					if (posicion >= nodoInteriorCorriente->keyMount + 1) {
						posicion -= (nodoInteriorCorriente->keyMount + 1);
						nodoInteriorCorriente = static_cast<InnerNode*> (*nuevoNodo);
					}
				}
			}

			int i = nodoInteriorCorriente->keyMount;
			while (i > posicion) {
				nodoInteriorCorriente->keys[i] = nodoInteriorCorriente->keys[i - 1];
				nodoInteriorCorriente->sons[i + 1] = nodoInteriorCorriente->sons[i];
				i--;
			}
			nodoInteriorCorriente->keys[posicion] = nuevaClave;
			nodoInteriorCorriente->sons[posicion + 1] = nuevoNodoHijo->number;
			nodoInteriorCorriente->keyMount++;
			nodoInteriorCorriente->occupiedSpace += nuevaClave.getSize() + TreeConstraits::getControlSizeRecord();

			persistNode(nuevoNodoHijo);
			freeNodeMemory(nuevoNodoHijo);
		}
		persistNode(nodoHijo);
		freeNodeMemory(nodoHijo);

		return resultado;

	} else {

		LeafNode *nodoHojaCorriente = static_cast<LeafNode*> (nodoCorriente);
		int posicion = getPosition(nodoHojaCorriente, clave);

		// chequea que no exista la clave
		if (posicion < nodoHojaCorriente->keyMount && equalKey(clave, nodoHojaCorriente->keys[posicion])) {
			return false;
		}

		int i = nodoHojaCorriente->keyMount - 1;
		while (i >= 0 && minorKey(clave, nodoHojaCorriente->keys[i])) {
			nodoHojaCorriente->keys[i + 1] = nodoHojaCorriente->keys[i];
			nodoHojaCorriente->byteData[i + 1] = nodoHojaCorriente->byteData[i];
			i--;
		}
		nodoHojaCorriente->keys[i + 1] = clave;
		nodoHojaCorriente->byteData[i + 1] = dato;
		nodoHojaCorriente->keyMount++;
		nodoHojaCorriente->occupiedSpace += dato.getSize() + clave.getSize() + TreeConstraits::getControlSizeRecord();

		if (nodoHojaCorriente->isOverflow(keyTopSize)) {

			splitLeafNode(nodoHojaCorriente, clavePromocion, nuevoNodo);

			if (posicion >= nodoHojaCorriente->keyMount) {
				posicion -= nodoHojaCorriente->keyMount;
				nodoHojaCorriente = static_cast<LeafNode*> (*nuevoNodo);
			}
		}

		if (nuevoNodo && nodoHojaCorriente != *nuevoNodo && posicion == nodoHojaCorriente->keyMount - 1) {
			*clavePromocion = clave;
		}

		return true;
	}
}
Esempio n. 12
0
IntervalTreeNode* IntervalTree::insert(SimpleInterval* new_interval)
{
  IntervalTreeNode* y;
  IntervalTreeNode* x;
  IntervalTreeNode* new_node;

  x = new IntervalTreeNode(new_interval);
  recursiveInsert(x);
  fixupMaxHigh(x->parent);
  new_node = x;
  x->red = true;
  while(x->parent->red)
  {
    /// use sentinel instead of checking for root
    if(x->parent == x->parent->parent->left)
    {
      y = x->parent->parent->right;
      if(y->red)
      {
        x->parent->red = true;
        y->red = true;
        x->parent->parent->red = true;
        x = x->parent->parent;
      }
      else
      {
        if(x == x->parent->right)
        {
          x = x->parent;
          leftRotate(x);
        }
        x->parent->red = false;
        x->parent->parent->red = true;
        rightRotate(x->parent->parent);
      }
    }
    else
    {
      y = x->parent->parent->left;
      if(y->red)
      {
        x->parent->red = false;
        y->red = false;
        x->parent->parent->red = true;
        x = x->parent->parent;
      }
      else
      {
        if(x == x->parent->left)
        {
          x = x->parent;
          rightRotate(x);
        }
        x->parent->red = false;
        x->parent->parent->red = true;
        leftRotate(x->parent->parent);
      }
    }
  }
  root->left->red = false;
  return new_node;
}