int tree_add(tree t, void * e) { int boolean = 0; boolean = tree_get(t,e) != NULL; if (!boolean) { t->header = Binary_Tree(t, e, t->header, t->H, &boolean); t->size++; } return !boolean; }
struct NODE * Binary_Tree (char *List, int Lower, int Upper) { struct NODE *Node; int Mid = (Lower + Upper)/2; Node = (struct NODE*) malloc(sizeof(struct NODE)); Node->Info = List [Mid]; if ( Lower>= Upper) { Node->Left_Child = NULL; Node->Right_Child = NULL; return (Node); } if (Lower <= Mid - 1) Node->Left_Child = Binary_Tree (List, Lower, Mid - 1); else Node->Left_Child = NULL; if (Mid + 1 <= Upper) Node->Right_Child = Binary_Tree (List, Mid + 1, Upper); else Node->Right_Child = NULL; return(Node); }
void main() { int flag; char List[100]; int Number = 0; char Info ; char choice; struct NODE *T = (struct NODE *) malloc(sizeof(struct NODE)); T = NULL; printf("\n Input choice 'b' to break:"); choice = getchar(); while(choice != 'b') { fflush(stdin); printf("\n Input information of the node: "); scanf("%c", &Info); List[Number++] = Info; fflush(stdin); printf("\n Input choice 'b' to break:"); choice = getchar(); } Number --; printf("\n Number of elements in the list is %d", Number+1); T = Binary_Tree(List, 0, Number); printf("\n Tree is \n"); Output(T, 1); fflush(stdin); printf("\n Input the information of the node to which want to search: "); scanf("%c", &Info); flag = Search_Node(T, Info); if (flag) { printf("\n Search is successful \n"); } else printf("Search unsuccessful"); }
void main() { char List[100]; int Number = 0; char Info ; char choice; struct NODE *T = (struct NODE *) malloc(sizeof(struct NODE)); T = NULL; printf("\n Input choice 'b' to break:"); choice = getchar(); while(choice != 'b') { printf("\n Input information of the node: "); scanf("%c", &Info); List[Number++] = Info; fflush(stdin); printf("\n Input choice 'b' to break:"); choice = getchar(); } Number --; printf("\n Number of elements in the lsit is %d", Number); T = Binary_Tree(List, 0, Number); Output(T,1); }
node Binary_Tree(tree t, void * data, node Parent, int *H, int * found) { node Node1; node Node2; if(!Parent) { Parent = (node) malloc(sizeof(struct node)); Parent->data = data; Parent->left = NULL; Parent->right = NULL; Parent->flag = 0; *H = T; return (Parent); } if(t->comp(data, Parent->data) < 0) { Parent->left = Binary_Tree(t, data, Parent->left, H, found); if(*H) /* Left branch has grown higher */ { switch(Parent->flag) { case 1: /* Right heavy */ Parent->flag = 0; *H = F; break; case 0: /* Balanced tree */ Parent->flag = -1; break; case -1: /* Left heavy */ Node1 = Parent->left; if(Node1->flag == -1) { Parent->left= Node1->right; Node1->right = Parent; Parent->flag = 0; Parent = Node1; } else { Node2 = Node1->right; Node1->right = Node2->left; Node2->left = Node1; Parent->left = Node2->right; Node2->right = Parent; if(Node2->flag == -1) Parent->flag = 1; else Parent->flag = 0; if(Node2->flag == 1) Node1->flag = -1; else Node1->flag = 0; Parent = Node2; } Parent->flag = 0; *H = F; } } } else if(t->comp(data, Parent->data) > 0) { Parent->right = Binary_Tree(t,data, Parent->right, H, found); if(*H) /* Right branch has grown higher */ { switch(Parent->flag) { case -1: /* Left heavy */ Parent->flag = 0; *H = F; break; case 0: /* Balanced tree */ Parent->flag = 1; break; case 1: /* Right heavy */ Node1 = Parent->right; if(Node1->flag == 1) { Parent->right= Node1->left; Node1->left = Parent; Parent->flag = 0; Parent = Node1; } else { Node2 = Node1->left; Node1->left = Node2->right; Node2->right = Node1; Parent->right = Node2->left; Node2->left = Parent; if(Node2->flag == 1) Parent->flag = -1; else Parent->flag = 0; if(Node2->flag == -1) Node1->flag = 1; else Node1->flag = 0; Parent = Node2; } Parent->flag = 0; *H = F; } } } else { *found = 1; } return(Parent); }