Esempio n. 1
0
void BSTree::inOrder(ostream& outs) {
    inOrder(outs, root);
}
Esempio n. 2
0
void inOrder(pRBNode root) {
    if(root == NULL) return;
    inOrder(root->left);
    printf("%d ", root->val);
    inOrder(root->right);
}
	void BinarySearchTree<T>::inOrder(void(*func)(const T &)) const
	{
		inOrder(func, root);
	}
Esempio n. 4
0
void BSTree::inOrder(void (*f) (int)){
    inOrder(root, f);
}
void BST::inOrder()
{
	inOrder(root);		// calls the next one
}
Esempio n. 6
0
void quadtree<T>::inOrder()
{
  inOrder(root);
}
void GLWidget::inOrder(typename TwoDTree::Node* n) {
    if ( n != nullptr ) {
        inOrder(n->left);

        if (n->isVertical) {
            double mostRightXInPartition = n->value.x();
            double mostLeftXInPartition = n->value.x();

            for (TwoDTree::Node *parent = n->parent; parent != nullptr; parent = parent->parent) {
                if (!parent->isVertical && parent->value.x() > mostRightXInPartition) {
                    mostRightXInPartition = parent->value.x();
                    break;
                }
            }

            for (TwoDTree::Node *parent = n->parent; parent != nullptr; parent = parent->parent) {
                if (!parent->isVertical && parent->value.x() < mostLeftXInPartition) {
                    mostLeftXInPartition = parent->value.x();
                    break;
                }
            }

            double leftX = mostLeftXInPartition;
            double rightX = mostRightXInPartition;

            if ( n->value.x() <= mostLeftXInPartition) {
                leftX = -2.0;
            }
            if ( n->value.x() >= mostRightXInPartition) {
                rightX = 2;
            }

            glBegin(GL_LINE_STRIP);

            glVertex2f( leftX, n->value.y() );
            glVertex2f( rightX, n->value.y() );

            glEnd();
       } else {
            double highestYInPartition = n->value.y();
            double lowestYInPartition = n->value.y();

            for (TwoDTree::Node *parent = n->parent; parent != nullptr; parent = parent->parent) {
                if (parent->isVertical && parent->value.y() > highestYInPartition) {
                    highestYInPartition = parent->value.y();
                    break;
                }
            }

            for (TwoDTree::Node *parent = n->parent; parent != nullptr; parent = parent->parent) {
                if (parent->isVertical && parent->value.y() < lowestYInPartition) {
                    lowestYInPartition = parent->value.y();
                    break;
                }
            }

            double lowerY = lowestYInPartition;
            double upperY = highestYInPartition;

            if ( n->value.y() >= highestYInPartition ) {
                upperY = 1.0;
            }
            if ( n->value.y() <= lowestYInPartition ) {
                lowerY = -1;
            }

            glBegin(GL_LINE_STRIP);

            glVertex2f( n->value.x(), lowerY );
            glVertex2f( n->value.x(), upperY );

            glEnd();
       }

       inOrder(n->right);
    }
}
Esempio n. 8
0
void BSTree::inOrder()
{
    inOrder(mRoot);
}
void Set::printSet()
{
    inOrder(root);
}
 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
     set<TreeNode*> S = {p, q};
     TreeNode* ans = NULL;
     inOrder(root, S, ans);
     return ans;
 }
Esempio n. 11
0
int main()
{
    node *root=NULL;
    int n;
    int num,i,value;
    while(1)
    {
        system("cls");
        printf("1. Create Binary Search Tree\n"
               "2. Delete a node\n"
               "3. inOrder Display\n"
               "4. Exit\n"
               "Enter Your Choice:- ");
        scanf("%d",&n);
        switch (n)
        {
            case 1:
                system("cls");
                printf("Enter the number of nodes you want to add: ");
                scanf("%d",&num);
                for(i=0;i<num;i++)
                {
                    printf("Enter the data for the %d node: ",i+1);
                    scanf("%d",&value);
                    createBST(&root,value);
                }
                printf("%d Nodes Added\n",num);
                getch();
                break;

            case 2:
                system("cls");
                printf("Enter the data of the node you want to Delete: ");
                scanf("%d",&value);
                del(&root,value);
                getch();
                break;

            case 3:
                system("cls");
                if(root==NULL)
                    printf("Empty Tree\n");
                else
                    inOrder(root);
                getch();
                break;

            case 4:
                system("cls");
                exit(0);

            default:
                system("cls");
                printf("\nWrong Selection");
                getch();
        }


    }
    return 0;
}
 int inOrder(TreeNode* root, set<TreeNode*> &S, TreeNode* &ans) {
     if (root == NULL) return NULL;
     int tot = inOrder(root->left, S, ans) + inOrder(root->right, S, ans) + S.count(root);
     if (tot == 2 && ans == NULL)     ans = root;
     return tot;
 }
void BinarySearchTree<T>::inOrder() const
{
    Node<T> *tmp = root;
    inOrder(tmp);
}
Esempio n. 14
0
	bool isValidBST(TreeNode *root)
	{
		if (root == nullptr) return true;
		return inOrder(root, root->left, root->right);
	}
Esempio n. 15
0
bool BTree<T>::inOrder(void (*visit) (BTreeNode<T>*))
{
	inOrder(visit, head);
}
Esempio n. 16
0
int main()
{
	//create tree node
	TreeNode* root = 0;
	insert(root, 1);
	insert(root, 8);
	insert(root, 3);
	insert(root, 4);
	insert(root, 6);
	insert(root, 7);
	insert(root, 10);
	insert(root, 13);
	insert(root, 14);
	insert(root, 14);
	insert(root, 15);
	
	std::cout<<"preOrder:";
	preOrder(root);
	std::cout<<"\n";

	std::cout<<"postOrder:";
	postOrder(root);
	std::cout<<"\n";

	std::cout<<"inOrder:";
	inOrder(root);
	std::cout<<"\n";

	std::cout<<"bfs:\n";
	bfs(root);
	std::cout<<"\n";

	{
		unsigned celem = 10;
		bool found = nonRecursiveSearch(root, celem);
		std::cout<<"found "<< celem << " : "<< (found? std::string("yes") : std::string("no"))<<std::endl;
	}

	{
		unsigned celem = 15;
		bool found = nonRecursiveSearch(root, celem);
		std::cout<<"found "<< celem << " : "<< (found? std::string("yes") : std::string("no"))<<std::endl;
	}
	{
		unsigned celem = 7;
		bool found = nonRecursiveSearch(root, celem);
		std::cout<<"found "<< celem << " : "<< (found? std::string("yes") : std::string("no"))<<std::endl;
	}

	unsigned closest = findClosestElem(root);
	std::cout<<"closest to root: "<<closest<<"\n";
	
	closestValue(root, 20);
	std::cout<<"closest to 20: "<<found<<" diff:" <<diff<<" in "<<counter<<" traversals\n";

	
	found = 0;
	diff = -1;
	counter = 0;
	closestValue(root, 12);
	std::cout<<"closest to 12: "<<found<<" diff:" <<diff<<" in "<<counter<<" traversals\n";

	std::cout<<"max level: "<<maxDepth(root)<<"\n";
	std::cout<<"min level: "<<minDepth(root)<<"\n";
	
	std::cout<<"all possible paths:\n";
	IntVector tmp;
	allPossiblePaths(root, tmp);
	
	{
		std::cout<<"-----------------------------------\n";
		bfs(root);
		std::cout<<"Nodes in range [10..15]: "<<nodesInRange(root, 10, 15)<<"\n";
	}
	
	deleteTree(root);
	
	return 0;
}
Esempio n. 17
0
void BSTree::inOrder (ostream& outs){
    inOrder(root, outs);
}
Esempio n. 18
0
void BSTree::inOrder (void (*funPtr)(int)) {
    inOrder (root, funPtr);
}
Esempio n. 19
0
 int kthSmallest(TreeNode* root, int k) {
     if(!root) return 0;
     inOrder(root, k);
     return vec.back();
 }
Esempio n. 20
0
int main()
{
    s *root=NULL;
    root=create('A');
    root->left=create('B');
    root->right=create('C');
    root->left->left=create('D');
    root->left->right=create('E');
    root->left->right->left=create('I');
    root->left->right->right=create('J');
    root->left->right->right->left=create('K');
    root->right->left=create('F');
    root->right->right=create('G');
    root->right->right->left=create('H');
    int n,num,h;
    while(1)
    {
        printf("1. All possible paths\n");
        printf("2. Height of the tree\n");
        printf("3. In Order\n");
        printf("4. Level Order\n");
        printf("5. Spiral Order\n");
        printf("6. Exit\n");
        printf("\nSelect an option: ");
        scanf("%d",&num);
        system("cls");
        switch(num)
        {
            case 1: allPath(root,0);
                    getch();
                    system("cls");
                    break;
            case 2: h= height(root);
                    printf("height = %d ",h);
                    getch();
                    system("cls");
                    break;
            case 3: inOrder(root);
                    printf("end");
                    getch();
                    system("cls");
                    break;

            case 4: printf("Enter the level you want to print: ");
                    scanf("%d",&n);
                    levelOrder(root,n);
                    printf("end");
                    getch();
                    system("cls");
                    break;

            case 5: spiralOrder(root,height(root));
                    getch();
                    system("cls");
                    break;

            case 6: exit(0);

            default: printf("Invalid Input!!");
                     getch();
                     system("cls");
        }
    }
    return 0;
// Create an array with the elements using in-order traversal of tree
vector<int> BST::toArray(){
    vector<int> arr;
    arr.resize(size);
    inOrder(root, &arr, 0);
    return arr;
}
Esempio n. 22
0
bool BST::inOrder(fTraverse traverse, void * pUser) const
{
    return inOrder(m_pTree, traverse, pUser);
}
Esempio n. 23
0
void BSTree::inOrder() 
{
    inOrder(root);
}
Esempio n. 24
0
/*6*/void lookupName(BST* contactBST)
{
	char inputStr[MAX];
	char changeRec, changeWhich;
	contactData* dataList;
	int strlength;
	int sucStatus = 0;

	while(sucStatus == 0){
		fflush(stdin);
		printf("Enter name to look up: ");
		fgets(inputStr,MAX,stdin);
		strlength = strlen(inputStr);
		if(inputStr[strlength-1] == '\n'){
			inputStr[strlength-1] = 0;
		}
		sucStatus = deleteBST(contactBST,(void*)inputStr,(void**)&dataList); //Use deleteBST instead
		if(sucStatus == 0){
			printf("%s cannot be found in list\n\n",inputStr);
			inOrder(contactBST,printTree);
			printf("\n");
		}
		else{
			printf("%-18s\t%s   %s\n", dataList->name, dataList->phoneNum, dataList->webAddr);
			printf("Change record? [y/n]: ");
			scanf(" %c",&changeRec); //Not working cause its not initialized
			if(changeRec == 'y' || changeRec == 'Y'){
				printf("n. change name\nc. change contact info\n");
				fscanf(stdin," %c",&changeWhich);
				if(changeWhich == 'n'){
					fflush(stdin);
					printf("Enter name: ");
					fgets(inputStr,MAX,stdin);
					strlength = strlen(inputStr);
					if(inputStr[strlength-1] == '\n'){
						inputStr[strlength-1] = 0;
					}
					strcpy(dataList->name,inputStr);
					insertBST(contactBST,(void*)dataList);
				}
				else if(changeWhich == 'c'){
					fflush(stdin);
					printf("Enter phone or hit <enter> for no change: ");
					fgets(inputStr,MAX,stdin);
					strlength = strlen(inputStr);
					if(inputStr[strlength-1] == '\n'){
						inputStr[strlength-1] = 0;
					}
					if(strcmp(inputStr,"\0") != 0){ //Means user wants a change
						strcpy(dataList->phoneNum,inputStr);
					}
					fflush(stdin);
					printf("Enter web address or hit <enter> for no change: ");
					fgets(inputStr,MAX,stdin);
					if(inputStr[strlength-1] == '\n'){
						inputStr[strlength-1] = 0;
					}
					if(strcmp(inputStr,"\0") != 0){ //Means user wants a change
						strcpy(dataList->webAddr,inputStr);
					}
					insertBST(contactBST,(void*)dataList);
				}
			}
		}
	}
}//end lookupName
Esempio n. 25
0
void inOrder(struct node *root){
	if(root == NULL) return;
	inOrder(root->left);
	printf("%d\t",root->data);
	inOrder(root->right);
}
Esempio n. 26
0
void BST::inOrder()
{
	inOrder(root);
}
Esempio n. 27
0
int
BinaryTree<DataType>::inOrder(int (*work)(DataType &))
{
	MustBeTrue(work != NULL);
	return(inOrder(root, work));
}
Esempio n. 28
0
void BTree<T>::print()
{
	//postOrder(&putout, head);
	//preOrder(&putout, head);
	inOrder(&putout, head);
}
Esempio n. 29
0
int main()
{

    int x[20], size, ii;

    printf("Ingrese tamanio de array: ");
    scanf("%d", &size);

    printf("Ingrese %d elementos: ", size);
    for(ii=0;ii<size;ii++){
        scanf("%d", &x[ii]);}

    quicksort(x,0,size-1);

    printf("Elementos ordenados");
    for (ii=0; ii<size;ii++){
        printf(" %d", x[ii]);}







    int contador = 0;
    int variable = 10;
    int * puntero = &variable;
    printf("\n\n%u\n",puntero);
    printf("%d\n",*puntero);
    *puntero = 50;

    printf("%u\n",puntero);
    printf("%d\n",*puntero);

    /****/
    struct node *root = NULL;
    FILE *file;
    file = fopen("C:\\Datos.txt","r");

    char singleLine[150];
    if (file == NULL){
        return -1;

    }
    else{
    while (!feof(file))
        {
            int i = 0;

            //int i = 0;
            fgets(singleLine,150,file);
            arreglo[i] = atoi(singleLine);
            auxiliar[contador] = arreglo[i];
            printf(singleLine);
            printf("\n*****\n");
            printf("valor numerico\n%i",arreglo[i]);
            root = insert(root, arreglo[i]);
            contador++;

            printf("\n*****\n");
            i++;


        }
        fclose(file);


        int a = arreglo[0];
        int b = arreglo[1];
        int c = arreglo[2];

        printf("\nvalor numerico en posicion 0\n%i",arreglo[0]);
        printf("\nvalor numerico en posicion 1\n%i",arreglo[1]);
        printf("\nvalor numerico en posicion 2\n%i",arreglo[2]);
        //int d = arreglo[0];
        //int e = arreglo[1];
        //int f = d+e;


        //printf("\n**********\n%i\n%i\n**********",singleLine[4],singleLine[0]);
        //printf(singleLine[1]);
        //printf(singleLine[2]);
        //printf(singleLine[3]);
        printf("\nLa suma de %i y %i es: %i ",a,b,c);
        //printf("\nLa suma de %i y %i es: %i ",d,e,f);



}
/*
    int arreglo [10];
    arreglo[0] = 1;

    int peaje;
    int tipo_vehiculo;
    printf("\nIntroduzca tipo de vehiculo: ");
    scanf("%d",&tipo_vehiculo);

    switch(tipo_vehiculo)
    {
    case 1:
        printf("Turismo\n");
        peaje = 500;
        printf("%d",peaje);
        break;

    case 2:
        printf("Autobus\n");
        peaje = 3000;
        printf("%i",peaje);

        break;

    case 3:
        printf("Motocicleta\n");
        peaje = 300;
        printf("%i",peaje);
        break;

    default:
        printf("Vehiculo no autorizado\n");


    }
    printf("\n*********************************\n");

    int contador = 0;
    while(contador<10)
        {
        printf("Hola mundo ciclo %d\n",contador);
        contador ++;
        }

    int i = 567;
    int *iPointer = &i;
    int **ipp = &iPointer;
    printf("%i\n", **ipp);

    client c1;
    c1.name = "John Doe";
    c1.email = "*****@*****.**";
    c1.age = 26;
    printf("%s\n",c1.name);

    variableGlobal +1;
    tab();
    funcion();
    printf("Hello world!\n");
    //printf(variableGlobal);*/




/*
    struct node *root = NULL;*/

  /* Constructing tree given in the above figure */
  /*root = insert(root, 10);
  root = insert(root, 20);
  root = insert(root, 30);
  root = insert(root, 40);
  root = insert(root, 50);
  root = insert(root, 25);
*/
  /* The constructed AVL Tree would be
            30
           /  \
         20   40
        /  \     \
       10  25    50
  */
/*
  printf("Pre order traversal of the constructed AVL tree is \n");
  preOrder(root);

*/

    printf("\nPreorden en el arbol AVL \n");
    preOrder(root);

    printf("\nInOrden en el arbol AVL \n");
    inOrder(root);

    printf("\n\n%d\n%d\n\n%d\n\n%d\n",auxiliar[0],auxiliar[1],auxiliar[24],auxiliar[151]);
    int prueba1;
    int prueba2;
    int resultado;

    prueba1 = auxiliar[0];
    prueba2 = auxiliar[1];

    resultado = prueba1 + prueba2;

    printf("The result of the operation adding First number %d and Second number %d from the array is Result: %d\n", prueba1, prueba2, resultado);

int array[150], n, c, d, swap;
int i, num;
num = 25;
/* Copying data from array 'a' to array 'b */
   for ( i = 0; i < num; i++) {
      array[i] = auxiliar[i];
   }





    c=25;
    for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);



    return 0;
}
Esempio n. 30
0
	void displayIn(){inOrder(root);cout<<endl;}