Beispiel #1
0
int
main(void)
{ 
      tree_node_t *bs_treep;   /* binary search tree 	*/
      int          data_key;   /* input - keys for tree  	*/
      int          status;     /* status of input operation	*/
 
      bs_treep = NULL;   /* Initially, tree is empty */
 
      /* As long as valid data remains, scan and insert keys, 
         displaying tree after each insertion. */
      for  (status = scanf("%d", &data_key);
            status == 1;
            status = scanf("%d", &data_key)) {
          bs_treep = tree_insert(bs_treep, data_key);
          printf("Tree after insertion of %d:\n", data_key);
          tree_inorder(bs_treep);
      }
    
      if (status == 0) {
            printf("Invalid data >>%c\n", getchar());
      } else {
            printf("Final binary search tree:\n");
            tree_inorder(bs_treep);
      }
 
      return (0);
}
Beispiel #2
0
/**
 * Prints the keys in the tree inorder.
 * @param t tree to print.
 * @param f(char *s) printing function to use.
 */
void tree_inorder(tree t, void f(char *s))
{
  if(t == NULL){
    return;
  }else{
    tree_inorder(t->left,f);
    f(t->key);
    tree_inorder(t->right,f);
  }
}
Beispiel #3
0
//트리 중위 순회 
//트리 노드를 인자로 받음 
void tree_inorder(TreeNode *root)
{
    if(root){
         //루트 노드의 왼쪽 자식노드인 중위 순회 실행 
         tree_inorder(root->left);
         //현재 노드의 데이터 출력 
         printf("%d ", root->data);
         // 루트 노드의 오른쪽 자식노드인 중위 순회 실행 
         tree_inorder(root->right);
    }
} 
bool tree_inorder(struct node_dll *head, struct node *root)
{
	if (root == NULL || head == NULL)
		return true;
	tree_inorder(head,root->left);
	
	if (head->data != root->data)
		return false;

	head = head->next;
	tree_inorder(head, root->right);
}
int is_identical(struct node_dll *head, struct node *root){
	bool correct = true;
	if (head == NULL || root == NULL)
		return -1;
	
	if (tree_inorder(head, root))
		return 1;

	return 0;
}
Beispiel #6
0
int main(void)
{
    //트리를 구성할 루트노드 
    TreeNode *root=NULL;
    //트리에 넣을 값들을 받을 정수형 배열 
    int val[100];
    //반복 구문을 수행할 정수형 변수 
    int i, j;
    
    //트리에 구성할 값들을 입력 받기 
    //설명문 출력 
    printf("트리를 구성할 값을 입력하세요.\n(정수 범위: 1~100 / 최대 100개 입력 가능 / 종료는 101)\n");
    //트리에 넣을 값을 배열에 저장(최대 100번 반복)
    for(i=0;i<100;i++)
    {
         //트리에 넣을 값 입력 
         scanf("%d", &val[i]);
         //101를 입력받으면 반복 종료 
         if(val[i]==101)
              break;
         //입력 범위를 초과하면 
         else if(val[i]>101)
              //오류 메세지 출력 
              error("값을 초과하였습니다.");
    }
    
    //입력 값 출력 및 입력 값 트리에 삽입 
    printf("\n입력 값 \n");
    //입력 한 값들의 갯수만큼 반복 
    for(j=0;j<i;j++)
    {
         //입력 값 확인을 위한 출력 
         printf("%d ", val[j]);
         //입력 값 트리에 삽입 
         tree_insert_node(&root, val[j]);
    }
    //출력 구분을 위한 개행 
    printf("\n-------------------------------------------------------\n");

    //전위 순회 함수를 통해 전위 순회 출력 
    printf("전위 순회 출력 결과\n"); 
    tree_preorder(root);
    
    //중위 순회 함수를 통해 전위 순회 출력 
    printf("\n중위 순회 출력 결과\n"); 
    tree_inorder(root);
    
    //후위 순회 함수를 통해 전위 순회 출력 
    printf("\n후위 순회 출력 결과\n"); 
    tree_postorder(root);
    printf("\n");
    
    //입력 받은 값 트리에서 삭제하기 
    //설명문 출력 
    printf("\n트리에서 삭제할 값을 순서대로 입력하세요!(종료는 101) \n");
    //트리에서 삭제할 값 배열에 저장(최대 100번 반복)
    for(i=0;i<100;i++)
    {
         //삭제할 값 입력 
         scanf("%d", &val[i]);
         //101을 입력받으면 반복 종료 
         if(val[i]==101)
              break;
         //입력 범위를 초과하면 
         else if(val[i]>101)
              //오류 메세지 출력 
              error("값을 초과하였습니다.");
    }
    
    //입력 값 출력 및 입력 값 트리에서 삭제 
    printf("\n입력 값 \n");
    //입력 한 값들의 갯수만큼 반복 
    for(j=0;j<i;j++)
    {
         //삭제 값 확인을 위한 출력 
         printf("%d ", val[j]);
         //트리에서 해당되는 노드 삭제  
         tree_delete_node(&root, val[j]);
    }
    
    //출력 구분을 위한 개행 
    printf("\n-------------------------------------------------------\n");
    //트리 노드 개수 구하는 함수를 통한 개수 출력 
    printf("삭제 후 트리의 노드 개수 : %d\n", tree_get_node_count(root));
    //트리 높이 구하는 함수를 통한 높이 출력 
    printf("삭제 후 트리의 높이 : %d\n", tree_height(root));
    //레벨 순회 함수를 통한 레벨 순회 출력
    printf("삭제 후 트리의 레벨 순회 \n");
    tree_level_order(root); 
    printf("\n");
    
    system("pause");
    return 0;
}
int main(void)
{
	/******************************VARS**********************************************/
	char input1[]={"enrollment_list\0"};	//input1 file 1
	char input2[]={"drop_list\0"};		//input1 file 2
	char output[]={"classlist.txt\0"};		//output file
	Tree students;								//class list
	
	//message to user that output file is done
	
	char done[]={"The class list was saved in the file\0"};
	
	/*************************VARS**************************************************/ 

	FILE *enroll;		//enrollment file stream
	FILE *drop;		//drop file stream
	FILE *classlist;	//output file stream

	//Open the input files. Exit if file(s) could not be opened.

	if(!(enroll=fopen(input1,"r")))
	{
		printf("The enrollment file did not open properly. The program will now terminate.\n");
		return -1;
	}

	if(!(drop=fopen(input2,"r")))
	{
		printf("The drop file did not open properly. The program will now terminate.\n");
		return -1;
	}

	//Initialize the list and read in the data from the enrollment file.
	
	tree_init(&students);
	int goodData=populate(enroll,&students);

	//Exit if file was improperly formatted (returns 0).
	//Exit if the file did not close (returns 2).

	if(goodData==0)
	{
		printf("The enrollment file was improperly formatted. The program will now terminate.\n");
		return -1;
	}
	else if(goodData==2)
	{
		printf("The enrollment file did not close properly. The program will now terminate.\n");
		return -1;
	}
	
	//Read in the data from the drop file and delete records from tree.
	
	goodData=deleteFromFile(drop,students);

	//Exit if file was improperly formatted (returns 0).
	//Exit if the file did not close (returns 2).

	if(goodData==0)
	{
		printf("The drop file was improperly formatted. The program will now terminate.\n");
		return -1;
	}
	else if(goodData==2)
	{
		printf("The drop file did not close properly. The program will now terminate.\n");
		return -1;
	}
		

	//Open the output file. Exit if the file could not be opened.

	if(!(classlist=fopen(output,"w")))
	{
		printf("The output file did not open properly. The program will now terminate.\n");
		return -1;
	}
	
	//Print the list to the output file.
	
	fprintf(classlist,"Class List for CS 300:\n\n");
	tree_inorder(students,classlist);
	int closed=(fclose(classlist));
	
	//Exit if the file did not close (returns 0).
	
	if(closed!=0)
	{
		printf("The output file did not close properly. The program will now terminate.\n");
		return -1;
	}
	
	//Tell user that output file is done.
	
	printf("%s %s.\n",done,output);
	
	//Close and exit the program.
	
	return 0;
}