Beispiel #1
0
int main()
{
    printf("root : ");
    node *root=createBinaryTree();
    while(isBST(root,INT_MIN,INT_MAX)==0){
        if (isBST(root,INT_MIN,INT_MAX)==0)
            printf("the tree is not a Binary Search Tree ! \n input again : \n");
        prettyPrint(root,0);
        printf("\n");
        //root=delete(root);
        printf("root : ");
        node *root=createBinaryTree();
        isBST(root,INT_MIN,INT_MAX);
    }

    prettyPrint(root,0);
    //root=AVL(root);
    int x=1,i;
    while(x==1){
        printf("give the node you want to insert : ");
        scanf("%d",&i);
        root=insertNode(root,i);
        prettyPrint(root,0);
        printf("do you want to insert another node? (1=yes, 0=no) \n");
        scanf("%d",&x);
    }
    return 0;
}
Beispiel #2
0
node *createBinaryTree(){
    char *content=(char*)malloc(sizeof(char)*3);
    scanf("%s",content);
    node *p=createNode(atoi(content));
    if(strcmp(content,"*")==0)
        return NULL;
    else {
        printf("left child of %d parent : ",atoi(content));
        p->left=createBinaryTree();
        printf("right child of %d parent : ",atoi(content));
        p->right=createBinaryTree();
    }
    return p;
}
Beispiel #3
0
NodeT* createBinaryTree(FILE* f)
{
    NodeT* p;
    char* data=(char*)malloc(sizeof(char)*100);
    fscanf(f, "%s", data);
    if(strcmp(data, "*")==0)
        return NULL;
    else
    {
        p=createNodeT(atoi(data));
        p->left=createBinaryTree(f);
        p->right=createBinaryTree(f);
    }
    return p;
}
Beispiel #4
0
void test_inserting_first_element(){
	int res;
	BinaryTree tree = createBinaryTree(cmpInt);
	int element1 = 10;
	res = insert(&tree,&element1,NULL);
	ASSERT(res == 1);
}
Beispiel #5
0
int main(int argc, const char *argv[])
{
    BinaryTree root1;
    BinaryTree root2;

    createBinaryTree(&root1);
    createBinaryTree(&root2);

    if(checkIfSubTree(root1, root2)){
        printf("Root2 is subtree of root1\n");
    }else{
        printf("Root2 is not subtree of root1\n");
    }

    return 0;
}
Beispiel #6
0
void MonolingualModel::readVocab(const string& training_file) {
    ifstream infile(training_file);

    if (!infile.is_open()) {
        throw runtime_error("couldn't open file " + training_file);
    }

    vocabulary.clear();

    string word;
    while (infile >> word) {
        addWordToVocab(word);
    }

    if (config.verbose)
        cout << "Vocabulary size: " << vocabulary.size() << endl;

    reduceVocab();

    if (config.verbose)
        cout << "Reduced vocabulary size: " << vocabulary.size() << endl;

    training_words = 0; // count number of words in the training file
    for (auto it = vocabulary.begin(); it != vocabulary.end(); ++it) {
        training_words += it->second.count;
    }

    createBinaryTree();
    initUnigramTable();
}
Beispiel #7
0
Set *createSet()
{
	Set *result = new Set;
	result->tree = createBinaryTree();
	result->count = 0;
	return result;
}
Beispiel #8
0
int main(int argc, char *argv[])
{
	/* Performance */
	clock_t begin, end;
	double time_spent;
	begin = clock();

	/* Some variables to initialise the input files read */
	FILE *inputMovieData = fopen(argv[1], "r");
	FILE *outputFile         = fopen(argv[2], "w");

	/* Initialise a global tree of type node */
	node *tree = makedict();

	/* Read in the binary tree with data from argv 1 */
	createBinaryTree(tree, inputMovieData);

	/* Search the tree and write the data to a file specified in argv 2 */
	searchTree(tree, outputFile);

	/* Close the files at the end */
	fclose(inputMovieData);
	fclose(outputFile);

	/* Performance Log */
	end = clock();
	time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
	printf("\nEXECUTION TIME: %.5f seconds\n", time_spent);

	return 0;
}
int main()
{
	BinaryTree BinaryTree;

	BinaryTree = createBinaryTree();
	printf("\n ====== test for postordering the BinaryTree presented by left_right_child structure  ====== \n");	 
	
	printf("\n test for respectively inserting 'A' and 'B' into left and right child of the parent '/' , then 'C' and 'D' into the left and right child of the parent 'A' \n");	
	insert('A', find('/', BinaryTree), 1);	// 1 means left child
	insert('B', find('/', BinaryTree), 0);	// 0 means right child
	insert('C', find('A', BinaryTree), 1);
	insert('D', find('A', BinaryTree), 0);
	printPreorder(1, BinaryTree);
	
	printf("\n test for respectively inserting 'A' and 'B' into left and right child of the parent '/' \n");	
	insert('E', find('/', BinaryTree), 1);
	insert('F', find('/', BinaryTree), 0);
	printPreorder(1, BinaryTree); 

	printf("\n test for inserting 'E' into the right child of the  parent 'B' , then repectively 'F' and 'G' into the left and right child of the parent 'H' \n");	
	insert('E', find('B', BinaryTree), 0);		
	insert('F', find('E', BinaryTree), 1);
	insert('G', find('E', BinaryTree), 0);
	printPreorder(1, BinaryTree); /**/

	return 0;
}
Beispiel #10
0
nodeT* createBinaryTree()
{
    nodeT* p;
    char* data = (char*)malloc(sizeof(char)*100);
    fscanf(f, "%s", data);
    if(strcmp(data, "*") == 0)
    {
        return NULL;
    }
    else
    {
        p = createTreeNode(atoi(data));
        p->left = createBinaryTree();
        p->right = createBinaryTree();
    }
    return p;
}
Beispiel #11
0
int main(int argc, char** argv)
{
  std::vector<int> vec = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  TreeLinkNode *root = createBinaryTree(vec.begin(), vec.end());
	
	Solution s;
	s.connect(root);
	print(root);
    
  return 0;
}
Beispiel #12
0
void test_inserting_elements_at_depth_two(){
	BinaryTree tree = createBinaryTree(cmpInt);
	int res;
	int element1 = 10,element2 = 20,element3 = 30,element4 = 40;
	insert(&tree,&element1,NULL);
	insert(&tree,&element2,&element1);
	insert(&tree,&element3,&element1);
	res = insert(&tree,&element4,&element1);
	ASSERT(res == 0);
	ASSERT(&element2 == getLeft(&tree,&element1));
	ASSERT(&element3 == getRight(&tree,&element1));
};
Beispiel #13
0
int main()
{
  TreeNode *root=NULL;
  createBinaryTree(&root);
  printf("\n\n");
  printPreOrder(root);
  printf("\n");
  printInOrder(root);
  printf("\n");
  printPostOrder(root);
  printf("\n");
  return 0;
}
Beispiel #14
0
void test_inserting_two_childrens_at_depth_two(){
	BinaryTree tree = createBinaryTree(cmpInt);
	int element1  = 10,element2 = 20,element3 = 30,element4 = 40,element5 = 50;
	BinaryTreeNode *node;
	insert(&tree,&element1,NULL);
	insert(&tree,&element2,&element1);
	insert(&tree,&element3,&element1);
	insert(&tree,&element4,&element2);
	insert(&tree,&element5,&element2);
	ASSERT(&element2 == getLeft(&tree,&element1));
	ASSERT(&element3 == getRight(&tree,&element1));
	ASSERT(&element4 == getLeft(&tree,&element2));
	ASSERT(&element5 == getRight(&tree,&element2));
};
Beispiel #15
0
void test_adding_childs_to_right_child_of_root(){
	BinaryTree tree = createBinaryTree(cmpInt);
	int element1  = 10,element2 = 20,element3 = 30,element4 = 40,element5 = 50;
	BinaryTreeNode *node;
	insert(&tree,&element1,NULL);
	insert(&tree,&element2,&element1);
	insert(&tree,&element3,&element1);
	insert(&tree,&element4,&element3);
	insert(&tree,&element5,&element3);
	ASSERT(&element2 == getLeft(&tree,&element1));
	ASSERT(&element3 == getRight(&tree,&element1));
	printf("-->%p",((BinaryTreeNode*)tree.root)->right->left);
	// ASSERT(&element4 == getLeft(&tree,&element3));
	// ASSERT(&element5 == getRight(&tree,&element3));
};
Beispiel #16
0
int main()
{
  int internalNodes ;
  TreeNode *root=NULL;
  createBinaryTree(&root);
  printf("\n\n");
  printPreOrder(root);
  printf("\n");
  printInOrder(root);
  printf("\n");
  printPostOrder(root);
  printf("\n");
  internalNodes = countInternalNodes(root);
  printf("Internal nodes =%d\n",internalNodes);
  return 0;
}
Beispiel #17
0
int main(int argc, const char * argv[])
{

    // insert code here...
    std::cout << "Hello, World!\n";
    
    BinaryTree *binaryTree = createBinaryTree();
    binaryTree->InorderTraversal();
  //  binaryTree->PreorderTraversal();
  //  binaryTree->PostorderTraversal();
    
    if('a' > 'b')
        std::cout << "a is greater than b";
    else
        std::cout << "b is greater than a";
    
    return 0;
}
Beispiel #18
0
int main()
{
    FILE* f=fopen("input.txt", "r");
    if(f==0)
        printf("err");
    NodeT *root=createBinaryTree(f);
    printf("Initial tree:\n");
    prettyPrint(root, 0);

    printf("\nList:\n");

    NodeL *head=ListFromTree(root);
    printList(head);

    printf("\nFinal tree:\n");

    root=TreeFromList(&head);
    prettyPrint(root, 0);

    fclose(f);
    return 0;
}
Beispiel #19
0
int main()
{
    NodeT* root=NULL;

    FILE* f=fopen("input.txt", "r");
    if(f==0)
        printf("err");

    root=createBinaryTree(f);
    printf("Initial tree:\n");
    prettyPrint(root, 0);
    printf("\n\n\n");

    int vvalue;
    for(vvalue=1; vvalue<=100; vvalue++)
        if(searchNode(root, vvalue)==0)//this line is only for making sure that a value is not inserted 2 times
            root=insertNodeAVLtree(root, vvalue);

    prettyPrint(root, 0);

    fclose(f);
    return 0;
}
Beispiel #20
0
void createBinaryTree(TreeNode **root)
{
  char c;
  int data;
  TreeNode *newNode=NULL,*parentNode = NULL;

  printf("\n Do you want insertion(Y/N) :");
  scanf(" %c",&c);
  //  c=getchar();
  
  if(c=='Y')
    {

      parentNode = deque();
      
      if( parentNode == NULL )
	{
	  printf("\n Enter data of root of tree :");
	  scanf("%d",&data);
	  newNode = (TreeNode *)malloc(sizeof(TreeNode));
	  newNode -> data = data ;
	  newNode -> leftChild = NULL;
	  newNode -> rightChild = NULL ;
	  (*root)= newNode ;
	  enque(newNode);
	  createBinaryTree(&*root);
	  return;
	}
      
      printf("\n Enter left child of parent %d . (If NULL enter -1) :",parentNode -> data);
      scanf("%d",&data);
      
      if(data != -1)
	{
	  newNode = (TreeNode *)malloc(sizeof(TreeNode));
	  newNode -> data = data ;
	  newNode -> leftChild = NULL;
	  newNode -> rightChild = NULL ;
	  parentNode -> leftChild = newNode ;
	  enque(newNode);
	}

      printf("\n Enter right child of parent %d . (If NULL enter -1) :",parentNode -> data);
      scanf("%d",&data);
      
      if(data != -1)
	{
	  newNode = (TreeNode *)malloc(sizeof(TreeNode));
	  newNode -> data = data ;
	  newNode -> leftChild = NULL;
	  newNode -> rightChild = NULL ;
	  parentNode -> rightChild = newNode ;
	  enque(newNode);
	}

      createBinaryTree(&*root);
      return;
    }

  else if(c=='N')
    {
      printf("Thank You.............. \n");
      return;
      // exit(0) ;
    }

  else
    {
      printf("\nWrong input .");
      createBinaryTree(&*root);
    }
  return;
}
int main(int argc,char*argv[])
{
	binarytreenode* root;
	root = createBinaryTree(10);
	inorder(root);
}