void KdTreeNode::insert (LineSegment *l) { switch (splitType) { case 0: if (l->p0 != splitAt && l->p1 != splitAt && XOrder(l->p0, splitAt) == 1 && XOrder(l->p1, splitAt) == 1) { insertLeft(l); } else if (l->p0 != splitAt && l->p1 != splitAt && XOrder(splitAt, l->p0) == 1 && XOrder(splitAt, l->p1) == 1) { insertRight(l); } else { LineSegment *l0, *l1; splitLineSegment(l, splitAt, splitType, &l0, &l1); insertLeft(l0); insertRight(l1); } break; case 1: if (l->p0 != splitAt && l->p1 != splitAt && YOrder(l->p0, splitAt) == 1 && YOrder(l->p1, splitAt) == 1) { insertLeft(l); } else if (l->p0 != splitAt && l->p1 != splitAt && YOrder(splitAt, l->p0) == 1 && YOrder(splitAt, l->p1) == 1) { insertRight(l); } else { LineSegment *l0, *l1; splitLineSegment(l, splitAt, splitType, &l0, &l1); insertLeft(l0); insertRight(l1); } break; } }
/*Insere um nodo na AVL*/ AVL insert(AVL p,void* unc_info , int *cresceu,int tipo_AVL) { if(tipo_AVL == Catalogo_C || tipo_AVL == Catalogo_P) { char* code = (char*)unc_info; if(p==NULL) { p = (AVL) malloc(sizeof(struct avl_node)); p->info = malloc(10*sizeof(char)); strcpy(p->info, code); p->right=p->left=NULL; p->bf=EH; *cresceu=1; } else { char* info = (char*)p->info; if(strncmp(code, info, strlen(code))<0) p=insertLeft(p,unc_info, cresceu,tipo_AVL); else p=insertRight(p,unc_info, cresceu,tipo_AVL); } } else { Comp compra = (Comp) unc_info; if(p==NULL) { Comp info = (Comp) p->info; p = (AVL) malloc(sizeof(struct avl_node)); p->info = (Comp) malloc(sizeof(struct compras)); info = p->info; info->codigo_produto = malloc(10*sizeof(char)); info->codigo_cliente = malloc(10*sizeof(char)); compracpy(p->info,compra); p->right=p->left=NULL; p->bf=EH; *cresceu=1; } else if(tipo_AVL == Compras_Ord_CP) { if(compracmpCP(compra,p->info) < 0) p=insertLeft(p,unc_info,cresceu,tipo_AVL); else p = insertRight(p,unc_info,cresceu,tipo_AVL); } else /* Compras_Ord_CC*/ { if(compracmpCC(compra,p->info) < 0) p=insertLeft(p,unc_info,cresceu,tipo_AVL); else p = insertRight(p,unc_info,cresceu,tipo_AVL); } } return p; }
/** * Insere nó à esquerda da última inserção, em uma posição adequada * Retorna ponteiro para o nó inserido * * Node* current: nó atual * Node* right: nó à direita do nó atual * int item: valor a ser inserido */ Node* insertLeft(Node* current, Node* right, int item){ if(current==NULL){ Node* newNode = (Node*)malloc(sizeof(Node)); if(!newNode)return right; newNode->item = item; newNode->nextNode = right; newNode->previousNode = NULL; right->previousNode = newNode; return newNode; } else if(current->item < item){ Node* newNode = (Node*)malloc(sizeof(Node)); if(!newNode)return current; newNode->item = item; newNode->nextNode = current->nextNode; newNode->previousNode = current; current->nextNode->previousNode = newNode; current->nextNode = newNode; return newNode; } else{ return insertLeft(current->previousNode,current,item); } }
void createByGenList(TNode *root, char *list) { TNode *temp; char *p; int i; int level = 0; root->left = 0; root->right = 0; for (p = list; *p == ')' && level == 1; p++) { switch(*p) { case '(': level += 1; ++p; insertLeft(temp, *p); break; case ',': break; case ')': level -= 1; break; default: if (level == 1) root->data = *p; else { temp = root->left; for (i = level; i > 1; i--) temp = temp->left; temp = insertRight(temp, *p); } break; } } }
bool ossimImageChain::insertLeft(ossimConnectableObject* newObj, const ossimId& id) { ossimConnectableObject* obj = findObject(id, false); if(obj) { return insertLeft(newObj, obj); } return false; }
void ossimSingleImageChain::addScalarRemapper() { if ( !m_scalarRemapper ) { m_scalarRemapper = new ossimScalarRemapper(); if ( m_resamplerCache.valid() ) { // Add to the left of the resampler cache. insertLeft(m_scalarRemapper.get(), m_resamplerCache.get()); } else { // Add to the end of the chain. addFirst(m_scalarRemapper.get()); } } }
/** * Insere nó de forma ordenada * retorna ponteiro para o nó inserido * * Node* middle: ponteiro para o nó da última inserção * (não necessariamente presente no meio da lista) * int item: valor a ser inserido no novo nó */ Node* insertOrderedNode(Node* middle, int item){ if(middle==NULL){ Node* newNode = (Node*)malloc(sizeof(Node)); newNode->item = item; newNode->nextNode = NULL; newNode->previousNode = NULL; return newNode; } else{ if(middle->item > item){ return insertLeft(middle->previousNode,middle,item); } else{ return insertRight(middle->nextNode,middle,item); } } }
Tree insertTree(Tree t, TreeEntry e, int *cresceu) { if(!t) { t = (Tree) malloc(sizeof(struct treenode)); t -> entry = e; t -> left = NULL; t -> right = NULL; t -> bf = EH; *cresceu = 1; } else if(e == t->entry) printf("\n[error] %d already in tree.\n",e); else if(e > t->entry) t = insertRight(t, e, cresceu); else t = insertLeft(t, e, cresceu); return(t); }
int main(int argc, const char * argv[]) { DQUEUE_p_t queue = (DQUEUE_p_t)calloc(SIZE, sizeof(DQUEUE_t)); initQueue (queue); char choice; do { printf("\n------------------------------------------------------------\n | 1. Insert Left\n | 2. Insert Right\n | 3. Delete Left\n | 4. Delete Right\n | 5. Display Queue.\n | Q. Quit\n | Enter choice : "); choice = getchar(); getchar(); char * item = (char *)malloc(SIZE * sizeof(char)); if (choice == '1') { printf("\n | Enter item to be inserted: "); fgets(item, SIZE, stdin); insertLeft(queue, item); } else if (choice == '2') { printf("\n | Enter item to be inserted: "); fgets(item, SIZE, stdin); insertRight(queue, item); } else if (choice == '3') { item = deleteLeft(queue); if (item != UNDERFLOW_CHAR) printf("\n | Deleted item: %s\n", item); } else if (choice == '4') { item = deleteRight(queue); if (item != UNDERFLOW_CHAR) printf("\n | Deleted item: %s\n", item); } display(*queue); } while (choice >= '1' && choice <= '5'); return 0; }
/* *Create - sub tree of current node: *Logic: if: there exist left child * insertLeft * else: * make leftChild as NULL * if : there exist right child * insertRight * else: * make rightChild as NULL * call recursively for each node */ void createSubTree(node *ptr){ char ch; printf("Sub tree of %d\n",ptr->data); fflush(stdin); getchar(); printf("Is left child 'y':"); scanf("%c",&ch); if(ch == 'y' || ch == 'Y') insertLeft(ptr); printf("Is right child 'y':"); fflush(stdin); getchar(); scanf("%c",&ch); if(ch == 'y' || ch == 'Y') insertRight(ptr); if(ptr->left != NULL){ printf("\nCreate sub tree of %d:\n",ptr->left->data); createSubTree(ptr->left); } if(ptr->right != NULL){ printf("\nCreate sub tree of %d:\n",ptr->right->data); createSubTree(ptr->right); } }