int bin_tree_search(struct tree_node *p, char *word) { if (p == NULL) return -1; else if (strcmp(p->word, word) < 0) bin_tree_search(p->rchild, word); else if (strcmp(p->word, word) > 0) bin_tree_search(p->lchild, word); else return 0; }
void treeMain(node *list) { t_node *root; node *inList; node *postList; root = buildTree(list); tree_print(root); printf("-999\n"); inList = inOrder(root); printList(inList); printf("-999\n"); freeList(inList); postList = postOrder(root); printList(postList); printf("-999\n"); freeList(postList); printf("%d\n", tree_search(root, 0)); printf("-999\n"); printf("%d\n", tree_search(root, 10)); printf("-999\n"); printf("%d\n", tree_search(root, -2)); printf("-999\n"); printf("%d\n", tree_search(root, 2)); printf("-999\n"); printf("%d\n", tree_search(root, 3)); printf("-999\n"); printf("%d\n", tree_search(root, 9)); printf("-999\n"); printf("%d\n", tree_search(root, 1)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 0)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 10)); printf("-999\n"); printf("%d\n", bin_tree_search(root, -2)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 2)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 3)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 9)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 1)); printf("-999\n"); freeTree(root); }
/*binary search the tree*/ int bin_tree_search(t_node *root, int target) { printf("-1"); if (root) { if (root->data == target) { return 1; } if (target < root->data) { return bin_tree_search(root->left, target); } else { return bin_tree_search(root->right, target); } } return 0; }