Exemplo n.º 1
0
void create_binary_tree(int *a, int len, node_t **root, node_t *parent)
{
    if (len > 0) {
        int mid = len/2;
        *root = (node_t *)malloc(sizeof(node_t));
        (*root)->parent = parent;
        (*root)->value = a[mid];
        create_binary_tree(a, mid, &(*root)->left, *root);
        create_binary_tree(a + mid + 1, len - mid - 1, &(*root)->right, *root);
    } else {
        *root = NULL;
    }
}
Exemplo n.º 2
0
int main(){
	bin_tree * r1 = create_binary_tree("input/non-bst.in");	
	bin_tree * r2 = create_binary_tree("input/bst2.in");
	bin_tree * r3 = create_binary_tree("input/is_bst.in");
	inorder(r1);
	puts("");
	pretty_print(r1);
	printf("%d\n\n",is_valid(check_binary_search_tree(r1)));
	inorder(r2);
	puts("");
	pretty_print(r2);
	printf("%d\n\n",is_valid(check_binary_search_tree(r2)));
	inorder(r3);
	puts("");
	pretty_print(r3);
	printf("%d\n\n",is_valid(check_binary_search_tree(r3)));
}
Exemplo n.º 3
0
void create_binary_tree(T** root) {
    char ch;

    ch=getchar();
    while(getchar()!='\n');
    
    if(ch=='#') {
       *root = NULL;
        return ;
    } else {
        *root = malloc(sizeof(T));
        (*root)->d = ch;
        printf("Create %c left child tree:\n",ch);
        create_binary_tree(&(*root)->l);
        printf("Create %c right chile tree:\n",ch);
        create_binary_tree(&(*root)->r);
    }
}
Exemplo n.º 4
0
int main() {
    T* root = NULL;
    create_binary_tree(&root);

    show_binary_tree(root);
    printf("\n");
    char searnum;
    searnum = getchar();

    search_binary_tree(root,searnum);
    return 0;
}
Exemplo n.º 5
0
int main(){
	/*
	* Pass the file containing, a representation of the tree. The
	* function returns the root.
	**/
	bin_tree * root = create_binary_tree("input/dll_test.txt");
	puts("");
	inorder(root);
	puts("");
	pretty_print(root);
	dll * result = convert_binary_tree_to_dll(root);
	print_list(result);
}
Exemplo n.º 6
0
void create_binary_tree(binary_tree *root)
{
    int elem;
    printf("Enter node value(0 is end): ");
    scanf("%d", &elem);

    if (elem == 0)
        *root = NULL;
    else
    {
        *root = (binary_tree)malloc(sizeof(binary_tree_node));
        if (root == NULL)
        {
            printf("malloc error.\n");
            exit(-1);
        }
        (*root)->elem = elem;
        printf("Creating the left child.\n");
        create_binary_tree(&((*root)->left));
        printf("Creating the right child.\n");
        create_binary_tree(&((*root)->right));
    }
}
Exemplo n.º 7
0
int main()
{
    int a[N];
    int i;
    for (i = 0; i < N; ++i)
        a[i] = i;
    node_t *root;
    create_binary_tree(a, N, &root, NULL);
    
    int src;
	node_t *target = find_node(root, 15);
	node_t *head = delete_node(root, target);
	print(head);
    return 0;
}
Exemplo n.º 8
0
int main()
{
    // init tree
    int arr[] = {1, 2, 3, 4, 5, 6};
    TreeNode *root = create_binary_tree(arr, sizeof(arr) / sizeof(int));
    print_binary_tree(root);

    // init list
    ListNode *head = create_list(arr, sizeof(arr) / sizeof(int));
    print_list(head);

    // init vector
    std::vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
    return 1;
}
Exemplo n.º 9
0
int main(int argc, char const *argv[]) {
  Tree *root;// = (Tree*)malloc(sizeof(Tree));
  // TreeElement list[15] = {18, 22, 7, -1, -1, 4, 5, -1, -1, -1, 10, 2, -1, -1, 21};
  TreeElement list[21] = {'-', '+', 'a', ' ', ' ', '*', 'b', ' ', ' ', '-', 'c', ' ', ' ', 'd', ' ', ' ', '/', 'e', ' ', ' ','f'};
  create_binary_tree(&root, list, 21);

  printf("Pre Order:\t");
  pre_order_traverse_recursive(root);
  newline();
  printf("In Order:\t");
  in_order_traverse_recursive(root);
  newline();
  printf("Post Order:\t");
  post_order_traverse_recursive(root);
  newline();
  printf("By Level:\t");
  level_traverse(root);
  return 0;
}
Exemplo n.º 10
0
int main()
{
    binary_tree root;
    init_binary_tree(&root);
    create_binary_tree(&root);

    printf("preorder: \t");
    preorder_traverse_recursive(root);
    printf("\n");

    printf("inorder: \t");
    inorder_traverse_recursive(root);
    printf("\n");

    printf("postorder: \t");
    postorder_traverse_recursive(root);
    printf("\n");

    exit(0);
}
Exemplo n.º 11
0
void init_hs(NetParam *param) {
  param->syn0_hid = create_sync_param(param->syn0_init_file, 
      param->vocab->size, param->layer1_size);
  assert(param->syn0_hid >= 0);

  param->syn1_hid = create_sync_param(param->syn1_init_file, 
      param->vocab->size, param->layer1_size);
  assert(param->syn1_hid >= 0);

  create_binary_tree(param->vocab);

  if (!expTable) {
    int i;
    expTable = (Dtype*)malloc((EXP_TABLE_SIZE + 1) * sizeof(Dtype));
    for (i = 0; i < EXP_TABLE_SIZE + 1; ++i) {
      // Precompute the exp() table
      expTable[i] = exp((i / (Dtype)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP);
      // Precompute f(x) = x / (x + 1)
      expTable[i] = expTable[i] / (expTable[i] + 1);
    }
  }
}