this code was error.please help me fix it

include <iostream>
include <conio.h>
include <algorithm>

using namespace std;
int a[]={1,5,7,4,9,8};
BST*binary_search_tree;

struct BST{
int value;
BST* left;
BST* right;
};

int search_bst(BST* node,int key){
if(node==NULL)
return -1;
if(key<node->value)
return search_bst(node->left,key);
if(key>node->value)
return search_bst(node->right,key);
else
return node->value;

}

void insert_bst(BST* &treeNode,BST* newNode){
if(treeNode==NULL)
treeNode=newNode;
else if (newNode->value<treeNode->value)
insert_bst(treeNode->left,newNode);
else
insert_bst(treeNode->right,newNode);

}

void inorder_bst(BST* tree){
if(tree==NULL)
return;
inorder_bst(tree->left);
cout<<tree->value<<" ";
inorder_bst(tree->right);
}

void binary_tree_sort(int a,int length){
BST item;
for(int i=0;i<length;i++){
item=new BST();
item->value=a[i];
insert_bst(binary_search_tree,item);}
inorder_bst(binary_search_tree);
}
int main()
{
binary_tree_sort(a,6);
return 0;
}

Recommended Answers

All 5 Replies

Lines 9, 53 and 52 are errors

BST*binary_search_tree;

This is an attempt to multiply BST by binary_search_tree. Did you mean that?

Create a binary tree

I'm about to go to bed, so I've not commented much of the code.
You also need to write a function to free the memory used by each node.
Here's your code, with the global variables moved to inside main()

#include <iostream>
//#include <conio.h>
//#include <algorithm>

//for brevity only
using namespace std;

struct BST {
    int value;
    BST* left;
    BST* right;
};

int search_bst(BST* node, int key){
    if (node == NULL)
        return -1;
    if (key<node->value)
        return search_bst(node->left,key);
    if (key>node->value)
        return search_bst(node->right,key);
    else
        return node->value;

}

void insert_bst(BST* &treeNode, BST* newNode){
    if(treeNode == NULL)
    {
        treeNode = newNode;
        return;
    }
    if (newNode->value<treeNode->value)
        insert_bst(treeNode->left, newNode);
    else
        insert_bst(treeNode->right, newNode);

}

void inorder_bst(BST* tree){
    if (tree==NULL)
        return;
    inorder_bst(tree->left);
    cout<<tree->value<<" ";
    inorder_bst(tree->right);
}

void binary_tree_sort(BST* &btree, int* a, int length){
    BST* item;
    for(int i=0; i<length; i++){
        item=new BST();
        item->value=a[i];
        item->left = item->right = NULL;
        insert_bst(btree, item);
    }
    inorder_bst(btree);
}

int main()
{
    int a[] = {1, 5, 7, 4, 9, 8};
    int size = sizeof(a) / sizeof(a[0]);
    BST* binary_search_tree = NULL;

    binary_tree_sort(binary_search_tree, a, size);

    cout << endl;

    int ret = search_bst(binary_search_tree, 7); //  return 7
    cout << ret << "\n";
    ret = search_bst(binary_search_tree, 11); // not found return -1
    cout << ret << endl;

    cin.get();

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.