Example #1
0
int main()
{
	int key;
	int option;
	node_t *root=NULL;
	printf("请输入根节点的关键字:  ");
	scanf("%d",&key);
	root=Init(key);
	printLevel(root);
	while(1)
	{
		printf("输入1表示插入,2表示删除,其他键表示退出:  ");
		scanf("%d",&option);
		if(option==1)
		{
			printf("请输入要插入节点的关键字: ");
			scanf("%d",&key);
			root=Insert(root,key);
			printLevel(root);
			printf("\n");
		}
		else if(option == 2)
		{
			printf("请输入要删除节点的关键字:  ");
			scanf("%d",&key);
			Delete(root,key);
			printLevel(root);
			printf("\n");
		}
		else 
			break;
	}
	return 0;
}
void printLevel(BST* root, int level) {
	if(root == NULL)
		return;
	if(level == 1)
		printf("%d\t",root->data);
	else if (level > 1) {
		printLevel(root->left, level-1);
    	printLevel(root->right, level-1);
    }
}
Example #3
0
void printLevel(Node *current, int level, FILE *output){
	if(current == NULL) return;
	if(level == 0){
		fprintf(output,"%c",current->data);
	}
	else if(level>0){
		printLevel(current->LSON, level-1, output);
		printLevel(current->RSON, level-1, output);
	}
}
Example #4
0
void printLevel (TNODE_p_t root, int level) {
	if (root == NULL)
		return;
	if (level == 1)
		printf(" %d", root->data);
	else {
		printLevel(root->left, level - 1);
		printLevel(root->right, level - 1);
	}
}
 void printLevel(TreeNode *root, int level, vector<int> &vec)
 {
     if(root == NULL) return;
     if(level == 1)
     {
         vec.push_back(root->val);
     }
     else
     {
         printLevel(root->left, level-1, vec);
         printLevel(root->right, level-1, vec);
     }
 }
int main(int argc, char* argv[]) {
    clock_t start, stop;
    start = clock();
    //int v = atoi(argv[1]);
    int v = 10; // nicer for IDE/Profiler
    std::cout << "The random seed is: " << v << std::endl;
    srand(v);

    GenRandGenerator randGenerator;
    LevelGenerator<GenRandGenerator> levelGenerator( randGenerator, NUM_LEVS );

    std::vector<std::thread> threads; threads.reserve( NUM_THREADS );
    for( uint32_t i = 0 ; i < NUM_THREADS ; ++i ) {
        uint32_t threadSeed = v * ((i+1)*(i+1));
        std::cout << "The seed of thread " << i << " is: " << threadSeed << std::endl;
        uint32_t partitionStartIndex( i * NUM_LEVS_PER_THREAD );
        uint32_t partitionEndIndex( partitionStartIndex + NUM_LEVS_PER_THREAD );
        threads.emplace_back(std::bind( &LevelGenerator<GenRandGenerator>::partitionedGenerateLevels,
                           &levelGenerator, threadSeed, partitionStartIndex, partitionEndIndex ));
    }

    for( uint32_t i = 0 ; i < NUM_THREADS ; ++i ) {
        threads[i].join();
    }
    NumRoomsMetric numRoomsMetric;
    Level & l( levelGenerator.pickLevelByCriteria( numRoomsMetric ) );
    printLevel( l );

    stop = clock();
    long clocks_per_ms = CLOCKS_PER_SEC/1000;
    std::cout << (stop - start)/clocks_per_ms << std::endl;
    return 0;
}
void printLevel(struct node* root, int level, int *arr, int *index)
{

	if (root == NULL)
		return;
	if (level == 1)
	{
		arr[*index] = root->data;
		++*index;
	}
	else if (level > 1)
	{
		printLevel(root->right, level - 1, arr, index);
		printLevel(root->left, level - 1, arr, index);

	}
}
void printLevel( Node<T> * node, int level, bool zig )
{
    if ( node == nullptr ) {
        return;
    }

    if ( level == 1 ) {
        std::cout << node->data << " ";
    }
    else {
        if( zig ){
            printLevel( node->left, level - 1,  zig );
            printLevel( node->right, level - 1, zig );
        } else {
            printLevel( node->right, level - 1, zig );
            printLevel( node->left, level - 1,  zig );
        }
    }
}
void levelOrderTraversal( Node<T> *root )
{
    int h = height(root);
    bool zig = true;
    for ( int i = 1; i <= h; ++i ) {
        printLevel( root, i, zig );
        std::cout << std::endl;
        zig = !zig;
    }
}
 vector<vector<int> > levelOrder(TreeNode *root) {
     int height = getHeight(root);
     vector<vector<int> > res;
     for(int level = 1; level <= height; level++)
     {
         vector<int> vec;
         printLevel(root, level, vec);
         res.push_back(vec);
     }
     return res;
 }
Example #11
0
UBool BiDiConformanceTest::checkLevels(const UBiDiLevel actualLevels[], int32_t actualCount,
                                       const char *paraLevelName) {
    UBool isOk=TRUE;
    if(levelsCount!=actualCount) {
        errln("Wrong number of level values; expected %d actual %d",
              (int)levelsCount, (int)actualCount);
        isOk=FALSE;
    } else {
        for(int32_t i=0; i<actualCount; ++i) {
            if(levels[i]!=actualLevels[i] && levels[i]<UBIDI_DEFAULT_LTR) {
                if(directionBits!=3 && directionBits==getDirectionBits(actualLevels, actualCount)) {
                    // ICU used a shortcut:
                    // Since the text is unidirectional, it did not store the resolved
                    // levels but just returns all levels as the paragraph level 0 or 1.
                    // The reordering result is the same, so this is fine.
                    break;
                } else {
                    errln("Wrong level value at index %d; expected %d actual %d",
                          (int)i, levels[i], actualLevels[i]);
                    isOk=FALSE;
                    break;
                }
            }
        }
    }
    if(!isOk) {
        printErrorLine(paraLevelName);
        UnicodeString els("Expected levels:   ");
        int32_t i;
        for(i=0; i<levelsCount; ++i) {
            els.append((UChar)0x20).append(printLevel(levels[i]));
        }
        UnicodeString als("Actual   levels:   ");
        for(i=0; i<actualCount; ++i) {
            als.append((UChar)0x20).append(printLevel(actualLevels[i]));
        }
        errln(els);
        errln(als);
    }
    return isOk;
}
int* BSTRighttoLeftRows(struct node* root)
{
	if (root == NULL)
		return 0;
	int n = count1(root);
	int *array = (int*)malloc(sizeof(int)*n);
	int h = height1(root);
	int i, index = 0;
	for (i = 1; i <= h; i++)
		printLevel(root, i, array, &index);
	return array;
}
int main()
{
  node *root = newNode(1);
  root->left        = newNode(2);
  root->right       = newNode(3);
 
  node *root1 = newNode(1);
  root1->right       = newNode(2);
  root1->right->right  = newNode(3);
  root1->right->right->right = newNode(4);

  printf("\n return value: %d",printLevel(root, root1));
 
  getchar();
  return 0;
}
Example #14
0
int main(int argc, char* argv[]) {
  clock_t start, stop;
	start = clock();
	int v = atoi(argv[1]);
	printf("The random seed is: %d \n", v);
	srand(v);

    GenRandGenerator randGenerator( v );

    LevelGenerator<GenRandGenerator> levelGenerator( randGenerator );
    levelGenerator.generateLevels();

    NumRoomsMetric numRoomsMetric;
    Level & l( levelGenerator.pickLevelByCriteria( numRoomsMetric ) );
    printLevel( l );

    stop = clock();
    long clocks_per_ms = CLOCKS_PER_SEC/1000;
    printf("%ld\n", (stop - start)/clocks_per_ms);

    return 0;
}
Example #15
0
/*
	知识准备:
	红黑树的性质:
	1 一个节点不红既黑   3 叶子节点是黑色 5各路径上黑高一致 
	2 根节点是黑色  
	4 如果一个节点是红色,那么他的两个孩子都为黑色 
	插入时将插入点设置为红色( 假设p(z) == p[p[z]]->left 另一情况):
		1 case 1: 如果插入的点是根,则违反 性质2 ,直接将其设置黑色即可
		2 如果 插入点(z)的父节点也为红色(其叔叔为y)
			2.1  case 2.1: 如果y为红色,将p[z],y调为黑色。将p[p[z]]调为红色。把z=p[p[z]]。继续检查
			2.2  case 2.2: 如果y为黑色,z为右孩子,将z进行左旋(z和 p[z] 调换),之后转为case 3
			2.3  case 2.3: 如果y为黑色,z为左孩子,进行一次右旋 bingo。

*/
node_t * Insert(node_t *root,key_t key)
{
	node_t *parent_node=NULL;
	node_t *insert_node=Search(root,&parent_node,key);
	node_t *uncle_node=NULL;
	//如果是空节点,则生成一个根节点,并返回
	//case 1
	if(root == NULL)
	{
		root = getNode(NULL, key);
		root->colour = BLACK;
		return root;
	}
	//将节点插入到 parent的孩子处
	//如果这样的点已经存在,怎返回 
	if(insert_node != NULL)
	{
		return root;
	}    

	//如果要插入的节点关键字小于parent 则插到左边 
	if(key < parent_node->key)
	{
		insert_node = getNode(parent_node, key);
		parent_node->left=insert_node;
	}

	else
	{
		insert_node = getNode(parent_node, key);
		parent_node->right=insert_node;
	}
	node_t * cur_node=insert_node;
	node_t * cur_parent=parent_node;
	node_t * cur_uncle=NULL;
	//至此插入完成,现在进行调整,使得继续满足红黑树
	while(cur_node->parent != NULL && cur_node->parent->colour == RED)
	{
		cur_parent=cur_node->parent;
		//如果插入点父亲是一个左孩子
		if(cur_node->parent == cur_node->parent->parent->left)
		{//p[z]=p[p[z]]->left
			//叔叔是红色 case 2.1
			if(cur_node->parent->parent->right!=NULL && cur_node->parent->parent->right->colour == RED)
			{
				//把父亲和叔叔置为黑色
				cur_node->parent->colour=BLACK;
				cur_node->parent->parent->right->colour=BLACK;
				//把父亲的父亲置为红色
				cur_node->parent->parent->colour=RED;
				//z 跳到它的爷爷处
				cur_node=cur_node->parent->parent;
				continue;
			}
			//case 2.2 叔叔是黑色,且自己为右孩子
			else if(cur_node == cur_node->parent->right)
			{
				cur_node=cur_node->parent;
				root=leftBlance(root,cur_node);
			}
			//debug 
			printLevel(root);
			//case 2.3 叔叔是黑色,自己为左孩子
			cur_node->parent->colour=BLACK;
			cur_node->parent->parent->colour=RED;
			root=rightBlance(root,cur_node->parent->parent);

			printLevel(root);
		}

		else//插入点的父亲是一个右孩子,和上面分析方法一样
		{//p[z]=right[p[p[z]]];
			//叔叔是红色 case 2.1
			//叔叔是红色 case 2.1
			if( cur_node->parent->parent->left!= NULL && cur_node->parent->parent->left->colour == RED )
			{
				//把父亲和叔叔置为黑色
				cur_node->parent->colour=BLACK;
				cur_node->parent->parent->left->colour=BLACK;
				//把父亲的父亲置为红色
				cur_node->parent->parent->colour=RED;
				//z 跳到它的爷爷处
				cur_node=cur_node->parent->parent;
				continue;
			}
			//case 2.2 叔叔是黑色,且自己为右孩子
			else if(cur_node == cur_node->parent->left)
			{
				cur_node=cur_node->parent;
				root=rightBlance(root,cur_node);
			}
			//case 2.3 叔叔是黑色,自己为左孩子
			cur_node->parent->colour=BLACK;
			cur_node->parent->parent->colour=RED;
			root=leftBlance(root,cur_node->parent->parent);       
		}
	}


	//满足 parent 不为空(还没走到根节点) 并且还没找到黑父节点,需要继续往上调整 
	//z为 z插入点的父亲,如果插入点的父亲为红色 
	root->colour = BLACK;
	return root;
}
Example #16
0
int main(){
	int i, j;  //for loops
	int choice; //option
	int height; //height of tree
	int size; //size of array
	int done = 0; //if user's done using the program, initialized as false
	int validation;

	output = fopen("2014.out","w"); //open output file

	while(done != 1){
		choice = printChoice();
		if(choice == 1){
			char data[MAX_SIZE];
			char tag[MAX_SIZE];

			//GET DATA
			printf("DATA: ");
			scanf("%[^\n]",data);
			getchar();

			//PRINT DATA ARRAY
			fprintf(output,"DATA: " );
			for(i=0;i<strlen(data);i++){
				fprintf(output,"%c", data[i]);
			}
			fprintf(output,"\n" );

			//GET TAGS
			printf("TAG: ");
			scanf("%[^\n]",tag);
			getchar();

			//PRINT TAG ARRAY
			fprintf(output,"TAG: " );
			for(i=0;i<strlen(tag);i++){
				fprintf(output,"%c", tag[i]);
			}
			fprintf(output,"\n" );

			if (strlen(data) != strlen(tag)){ //not equal input
				fprintf(output,"Input is invalid.\n\n");
			}

			else{
				getInput(data);
				getInput(tag);

				size = strlen(data);

				if(size==1 && tag[0] == '0'){ //tree with only one node
					fprintf(output,"Postorder: %c\n",data[0]);
					fprintf(output,"Levelorder: %c\n",data[0]);
					fprintf(output,"\nTable: \nLSON NODE RSON\n--------------\n");
					fprintf(output,"      %c\n\n",data[0]);
				}

				else if(size==1 && tag[0] == '1'){ //one node with children is invalid
					fprintf(output,"Input is invalid.\n\n");
				}

				else{
					if(tag[0] == '0'){
						fprintf(output,"Input is invalid.\n\n");
					}

					else{
						validation = valid1(tag,size);
						if(validation == -1){
							Node *root = Tree(data,tag,size);
							fprintf(output,"Postorder: ");
							height = printPost(root,output,0);
							fprintf(output,"\nLevel Order: ");
							for(i=0;i<height;i++){
									printLevel(root, i, output);
							}
							fprintf(output,"\nTable: \nLSON NODE RSON\n--------------\n");
							printTable(root,output);
							fprintf(output,"\n\n");
						}
						else{
							fprintf(output,"Input is invalid.\n\n");
						}
					}
				}
			}
		}

		else if(choice == 2){
			char filename[MAX_SIZE];
			char data_array[MAX_SIZE];
			char tag_array[MAX_SIZE];
			char data = ' ';
			int index = 0;

			printf("Filename: ");
			scanf("%s",filename);

			input = fopen(filename,"r");

			while(!feof(input)){
				index = 0;
				while(index < MAX_SIZE){
					data_array[index] = ' ';
					tag_array[index] = -1;
					index++;
				}
				index = 0;
				while(data!=':' && !feof(input)){
					data = fgetc(input);
				}
				fgetc(input);
				while(data!='\n' && !feof(input)){
					data = fgetc(input);
					if(data!=' '){
						data_array[index] = data;
						index++;
					}
				}
				int index = 0;
				while(data!=':' && !feof(input)){
					data = fgetc(input);
				}
				fgetc(input);
				data = fgetc(input);
				while(data!='\n' && !feof(input)){
					if(data!=' '){
						tag_array[index] = atoi(&data);
						index++;
					}
					data = fgetc(input);
				}

				fprintf(output,"DATA: " );
				int i;
				for(i=0;i<index;i++){
					if(i==index-1){
						fprintf(output,"%c", data_array[i]);
					}
					else{
					fprintf(output,"%c", data_array[i]);
					}
				}
				fprintf(output,"\n"); 

				fprintf(output,"TAG: " );
				for(i=0;i<index;i++){
					fprintf(output,"%d", tag_array[i]);
				}
				fprintf(output,"\n");

				int size = strlen(data_array);

				if(index==1 && tag_array[0] == 0){
					fprintf(output,"Postorder: %c\n",data_array[0]);
					fprintf(output,"Levelorder: %c\n",data_array[0]);
					fprintf(output,"\nTable: \nLSON NODE RSON\n--------------\n");
					fprintf(output,"      %c\n\n",data_array[0]);
				}
				else if(index==1 && tag_array[0] != 1){
					fprintf(output,"Input is invalid.\n\n");
				}
				else{
					//print(tag_array,size1);
					if(tag_array[0] == 0){
						fprintf(output,"Input is invalid.\n\n");
					}
					else{
						int validation = valid2(tag_array,index);
						if(validation == -1){
							Node *head_node = Tree2(data_array,tag_array,index);
							fprintf(output,"Postorder: ");
							height = printPost(head_node, output,0);
							fprintf(output,"\nLevelorder: ");
							int i = 0;
							for(i=0;i<height;i++){
								printLevel(head_node, i, output);
							}
							fprintf(output,"\nTable: \nLSON NODE RSON\n--------------\n");
							printTable(head_node,output);
							fprintf(output,"\n\n");
							}
						else{
							fprintf(output,"Input is invalid.\n\n");
						}
					}
				}
			}
			fclose(input);
		}

		else if(choice==3){
			printf("Exits.\n");
			done = 1;
		}

		else{
			printf("Wrong input.\n");
		}
	}
	fclose(output);
}
Example #17
0
void levelOrder (TNODE_p_t root) {
	int h = height(root), i;
	for (i = 1; i <= h; ++i)
		printLevel(root, i);
}
			std::ostream & print(std::ostream & out)
			{
				for ( int i = static_cast<int>(levelstarts.size())-1; i >= 0; --i )
					printLevel(out,i);
				return out;
			}
Example #19
0
int main(int argc, char *argv[])
{
  time_t lt;
  FILE *file;
  WordList list=NULL;
  char *levelList[100000];
  char word[max];
  char test[100];
  int i=0;
  char *s[50];

  if(argc<4)
  {
    printf("\nUsage: ktouchgen ConfigFile WordFile TrainingFile\n");
    exit(0);
  }

  /**
   * Read in the configFile
   *
   */
  if((file = fopen(argv[1],"r"))==NULL)
  {
    printf("can't open config_file:%s for reading",argv[2]);
  }
  i=0;
  while(!feof(file))
  {
    fscanf(file,"%s",word);

    s[i]=strdup(word);
    printf("%s\n",s[i]);
    i++;
  }
  s[i]=NULL;
  fclose(file);



  /**
   * Read in the wordFile and add each word to the list
   *
   */
  printf("Starting reading words");
  if((file = fopen(argv[2],"r"))==NULL)
  {
    printf("can't open word_file:%s for reading",argv[2]);
  }
  while(!feof(file))
  {
    fscanf(file,"%s",word);
    list=addWord(list, word);
  }
  fclose(file);


  if((file = fopen(argv[3],"w"))==NULL)
  {
    printf("Error when writing to file:%s",argv[3]);
  }

  lt = time(NULL);
  fprintf(file,"#############################################################\n");
  fprintf(file,"# Rrainingfile genereated %s",ctime(&lt));
  fprintf(file,"# Program written by Håvard Frøiland\n");
  fprintf(file,"#############################################################\n\n");

  strcpy(test,"");

  i=0;
  while(s[i]!=NULL)
  {
    if (strlen(test) + strlen(s[i]) + 1 > sizeof(test))
    {
       printf("Buffer overflow.\n");
       exit(1);
    }
    strcat(test,s[i]);
    fprintf(file,"# Level %d\n",i+1);
    fprintf(file,"%s\n", s[i]);
    creatLevelList(list,levelList,test,s[i]);
    printLevel(file,levelList);
    fprintf(file,"\n");
    i++;
  }
  fclose(file);

  return EXIT_SUCCESS;
}
void printLevelOrder(BST* root) {
	int h = getTreeHeight(root);
	int i;
	for(i=1; i<=h; i++)
		printLevel(root, i);
}