0

So as a novice programming I am trying to learn data structures and a question came in my mind while I was working on Binary trees. So the code/function to add a node to binary tree is: //binary tree node addition

 struct tree* addnode(int rdata)
{
    struct tree* temp = new tree();
    temp->data = rdata;
    temp->right = NULL;
    temp->left = NULL;
    return temp;
}

Here we can see that there is only one parameter required in the addition of node i.e. we don't pass the root address in the function. But in addition of linked list the addition of node is at any place (Beginning, ending or after k nodes) has two parameters which are a headref pointer of linked list and the data value. Like code for adding a node in the starting of the linked list is :

    void addatstartLL(struct LL** headref, int rdata) {
    struct LL* temp = new LL();
    temp->data = rdata;
    temp->next = (*headref);
    (*headref) = temp;
}

The above two codes are applied like this :

#include<iostream>
using namespace std;
struct LL {
    int data;
    struct LL* next;
};

struct tree {
    int data;
    struct tree* left;
    struct tree* right;
};
int main()
{
struct tree* root = new tree();
    root->data = 1;
    root->left=addnode(2);
    struct LL* head = NULL;
    addatstartLL(&head, 2);
}

So, my question here why do we need only one parameter in binary tree (only data and not the root address cause) and two parameters in linked list i.e. headref and data? Why don't we write the same kind of function for both of the data structures? Thank you in advanced.

Evg
  • 25,259
  • 5
  • 41
  • 83
Harsh Vats
  • 39
  • 5
  • 1
    Your addnode function just constructs a new node. It doesn't add it to an existing tree. It could do with a better name. – Rup Jun 08 '20 at 08:04
  • 1
    Don't get discouraged by the down votes on your question. Most likely they are because of the very bad style code presented in them - the votes are for the question, not for you. I applaud you for your curiosity and spirit of questioning something that doesn't seem to make sense. So have a +1 from me for asking the right question! – bolov Jun 08 '20 at 08:24
  • Thanks to everyone who has answered. Especially you bolov. – Harsh Vats Jun 08 '20 at 09:02

3 Answers3

4

TLDR They are badly written functions.

Those functions are what you make them to be. If you make one take one argument and the other take 2 arguments then that's what it is. They are not standard functions and they are not well written functions either (in terms of both interface and implementation).

There are many problems with the code you have:

  • addnode doesn't actually add a node to a list. It just creates a node. That's why it takes one argument.

  • LL structure doesn't represent a linked list. It represents a node.

  • You use owning raw pointers everywhere so there is no clear owner of the memory allocated by new. You code will leak at the first exception, even if you explicitly delete the nodes. That's why you need to religiously follow RAII concept in C++.

  • struct tree* root = new tree(); There is absolutely no reason to dynamically allocate tree there in main. tree root{} would suffice.

  • structures are used in the most minimalist way possible, just the bare C capable way. In C++ - true C++ - you would use constructors, encapsulation, methods and so on.

This is by far not idiomatic, correct C++ code. It's C code (sprinkled with C++ IO) and compiled with a C++ compiler. If that's what your teacher requires then by all means write this for him/her to make them happy, but be aware that it's definitely not how you write correct, clean, idiomatic, modern C++ code. If you lean this from a tutorial or book then ditch it immediately and learn from a good C++ resource.

bolov
  • 72,283
  • 15
  • 145
  • 224
3

Your first function doesn't add a node to a tree. It creates a new tree of one node.

It can be used by a function that adds a node to a tree, once the location to add it is determined.

Your second function is adding a node to a list at a specific position. Comparable tree functions would be

void addnodebefore(tree** root, int rdata)
{
    tree* temp = new tree();
    temp->data = rdata;
    temp->right = *root;
    temp->left = nullptr;
    *root = temp;
}

void addnodeafter(tree** root, int rdata)
{
    tree* temp = new tree();
    temp->data = rdata;
    temp->right = nullptr;
    temp->left = *root;
    *root = temp;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
1

There are STL containters similar to what you have, std::set is usually implemented as some type of sorted tree, while std::list is usually implemented as a circular doubly linked list with a dummy node used for the head and tail of the list.

To add a new element to std::set, std::set::insert(value) can be used, and it is a single parameter call.

To add a new element to std::list, std::list::push_front(value) or std::list::push_back(value) can be used, and they are single parameter calls.

Although these are single parameter calls, the container itself could be considered to be similar to having a second parameter. In other words, you could create static functions (not tied to a specific instance of a container) for insert, push_front, or push_back, that would take an instance of the container as one the parameters.

rcgldr
  • 27,407
  • 3
  • 36
  • 61