Exemple #1
0
void insert_tree(int num, tree_node *node) {
    /* 1つも挿入されていない場合 */
    if(node == NULL) {
        tree_root = create_new_node(num);
        return;
    }

    /* numが現在のnodeの値よりも小さい場合 */
    if(node->value > num) {
        if(node->left != NULL) {
            insert_tree(num, node->left);
        } else {
            /* 左側に追加する */
            node->left = create_new_node(num);
        }
    } else {
    /* numが現在のnodeの値以上の場合 */
        if(node->right!=NULL) {
            insert_tree(num, node->right);
    	} else {
            /* 右側に追加する */
            node->right = create_new_node(num);
        }
    }

    return;
}
void
edit_interface_rep::complete_start (string prefix, array<string> compls) {
  // check consistency
  tree st= subtree (et, path_up (tp));
  if (is_compound (st)) return;
  string s= st->label;
  int end= last_item (tp);
  if ((end<N(prefix)) || (s (end-N(prefix), end) != prefix)) return;

  // perform first completion and switch to completion mode if necessary
  if (N (compls) == 1) {
    string s= compls[0];
    if (ends (s, "()")) // temporary fix for Pari
      insert_tree (s, path (N(s)-1));
    else insert_tree (s);
    completions= array<string> ();
  }
  else {
    completion_prefix= prefix;
    completions      = close_completions (compls);
    completion_pos   = 0;
    insert_tree (completions[0]);
    complete_message ();
    beep ();
    set_input_mode (INPUT_COMPLETE);
  }
}
Exemple #3
0
tree_t insert_tree(int elt, tree_t tree) {
    if (tree_isEmpty(tree)) return tree_make(elt, tree_make(), tree_make());
    if (elt <= tree_elt(tree)) return tree_make(tree_elt(tree), 
        insert_tree(elt, tree_left(tree)), tree_right(tree));
    return tree_make(tree_elt(tree), 
        tree_left(tree), insert_tree(elt, tree_right(tree)));
}
Exemple #4
0
tree* insert_tree(tree** t, const void* key, int key_sz, const tree* parent,
                  int (*compare_keys)(const void* key1, const void* key2)) {
    tree* p;

    if(*t == NULL) {
        p = malloc(sizeof(tree));

        p->key = malloc(key_sz);
        p->key_sz = key_sz;
        memcpy(p->key, key, key_sz);

        p->item = NULL;

        p->left = p->right = NULL;
        p->parent = (tree*)parent;

        *t = p;
        return *t;
    }

    int cmp = compare_keys(key, (*t)->key);
    if(cmp < 0)
        return insert_tree(&((*t)->left), key, key_sz, *t, compare_keys);
    else
        return insert_tree(&((*t)->right), key, key_sz, *t, compare_keys);
}
Exemple #5
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
	int
main ( int argc, char *argv[] )
{
	BStree *root = NULL;
	printf("insert the data 1 7 4 8 3 6 5\n");
	root = insert_tree(root, NULL, 2);
	delete_tree(root, 2);
	root = NULL;
	root = insert_tree(root,NULL, 2);
	insert_tree(root, NULL, 1);
	insert_tree(root, NULL, 7);
	insert_tree(root, NULL, 4);
	insert_tree(root, NULL, 8);
	insert_tree(root, NULL, 3);
	insert_tree(root, NULL, 6);
	insert_tree(root, NULL, 5);

	print_tree(root);

	printf("delete 7\n");
	delete_tree(root, 7);
	print_tree(root);
	printf("delete 2\n");
	delete_tree(root, 2);
	print_tree(root);

	printf("find 8\n");
	printf("%d\n", find_tree(root, 8)->element);
	clear(root);
	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
Exemple #6
0
tree *insert_tree(tree *root, char c) {

  if (root == NULL) {
    root = make_node(c, NULL, NULL);
  } else if (c < root->data) {
    root->left = insert_tree(root->left, c);
  } else {
    root->right = insert_tree(root->right, c);
  }
  return root;
}
Exemple #7
0
tree_node* build_decision_tree(map<char, string> morse_code){
	tree_node* root = (tree_node*)malloc(sizeof(tree_node));
	root->data = '*';
	root->left = NULL;
	root->right = NULL;

	for (map<char, string>::iterator itor = morse_code.begin(); 
		itor != morse_code.end(); ++itor){
		if (itor->second.at(0) == '.')
			root->left = insert_tree(root->left, itor->first, itor->second);
		else
			root->right = insert_tree(root->right, itor->first, itor->second);
	}

	return root;
}
Exemple #8
0
int main()
{
    char _[] = "deadbeef";
    tree_t t = create_tree(_, strlen(_));

    inorder_traversal(t, print_node);
    sep;
    printf("%c %c %c", retrieve(find_min(t)), retrieve(find_max(t)),
                       retrieve(find('a', t)));
    sep;

    t = insert_tree('z', t);
    printf("%c %c %c", retrieve(find_min(t)), retrieve(find_max(t)),
                       retrieve(find('a', t)));
    sep;
    inorder_traversal(t, print_node);
    sep;
    
    delete_tree('z', t);
    printf("%c %c %c", retrieve(find_min(t)), retrieve(find_max(t)),
                       retrieve(find('a', t)));
    sep;
    inorder_traversal(t, print_node);

    dispose_tree(t);

    return 0;
}
GsTreeBase::GsTreeBase ( const GsTreeBase& t )
 {
   _root = _cur = NIL;
   _elements = 0;
   _man = t._man;
   insert_tree ( t );
 }
Exemple #10
0
void insert_tree(node **l, int x) {
  node *p;
  
  if (*l == NULL) {
    p = malloc(sizeof(node));
    p->val = x;
    p->left = p->right = NULL;
    *l = p;
    return;
  }
  
  if (x < (*l)->val)
    insert_tree(&((*l)->left), x);
  else
    insert_tree(&((*l)->right), x);  
}
Exemple #11
0
void
edit_interface_rep::key_press (string gkey) {
  string zero= "a"; zero[0]= '\0';
  string key= replace (gkey, "<#0>", zero);
  if (pre_edit_mark != 0) {
    ASSERT (sh_mark == 0, "invalid shortcut during pre-edit");
    mark_cancel (pre_edit_mark);
    pre_edit_s= "";
    pre_edit_mark= 0;
  }
  if (starts (key, "pre-edit:") ) {
    interrupt_shortcut ();
    string s= key (9, N(key));
    if (s == "") return;
    int i, n= N(s), pos= N(s);
    for (i=0; i<n; i++)
      if (s[i] == ':' && is_int (s (0, i))) {
        int k= as_int (s (0, i));
        s= s (i+1, n);
        pos= 0;
        for (int j=0; j<k && pos<N(s); j++)
          tm_char_forwards (s, pos);
        break;
      }
    pre_edit_s= s;
    pre_edit_mark= new_marker ();
    mark_start (pre_edit_mark);
    archive_state ();
    insert_tree (compound ("pre-edit", s), path (0, pos));
    return;
  }
  
  string new_sh= N(sh_s)==0? key: sh_s * " " * key;
  if (try_shortcut (new_sh)) return;
  if (new_sh != key) {
    interrupt_shortcut ();
    if (try_shortcut (key)) return;
  }
  
  string rew= sv->kbd_post_rewrite (key);
  if (N(rew) == 1) {
    int i ((unsigned char) rew[0]);
    if ((i >= 32 && i <= 127) || (i >= 128 && i <= 255) || (i == 25))
      if (!inside_active_graphics ()) {
        archive_state ();
        call ("kbd-insert", rew);
      }
    interrupt_shortcut ();
  }
  else if (contains_unicode_char (rew)) {
    archive_state ();
    call ("kbd-insert", key);
    interrupt_shortcut ();    
  }
  else if (DEBUG_KEYBOARD)
    debug_keyboard
      << "unrecognized key " << key << ". "
      << "Undefined shortcut or key missing in the encoding files.\n";
}
Exemple #12
0
void
edit_select_rep::selection_move () {
  observer pos= position_new (tp);
  tree t= selection_get_cut ();
  go_to (position_get (pos));
  insert_tree (t);
  position_delete (pos);
}
void
edit_process_rep::generate_table_of_contents (string toc) {
  if (DEBUG_AUTO)
    debug_automatic << "Generating table of contents [" << toc << "]\n";
  tree toc_t= buf->data->aux[toc];
  if (buf->prj != NULL) toc_t= copy (buf->prj->data->aux[toc]);
  if (N(toc_t)>0) insert_tree (remove_labels (toc_t));
}
Exemple #14
0
int main(void) {
    int i, action;

    for(i = 0; i < 10; i++) {
        insert_tree(rand() % 99 + 1, tree_root);
    }
    for(;;) {
        print_tree(0, tree_root);
        printf("実行する操作のタイプを入力してください。\n \
            1 :追加\t2 :検索\t3 :削除\t それ以外:終了>");
        scanf("%d", &action);
        switch(action) {
        case 1:
            printf("1 〜100の範囲で,追加する数字を"
                                            "入力してください:");
            scanf("%d", &i);
            if(i < 1 || i > 100) {
                continue;
            }
            insert_tree(i, tree_root);
            break;
        case 2:
            printf("検索する数字を入力してください:");
            scanf("%d", &i);
            if(find_value(tree_root, i) != NULL) {
                printf("%dを発見しました\n", i);
            } else {
                printf("%dは見つかりませんでした\n", i);
            }
            break;
        case 3:
            printf("削除する数字を入力してください:");
            scanf("%d", &i);
            if(delete_tree(i) == 1) {
                printf("%dを削除しました\n", i);
            } else {
                printf("%dは見つかりませんでした\n", i);
            }
            break;
        default:
            free_tree(tree_root);
            return EXIT_SUCCESS;
        }
    }
}
Exemple #15
0
void insert_tree(int data,AVLTree &node)
{
    if (node == NULL)
    {
        node = (AVL*)malloc(sizeof(AVL));
        node->data = data;
        node->height = 0;
        node->lchild = node->rchild = NULL;
        printf("test01\n");
        return;
    }
    else if (data < node->data)
    {
        insert_tree(data, node->lchild);
    }
    else if (data > node->data)
    {
        insert_tree(data, node->rchild);
    }
    
    node->height = Max(Height(node->lchild), Height(node->rchild)) + 1;
    
    if (Height(node->lchild) - Height(node->rchild) == 2)
    {
        if (node->lchild->data > data)
        {
            SingleRotateWithLeft(node);
        }
        else
        {
            DoubleRotateWithLeft(node);
        }
    }
    else if (Height(node->rchild) - Height(node->lchild) == 2)
    {
        if (node->rchild->data < data)
        {
            SingleRotateWithRight(node);
        }
        else
        {
            DoubleRotateWithRight(node);
        }
    }
}
void insert_tree(tree **l, int x/*, tree *parent*/)
{
    tree *p;       /* temp pointer */

    if (*l == NULL) {
        p = malloc(sizeof(tree));
        p->item = x;
        p->left = p->right = NULL;
        /*p->parent = parent;*/
        *l = p;     /* link into parent's record */
        return;
    }

    if (x < (*l)->item)
        insert_tree(&((*l)->left), x/*, *l*/);
    else
        insert_tree(&((*l)->right), x/*, *l*/);
}
Exemple #17
0
struct node *create_tree (int num_nodes, int branching_factor, int *values) {
    struct node *root = (struct node *) malloc (sizeof (struct node));
    root->value = values[0];
    int count;
    for (count = 1; count < num_nodes; count++) {
        insert_tree (root, values[count]);
    }
    return root;
}
Exemple #18
0
int main()
{
    AVLTree root = NULL;
    
    insert_tree(2, root);
    insert_tree(4, root);
    insert_tree(6, root);
    insert_tree(5, root);
    printf("insert over\n");
    print_tree(root);
    
    delete_node(root, 5);
    
    printf("after\n");
    print_tree(root);
    
    return 0;
}
void insert_tree(tree **t, item_type x, tree *parent)
{
    if (*t == NULL)
    {
        tree *p = malloc(sizeof(tree));
        p->item = x;
        p->parent = parent;
        p->left = NULL;
        p->right = NULL;
        *t = p;
        return;
    }

    if ((*t)->item > x)
        insert_tree(&((*t)->left), x, *t);
    else
        insert_tree(&((*t)->right), x, *t);
}
Exemple #20
0
void insert_tree (struct node*node, int value) {
    if (node != null) {
        if (node->value > value) {
            if (node->left == null) {
                node->left = create_node (value);
            } else {
                insert_tree (node->left, value);
            }
        } else {
            if (node->value < value) {
                if (node->right == null) {
                    node->right = create_node (value);
                } else {
                    insert_tree (node->right, value);
                }
            }
        }
    }
}
Exemple #21
0
void insert_tree(int tree[], int n, int root)
{
    int left, right;

    if (tree[root] == -1) {
        printf("put %d to node [%d]\n", n, root);
        tree[root] = n;
        return;
    }

    left = 2 * root + 1;
    right = 2 * root + 2;

    // go to left child
    if (n <= tree[root])
        insert_tree(tree, n, left);
    // go to right child
    else
        insert_tree(tree, n, right);
}
Exemple #22
0
void
edit_select_rep::selection_paste (string key) {
  tree t; string s;
  (void) ::get_selection (key, t, s, selection_import);
  if (inside_active_graphics ()) {
    if (is_tuple (t, "texmacs", 3))
      call ("graphics-paste", t[1]);
    return;
  }
  if (is_tuple (t, "extern", 1)) {
    string mode= get_env_string (MODE);
    string lan = get_env_string (MODE_LANGUAGE (mode));
    if ((selection_import == "latex") && (mode == "prog")) mode= "verbatim";
    if ((selection_import == "latex") && (mode == "math")) mode= "latex-math";
    if ((selection_import == "html") && (mode == "prog")) mode= "verbatim";
    string fm= selection_import * "-snippet";
    tree doc= generic_to_tree (selection_decode(lan, as_string(t[1])), fm);
    if (is_func (doc, DOCUMENT, 1)) doc= doc[0]; // temporary fix
    insert_tree (doc);
  }
  if (is_tuple (t, "texmacs", 3)) {
    string mode= get_env_string (MODE);
    string lan = get_env_string (MODE_LANGUAGE (mode));
    if (is_compound (t[1], "text", 1) && mode == "text")
      t= tuple ("texmacs", t[1][0], "text", lan);
    if (is_compound (t[1], "math", 1) && mode == "math")
      t= tuple ("texmacs", t[1][0], "math", lan);
    if (mode == "math" && t[2] == "text")
      set_message ("Error: invalid paste of text into a formula", "paste");
    else if (mode == "prog" && t[2] == "math") {
      tree in= tuple (lan, t[1]);
      tree r= stree_to_tree (call ("plugin-math-input", tree_to_stree (in)));
      insert_tree (r);
    }
    else {
      if ((t[2] != mode) && (t[2] != "src") && (mode != "src") &&
	  ((t[2] == "math") || (mode == "math"))) {
        if (t[2] == "math")
          insert_tree (compound ("math", ""), path (0, 0));
        else if (t[2] == "text")
          insert_tree (compound ("text", ""), path (0, 0));
        else
          insert_tree (tree (WITH, copy (MODE), copy (t[2]), ""), path (2, 0));
      }
      if (is_func (t[1], TFORMAT) || is_func (t[1], TABLE)) {
	int row, col;
	path fp= search_format (row, col);
	if (is_nil (fp)) insert_tree (compound (copy (TABULAR), t[1]));
	else table_write_subtable (fp, row, col, t[1]);
      }
      else insert_tree (t[1]);
    }
  }
}
Exemple #23
0
int main(void)
{
    FILE *fp;       // ファイルポインタ
    int c;          // 入力文字列
    char tmp[WRD_LEN];  // 格納文字列
    int len = 0;        // 文字の長さ
    int state = S_SPACE;    // 状態
    
    if ((fp = fopen(FILE_NAME, "r")) == NULL)
    {
        printf("File Open Error: %s\n", FILE_NAME);
        return 1;
    }
    
    // 終わりまで1文字ずつ取得
    while ((c = getc(fp)))
    {
        // 空白文字か判別
        if (isspace(c) || c == EOF)
        {
            if (state == S_WORD)    // 文字が入力されていたなら
            {
                tmp[len] = '\0';    // ヌル文字の挿入
                insert_tree(tmp);   // 木に追加
            }
            
            if (c == EOF)   // 繰り返しを抜ける
                break;
            
            len = 0;            // 文字数の初期化
            state = S_SPACE;    // 状態の変更
        }
        // 空白文字でない場合(単語の場合)
        else
        {
            tmp[len++] = c; // 文字の保存
            state = S_WORD; // 状態の変更
        }
    }
    
    fclose(fp);
    
    //show_tree(&root);
    
    find_max(&root);    // 最大頻度単語を探す
    printf("最大頻度単語:%s, 頻度:%d\n", max.wrd, max.freq);
    
    free_tree(&root);   // 木の解放

    return 0;
}
Exemple #24
0
void insert_tree(Tree **node, int num)
{
	Tree *new_node;
	if(*node == NULL)
	{
		new_node = malloc(sizeof(Tree));
		new_node->data = num;
		new_node->left = NULL;
		new_node->right = NULL;
		*node = new_node;
		return;
	}
	else{
		if ((*node)->data > num)
		{
			insert_tree(&(*node)->left, num);
		}
		else
		{
			insert_tree(&(*node)->right, num);
		}
	}
}
Exemple #25
0
//---------------------------------------------------
int main()
{
int val,i;
printf("Enter values for the first tree\n");
for(i=0;i<8;i++)
{
printf("Enter the value\n");
scanf("%d",&val);
insert_tree(val);
inorder(root);
}
dump(root);
display(queue);
return 0;
}
Exemple #26
0
tree_node* insert_tree(tree_node* node, char ch, string code){
	if (code.length() == 0)
		return node;

	if (node == NULL){
		node = (tree_node*)malloc(sizeof(tree_node));
		node->left = NULL;
		node->right = NULL;		
		if (code.length() > 1) 
			node->data = ' ';
		else{
			node->data = ch;
			return node;
		}
	}

	if (code.at(0) == '.'){
		node->left = insert_tree(node->left, ch, code.substr(1));	
	}else{
		node->right = insert_tree(node->right, ch, code.substr(1));
	}

	return node;
}
Exemple #27
0
/*
 * insert a element in the tree
 */
BStree *insert_tree(BStree *tree, BStree *parent, item_type element)
{
	if(tree == NULL)
	{
		tree = (BStree *)malloc(sizeof(BStree));
		tree->element = element;
		tree->parent = parent;
		tree->right = tree->left = NULL;
		if(parent != NULL)
		{
			if(element < parent->element)
				parent->left = tree;
			else
				parent->right = tree;
		}
		return tree;
	}
	else if( element < tree->element)
		return insert_tree(tree->left, tree, element);
	else if( element > tree->element)
		return insert_tree(tree->right, tree, element);
	else
		return tree;
}
Exemple #28
0
int main()
{
    int n[NUM] = {6, 5, 23, 81, 9, 6, 32, 75, 60, 1};
    //int n[NUM] = {7, 4, 2, 25, 3, 10};
    int tree[MAX];
    int i;

    // initialize binary search tree
    for (i = 0; i < MAX; i++)
        tree[i] = -1;

    for (i = 0; i < NUM; i++)
        insert_tree(tree, n[i], 0);
    print_tree(tree, MAX);
}
Exemple #29
0
tree *read_tree( void ) {
  int i = 1;
  char c;
  tree *tp = NULL;
  				
  c = getchar();
  grab[0] = c;
  while (c != '.') {
    tp = insert_tree(tp, c);
    c = getchar();
    grab[i] = c;
    i++; 
    count++;
  }
  return tp;
}
Exemple #30
0
/* Create a binary tree on an array of integer 
 */
void build_binary_tree(node **root, int *input, int size) {
  node *head;
  int i;
  
  printf("%s\n", __FUNCTION__);
  
  if (size == 0) {
    printf("array size is 0\n");
    return;
  }
  
  for (i = 0; i < size; i++) {
    insert_tree(&head, input[i]);
  }
  *root=head;
}