0

This code for a standard binary search tree does not contain any information, only the value of the nodes. Is there any way I can contain another value, for example, age, in the nodes? So that the node number will be id and the value it is carrying will be the age.Essentially every node would contain a key-value pair. Thank you for your help!

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

struct node
{
int key;
struct node *left, *right;
};

//to create a new BST node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// to do inorder traversal of BST
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d \n", root->key);
inorder(root->right);
}
}

//function to insert a new node with given key in BST
struct node* initialize(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);

/* Otherwise, recur down the tree */
if (key < node->key)
node->left = initialize(node->left, key);
else if (key > node->key)
node->right = initialize(node->right, key);

/* return the  node pointer */
return node;
}
struct node* insert(node* root, int key)
{
    // Create a new Node containing
    // the new element
    node* newnode = newNode(key);

    // Pointer to start traversing from root and
    // traverses downward path to search
    // where the new node to be inserted
    node* x = root;

    // Pointer y maintains the trailing
    // pointer of x
    node* y = NULL;

    while (x != NULL) {
        y = x;
        if (key < x->key)
            x = x->left;
        else
            x = x->right;
    }

    // If the root is NULL i.e the tree is empty
    // The new node is the root node
    if (y == NULL) {
        y = newnode;
    }

    // If the new key is less then the leaf node key
    // Assign the new node to be its left child
    else if (key < y->key){
        y->left = newnode;
    }

    // else assign the new node its right child
    else{
        y->right = newnode;
    }
   
    // Returns the pointer where the
    // new node is inserted
    return y;
}
user840
  • 134
  • 1
  • 13
  • What is the problem? You can have `age` in `node` just as you have `key` in there. – Yksisarvinen Dec 19 '19 at 12:49
  • Why not add it as a member in `node`? – Jaideep Shekhar Dec 19 '19 at 12:49
  • You are allocating memory with malloc and deallocating it with delete. This causes undefined behavior. You could use `malloc` and `free` or `new`/`new[]` and `delete`/`delete[]` or smart pointers and containers. – Thomas Sablik Dec 19 '19 at 13:00
  • You should not use `malloc` in C++ (except in very rare cases that you will probably never encounter), and absolutely not combine it with `delete`. – molbdnilo Dec 19 '19 at 13:00

2 Answers2

1

Just declare the node like

template <typename Key, typename Value>
struct node
{
    Key key;
    Value value;
    struct node *left, *right;
};

Pay attention to that instead of malloc you should use the operator new.

For example

template <typename Key, typename Value>
struct node<Key, Value>  *newNode( const Key &key, const Value &value )
{
    return new node<Key, Value> { key, value, nullptr, nullptr };
}

That is if you are writing a C++ program then use C++ constructions.

The function insert can be defined the following way

template <typename Key, typename Value>
struct node
{
    Key key;
    Value value;
    struct node *left, *right;
};

template <typename Key, typename Value>
struct node<Key, Value>  * newNode( const Key &key, const Value &value )
{
    return new node<Key, Value> { key, value, nullptr, nullptr };
}


template <typename Key, typename Value>
void insert( node<Key, Value> * &head, const Key &key, const Value &value )
{
    node<Key, Value> **current = &head;

    while ( *current != nullptr )
    {
        if ( key < ( *current )->key ) current = &( *current )->left;
        else current = &( *current )->right;
    }

    *current = newNode( key, value );
}                             

In main you can write

int main()
{
    node<int, unsigned int> *head = nullptr;

    //…
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

If this is not a university project, don't bother creating your own Binary Search Tree unless you really want to learn how to do that. As C++ provides you a Binary Search Tree in the form of std::map and std::set

std::map<int, std::string> myMap

defines a tree that uses integers as keys, and stores std::strings.

Tomaz Canabrava
  • 2,320
  • 15
  • 20