/****************************************************************************** *Function : main *Input parameters: no parameters *RETURNS : 0 on success ******************************************************************************/ int main(void) { bool ht_inc; int info,choice; NODEPTR root; root = NULL; while(1) { printf("1.Insert\n2.Display\n3.Quit\nEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1:printf("Enter the value to be inserted : "); scanf("%d", &info); if( search(root,info) == NULL ) root=insert(info, root, &ht_inc); else printf("Duplicate value ignored\n"); break; case 2:if(root==NULL) { printf("Tree is empty\n"); continue; } printf("Tree is :\n"); printf("Inorder Traversal is: "); inorder(root); printf("\n \n"); break; case 3:exit(1); default:printf("Wrong choice\n"); } } return 0; }
int main() { int i; int n; char in[1024]; char out[1024]; NodePointer root; printf("Input data by Reverse Polish Notation: "); scanf("%s", in); root = tree_by_revpol(in); n = 0; preorder(root, out, &n); printf("preorder: "); for ( i = 0; i < n; ++ i ) { printf("%c ", out[i]); } printf("\n"); n = 0; inorder(root, out, &n); printf("inorder: "); for ( i = 0; i < n; ++ i ) { printf("%c ", out[i]); } printf("\n"); n = 0; postorder(root, out, &n); printf("postorder: "); for ( i = 0; i < n; ++ i ) { printf("%c ", out[i]); } printf("\n"); return 0; }
int main() { int Q; scanf("%d",&Q); while (Q--) { char op[10]; scanf("%s",op); if (op[0]=='i') { int x; scanf("%d",&x); insert(root,x,0); } else if (op[0]=='f') { int x; scanf("%d",&x); printf("%s\n",find(root,x)?"yes":"no"); } else if (op[0]=='d') { int x; scanf("%d",&x); del(root,x); } else { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return(0); }
int main() { FILE * fin = fopen("input.txt", "r"); FILE * fout = fopen("output.txt", "w"); tree root; tree node; tree rear, front; int n, tmp; int i; fscanf(fin, "%d", &n); root = (tree)malloc(sizeof(*root)); root->next = NULL; root->parent = NULL; root->rc = NULL; root->lc = NULL; node = root; for ( i = 0 ; i < n ; i ++ ) { fscanf(fin, "%d", &tmp); insert(&root, &node, tmp); } front = root->next; rear = root->next; while(1) { if(rear->next != NULL) { rear = rear->next; if(rear->val != 0 ) { rear->parent = front; front->lc = rear; } } if(rear->next != NULL) { rear = rear->next; if(rear->val != 0) { rear->parent = front; front->rc = rear; } } front = front->next; if(front == NULL) { break; } } inorder((root->next), fout); rip(&root); //free(root); fclose(fin); fclose(fout); return 0; }
void BTree<T>::inorder() { inorder(manager.get_root()); cout << endl; }
void inorder(TreeNode *p) { if (!p) return; inorder(p->left); ptr.push_back(p); inorder(p->right); }
vector<int> inorderTraversal(TreeNode *root) { inorder(root); return nodes; }
void BSTree::inOrderPrint() { inorder(root); }//end inOrderPrint.
void main() { int choice; char ans = 'N'; int key; Nodo *nuevo_nodo, *raiz, *tmp; raiz = NULL; system("clear"); printf("\nPrograma para árbol de búsqueda binaria:"); do { printf("\n1.Insertar"); printf("\n2.Buscar"); printf("\n3.Listar árbol"); printf("\n4.Dibujar arbol"); printf("\n5.Exit"); printf("\nIndicar operación : "); scanf("%d", &choice); switch (choice) { case 1: do { nuevo_nodo = crear_nodo(1); printf("\nIngresar elemento : "); scanf("%d", &nuevo_nodo->clave); insertar(&raiz, &nuevo_nodo); printf("\nQuiere ingresar mas elementos?(y/n) : "); scanf(" %c", &ans); } while (ans == 'y'); break; case 2: printf("\nIngresar clave a ser buscada : "); scanf("%d", &key); tmp = buscar(&raiz, key); if(tmp == NULL) printf("\nNo existe la clave en el árbol!!!"); break; case 3: if (raiz == NULL) { printf("Arbol no está creado."); } else { printf("\nMostrar Inorder : "); inorder(&raiz); printf("\nMostrar Preorder : "); preorder(&raiz); printf("\nMostrar Postorder : "); postorder(&raiz); } break; case 4: dibujar_arbol(raiz); break; } } while (choice != 5); }
int main() { int i,num,ch; count=0; for(i=0;i<20;i++) ar[i]=0; do { printf("###Array implementation of a binary TREE###\n1.insert an element into the TREE\n2.delete a node from the TREE\n3.dispaly the nodes in the TREE\n4.search the nodes in the TREE\n5.exit\nENTER A ValID CHOICE TO PROCEED"); scanf("%d",&ch); switch (ch) { case 1: { printf("enter the element to be inserted:\n"); scanf("%d",&num); ar[count]=num; count++; } break; case 2: { printf("enter the element to be deleted:\n"); scanf("%d",&num); for(i=0;i<count;i++) { if (ar[i]==num) { count--; ar[i]=ar[count]; ar[count]=0; /* code */ } } /* code */ } break; case 4: { printf("enter the element to searched:\n"); scanf("%d",&num); for(i=0;i<count;i++) { if(ar[i]==num) { printf("the element is hit at index %d",(count-1)); break; } } if(i==count) { printf("the element is not found"); } } break; case 3: { inorder(0); printf("\n"); } break; case 5: { printf("\n\nAdios!\n\n"); exit(0); } break; } /* code */ } while(ch!=5); return 0; }
//@param root: The root of binary tree. Solution(TreeNode *root) { // write your code here inorder(root); }
void inorder(TreeNode * n) { if (! n) return; inorder(n->left); v.push_back(n); inorder(n->right); }
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { if (!p) return NULL; inorder(root, p); return suc; }
void inorder() { inorder(root); }
int main(void) { struct Node *root = NULL; root_ptr = NULL; long int loop; long int value; int ch;long int x; scanf("%ld",&loop ); x = loop; while(loop --) { // ch = 1; scanf("%d %ld",&ch, &value ); // scanf("%ld", &value); switch (ch) { case 0 : // (search(root, value)) ? printf("%s\n", "VALUE FOUND") : printf("%s\n", "VALUE NOT FOUND"); break; case 1: rb_tr_insert( value); root = root_ptr; inorder(root); break; case 2: root = root_ptr; if(!root) { printf("%s\n", "ROOT IS NULL"); break; } rb_tr_delete(value); root = root_ptr; inorder(root); break; case 3: root = root_ptr; // printf("rank of element %ld : %ld\n",value, rank(root, value)); break; case 4: // root = root_ptr; // if(! root) // break; // if(value > 1+root->lcount+root->rcount) // { // printf("%s\n","EXCEEDS NUMBER OF ELEMENTS PRESENT IN TREE" ); // break; // } // printf("no at rank %ld : %ld\n",value, find_rank(root, value)); break; default: break; } } printf("red nodes : %d\n", red_count(root_ptr)); printf("root color : %s\n",(root_ptr->col)? "RED" : "BLACK" ); printf("black nodes : %d\n", black_count(root_ptr)); printf("lcount : %d, rcount = %d\n",root_ptr->lcount, root_ptr->rcount ); // check_double_red(root_ptr); // black_path(root_ptr,0); // inorder(root_ptr); return EXIT_SUCCESS; }
/** Recursive inorder traversal 'helper' function */ void inorder(BSTNode<Data>* n) const { if(n==0) return; inorder(n->left); std::cout << *n << std::endl; inorder(n->right); }
void inorder(TreeNode *root, vector<int> &ans) { if (!root) return; inorder(root->left, ans); ans.push_back(root->val); inorder(root->right, ans); }
uint QFragmentMapData::erase_single(uint z) { uint w = previous(z); uint y = z; uint x; uint p; if (!Y.left) { x = Y.right; } else if (!Y.right) { x = Y.left; } else { y = Y.right; while (Y.left) y = Y.left; x = Y.right; } PMDEBUG("removeAndRebalance on %d (x=%d, y=%d)", z, x, y); inorder(); if (y != z) { F(Z.left).parent = y; Y.left = Z.left; Y.size_left = Z.size_left; Y.weight_left = Z.weight_left; if (y != Z.right) { /* z y / \ / \ a b a b / / ... --> ... / / y x / \ 0 x */ p = Y.parent; if (x) X.parent = p; P.left = x; Y.right = Z.right; F(Z.right).parent = y; uint n = p; while (n != y) { N.size_left -= Y.size; N.weight_left -= 1; n = N.parent; } } else { /* z y / \ / \ a y --> a x / \ 0 x */ p = y; } uint zp = Z.parent; if (!zp) { Q_ASSERT(head->root == z); head->root = y; } else if (F(zp).left == z) { F(zp).left = y; F(zp).size_left -= Z.size; F(zp).weight_left -= 1; } else { F(zp).right = y; } Y.parent = zp; // Swap the colors uint c = Y.color; Y.color = Z.color; Z.color = c; y = z; } else { /* p p p p / / \ \ z --> x z --> x | | x x */ p = Z.parent; if (x) X.parent = p; if (!p) { Q_ASSERT(head->root == z); head->root = x; } else if (P.left == z) { P.left = x; P.size_left -= Z.size; P.weight_left -= 1; } else { P.right = x; } } uint n = z; while (N.parent) { uint p = N.parent; if (P.left == n) { PMDEBUG("reducing size_left of %d by %d", N.parent, Z.size); P.size_left -= Z.size; P.weight_left -= 1; } n = p; } freeFragment(z); PMDEBUG("after removal"); inorder(); check(); if (Y.color != Red) { while (X.parent && (x == 0 || X.color == Black)) { if (x == P.left) { uint w = P.right; if (W.color == Red) { W.color = Black; P.color = Red; rotateLeft(p); w = P.right; } if ((W.left == 0 || F(W.left).color == Black) && (W.right == 0 || F(W.right).color == Black)) { W.color = Red; x = p; p = X.parent; } else { if (W.right == 0 || F(W.right).color == Black) { if (W.left) F(W.left).color = Black; W.color = Red; rotateRight(P.right); w = P.right; } W.color = P.color; P.color = Black; if (W.right) F(W.right).color = Black; rotateLeft(p); break; } } else { uint w = P.left; if (W.color == Red) { W.color = Black; P.color = Red; rotateRight(p); w = P.left; } if ((W.right == 0 || F(W.right).color == Black) && (W.left == 0 || F(W.left).color == Black)) { W.color = Red; x = p; p = X.parent; } else { if (W.left == 0 || F(W.left).color == Black) { if (W.right) F(W.right).color = Black; W.color = Red; rotateLeft(P.left); w = P.left; } W.color = P.color; P.color = Black; if (W.left) F(W.left).color = Black; rotateRight(p); break; } } } if (x) X.color = Black; } PMDEBUG("after rebalance"); inorder(); check(); return w; }
int main() { int number_of_nodes ; int save_the_input; int iterator, user_input,to_be_searched_item, to_be_deleted_item; BSTREE *root_node,*pre_root,*catch_search_item; root_node = NULL; while(1) { printf( " \n -------------Enter your choice --------------------------\n "); printf("\n 1. Insertion \n 2. Preorder \n 3. Inorder \n 4. Postorder \n 5. Deletion \n 6. Search \n 7. Exit \n "); scanf ("%d", &save_the_input ); switch(save_the_input) { case 1 : printf( " \n......... Please enter the numbers to create the binary tree....... \n"); scanf ("%d", &number_of_nodes); printf( "\n...... Please enter the numbers one by one... \n"); for (iterator = 0; iterator < number_of_nodes; iterator++) { scanf ("%d", &user_input); if (( user_input < 0 )) { printf( " ------- You have either entered a Character or a Negative number ----- \n "); printf( " ------- Please enter the Positive integer values-------------\n "); main(); } else { root_node = insert (root_node, user_input); printf("\n"); printf("Data element inserted is %d \n ", user_input); } } break; case 2 : pre_root = preorder(root_node); if (!pre_root){ printf("---------- You have not inserted the elements --------- \n --------Please insert--------- \n"); break; } case 3 : printf("\n---------- The Inorder is as follows for the given tree....... \n "); inorder(root_node); break; case 4 : printf("\n---------- The Postorder is as follows for the given tree....... \n "); postorder(root_node); break; case 5 : printf(" \n please enter number to be deleted from binary tree....\n "); scanf("%d",&to_be_deleted_item); root_node = delete(root_node, to_be_deleted_item); if( root_node == NULL){ printf( " ELEMENT NOT FOUND !!!!!!!! \n *********** OOPS !!! THE TREE IS EMPTY *********** \n"); } //else{ //printf( " ELEMENT NOT FOUND !!!!!!!! \n "); //} break; case 6 : printf(" \n Please enter the number to be searched in binary tree......\n"); scanf("%d", &to_be_searched_item); catch_search_item == search(root_node, to_be_searched_item); if( catch_search_item == NULL) { printf(" ELEMENT NOT FOUND !!!!!!!! \n"); } break; case 7 : exit(0); default : printf( " INVALID CHOICE \n "); } } return SUCCESS; }
uint QFragmentMapData::insert_single(int key, uint length) { Q_ASSERT(!findNode(key) || (int)this->position(findNode(key)) == key); uint z = createFragment(); Z.size = length; Z.left = 0; Z.right = 0; Z.size_left = 0; Z.weight_left = 0; PMDEBUG("inserting with key %d", key); uint y = 0; uint x = root(); Q_ASSERT(!x || X.parent == 0); uint s = key; // inorder(); bool right = false; while (x) { y = x; // PMDEBUG("x=%p: x->size_left=%d, key=%d, s=%d", x, x->size_left, x->key(), s); if (s <= X.size_left) { x = X.left; right = false; } else { s -= X.size_left + X.size; x = X.right; right = true; } } // if (y) // PMDEBUG(" y=%p: y->size_left=%d, y->key=%d s=%d", y, y->size_left, y ? y->key() : -1, s); Z.parent = y; if (!y) { head->root = z; } else if (!right) { // PMDEBUG("inserting left"); Y.left = z; Y.size_left = Z.size; Y.weight_left = 1; } else { // PMDEBUG("inserting right"); Y.right = z; } while (y && Y.parent) { uint p = Y.parent; if (P.left == y) { P.size_left += Z.size; P.weight_left += 1; } y = p; } // PMDEBUG("before rebalance"); // inorder(); rebalance(z); inorder(); PMDEBUG("end insert\n"); return z; }
void rbtree_inorder(struct rbtree const *tree, rbtree_print_func *print, void *aux) { inorder(tree->root, print, aux); }
int kthSmallest(TreeNode *root, int k) { return inorder(root, k); }
int main(int argc,char*argv[]) { binarytreenode* root; root = createBinaryTree(10); inorder(root); }
vector<int> impl(TreeNode* node) { vector<int> res; inorder(node, res); return res; }
void printSetIncreasing(Set *set) { inorder(set->tree, returnRootPosition(set->tree)); }
int main() { node* root = newNode(18); insert(32,root); insert(19,root); insert(14,root); insert(17,root); insert(13,root); insert(15,root); insert(30,root); insert(25,root); insert(35,root); /* 18 / \ / \ 14 32 / \ / \ 13 17 19 35 / \ 15 30 / 25 */ //int arr[]={10,5,1,7,40,50}; //cout<<isBST(root)<<endl; //printLCA(root,25,35); inorder(root); cout<<endl<<sizeOfBST(root)<<endl; /* int *arr; int count = 0; int n = sizeOfBST(root); arr = new int[n]; storeToArray(root,arr,&count); cout<<sumExist(arr,n,41)<<endl; */ int arr[] = {1,2,3,4,5,6,7,8,9,10}; cout<<endl; int size = sizeof(arr)/sizeof(arr[0]); node *newRoot = ListToBST(arr,0,size-1); preorder(newRoot); /*int x=-1; ceilValue(root,35,&x); cout<<endl<<x;*/ //cout<<maxBST(root->right)<<","<<minBST(root->right); /*int n=-1; cin>>n; while(n!=0) { printPredSuc(n,root); cin>>n; }*/ //cout<<endl<<nodeSearch(100,root)<<endl; _getch(); return 0; }
string printPretty(Heap h) { int numNodes = numElements(h); int numLines = 1; Heap *node = *returnAllHeaps(h); // find the height of the heap Heap *leftHeap = new Heap; leftHeap = h.left; while(leftHeap != nullptr){ numLines++; leftHeap = leftHeap->left; } // get the node in order from left to right (in order) std::vector<Heap> heapInorder; inorder(&h, heapInorder); // initialize the char[] with blank spaces char pretty[lengthOfContent(h)*numLines+numLines]; for (int i = 0; i< lengthOfContent(h)*numLines+numLines; i++){ pretty[i] = ' '; } // add line changes for(int i = 1; i < numLines; i++){ pretty[i*lengthOfContent(h)+i-1] = '\n'; } // add terminator pretty[lengthOfContent(h)*numLines+numLines -1] = '\0'; // find where the node belongs and place it int nodeStart; for(int i = 0; i< numNodes; i++){ nodeStart = 0; // find out where it belongs horizontally for(int j = 0; j< i; j++){ nodeStart = nodeStart + heapInorder.at(j).name.length(); } // find out on which line the node belongs bool lineFound = false; int k = 0; int line; while(!lineFound){ if(node[k].name == heapInorder.at(i).name){ line = (int) log2((double)(k+1)); lineFound = true; } else{ k++; } } // place the node where it belongs for(int z = 0; z< heapInorder.at(i).name.length(); z++){ pretty[line*(lengthOfContent(h)+1)+nodeStart+z] = heapInorder.at(i).name.at(z); } } string str(pretty); return str; }
int main() { int ch,k,p,t; struct node* root=NULL; root=insert(root,1); printf("Binary serch tree operation\n"); lable: printf("Press\n1 for insertion \n2 for search \n3 for traverse \n4 for max/min of tree \n5 for deletion"); scanf("%d",&ch); switch(ch){ case 1: printf("Enter data for new node\n"); scanf("%d",&k); insert(root,k); break; case 2: printf("Insert a element for search"); scanf("%d",&t); search(root,t); break; case 3: printf("Press \n1 for inorder \n2 for preorder \n3 for postorder\n"); scanf("%d",&p); printf("\n"); switch(p){ case 1: inorder(root); break; case 2: preorder(root); break; case 3: postorder(root); break; } break; case 4: int si; printf("For min press 0 else press 1\n"); scanf("%d",&si); struct node* p; switch(si){ case 0: p=min(root);//return node having lowest value printf("minimum element is %d \n",p->data); break; case 1: p=max(root); printf("maximum element is %d \n",p->data); break; default: printf("Invalid choice\n"); break; } break; case 5: int ke; printf("Enter the key to be deleted\n"); scanf("%d",&ke); root=del(root,ke);//root will be updated after deletion break; default: printf("Invalid choice\n"); break; } goto lable; return 0; }
main() { node *root=NULL; int x,n,i,op; int entrada; char line[10]; float tiemposArregloArr[3]; int contadorElements = 0; char pathx[30]; printf("ingrese el nombre del archivo en el escritorio: \n"); scanf("%c",&pathx); fr = fopen ("C:\\Users\\Cris\\Desktop\\pruebaText.txt","rt"); /* open the file for reading */ // PARA INGRESAR NODOS AL ARBOL struct timeb start, end; int diff; ftime(&start); while(fgets(line, 10, fr) != NULL) { sscanf (line, "%d", &entrada); /* convert the string to int */ root=insert(root,entrada); printf ("%d\n", entrada); /*entrada aqui va a tener el valor de la linea que quiero meter al arbol*/ } fclose(fr); /* close the file */ ftime(&end); diff = (int) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm)); tOrdenArbol = (((float)diff)/1000); printf("\nOperation took %u milliseconds\n", diff); printf("\n tordenarbol %f milliseconds\n", tOrdenArbol); contadorElements = contador(root); printf("numero de elementos %d \n",contadorElements); cosa = contadorElements; //PARA INGRESAR NODOS AL ARREGLO int arreglito[cosa]; int quickArr[cosa]; int piv = 0; fr = fopen ("C:\\Users\\Cris\\Desktop\\pruebaText.txt","rt"); while(fgets(line, 10, fr) != NULL) { sscanf (line, "%d", &entrada); /* convert the string to int */ arreglito[piv]=entrada; quickArr[piv]=entrada; piv++; printf ("\n%d entra al arreglo", entrada); /*entrada aqui va a tener el valor de la linea que quiero meter al arbol*/ } fclose(fr); /* close the file */ //AQUI ORDENO EL ARRAY int tempz, swappedz; int ii; int varz; for(varz = 0;varz<3;varz++){ ftime(&start); while(1){ swappedz = 0; for(ii=0;ii<contadorElements;ii++) { if(arreglito[ii]>arreglito[ii+1]){ int tempz = arreglito[ii]; arreglito[ii]=arreglito[ii+1]; arreglito[ii+1]=tempz; swappedz=1; } } if(swappedz==0){ break; } } for(ii=0;ii<contadorElements;ii++) { printf("\n array en pos %d = ",ii); printf(" %d\n",arreglito[ii]); } ftime(&end); diff = (int) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm)); tOrdenArreglo = (((float)diff)/1000); printf("\nOperation took %u \n", diff); printf("\n tArreglo %f \n", tOrdenArreglo); tiemposArregloArr[varz] = tOrdenArreglo; } printf("\n \n SALIDA ORDENADA DE BURBUJA:\n"); for(ii=0;ii<=cosa;ii++) { printf(" %d",arreglito[ii]); } printf("\n en tiempo: %f \n",tOrdenArreglo); /* ******************************************orden quicksort */ ftime(&start); quicksort(quickArr,0,cosa-1); printf("\n \n \n SALIDA ORDENADA DE QUICKSORT \n"); for(i=0;i<cosa;i++){ printf(" %d",quickArr[i]); } ftime(&end); diff = (int) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm)); tOrdenQuick= (((float)diff)/1000); printf("\n quicksort en tiempo de: %f \n", tOrdenQuick); printf("\n \nSALIDA ORDENADA DEL ARBOL:\n"); inorder(root); printf("\n en tiempo: %f ",tOrdenArbol); printf("\n \n Tiempos para graficar: "); for(varz = 0; varz<3;varz++) { printf("\n tiempo %d = %f \n",varz,tiemposArregloArr[varz] ); } FILE *archiv; archiv = fopen("C:/Users/Cris/Desktop/archivoGA.txt","w"); int yz; for(yz =0;yz<3;yz++){ fprintf(archiv,"%f\t%d\n",tiemposArregloArr[yz],yz); } fclose(archiv); FILE *gnuplotPipeM = popen("gnuplot -persists","w"); if(gnuplotPipeM){ fprintf(gnuplotPipeM,"set style data lines\n"); fprintf(gnuplotPipeM,"set title 'Grafica de ordenamiento' title 'caso a'\n"); fprintf(gnuplotPipeM,"plot [:][:0,000008]'C:/Users/Cris/Desktop/archivoGA.txt' title 'Caso a'\n"); fflush(gnuplotPipeM); getchar(); fprintf(gnuplotPipeM,"exit \n"); pclose(gnuplotPipeM); } do { /*printf("\n0)Create:"); printf("\n4)Insert:"); printf("\n3)Delete:");*/ printf("\n1)Print:"); printf("\n2)Quit:"); printf("\n\nEnter Your Choice:"); scanf("%d",&op); switch(op) { case 4:printf("\nEnter a data:"); scanf("%d",&x); root=insert(root,x); break; case 3:printf("\nEnter a data:"); scanf("%d",&x); root=Delete(root,x); break; case 1: printf("\nSalida ordenada del arbol:\n"); ftime(&start); inorder(root); printf("\n\nARBOLITO:\n"); display(root,0); //printf("fff %d",contador(root)); ftime(&end); diff = (int) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm)); printf("\nEl recorrido del arbol tomo %u millisegundos\n", diff); printf("\n"); break; } }while(op!=2); return 0; } /*of main*/
/** Perform an inorder traversal of this BST. */ void inorder() const { inorder(root); }