C/C++ Beginner's Guide and Post Basic Questions here

RBX

In the zone
i am sorry , i am not getting your question..are you making threaded tree? coz you're saying you root node always has a pointer to it;
BTW if you want to set the value NULL if method has been called by pointer to pointer(void abc(node **,int))then you could set the value of node such as tree_1 as NULL by using (*tree_1)->left=NULL ; which i guess is not the question you are asking.

This was the code to be prefixed, I had to design methods around it. Although recursive methods could be designed, I decided to make iterative ones.
Code:
#include <iostream>

using namespace std;

class binarySearchTree;
class treeNode{
    /*
    *with the following friend declaration
    *binarySearchTree class can also access private members of treeNode
    */
    friend class binarySearchTree;
    private:
        int data;
        treeNode * left;
        treeNode * right;
    
    public:
        treeNode(){
            left=right=NULL;
            //parent = NULL;
        }
        treeNode(int key){
            data=key;
            left=right=NULL    ;
            //parent=NULL;
        }
        int getData(){ return data;}
        void setData(int key){data=key;}
};

class binarySearchTree{
    private:
        treeNode * root;
    public:
        binarySearchTree(){root=NULL;}

/*THE ABOVE CODE WILL BE ADDED AS PREFIX TO YOUR CODE**/
/*Finish The Implementation of the binarySearchTree Class*/

This is how I implemented delete (seems to be quite a bad implementation, I'll try to improve it).

Code:
void binarySearchTree::remove(int val) {
    treeNode* temp = root;
    treeNode* tempParent = NULL;    //needs to be tracked so that parent doesn't point to a deleted node

    

    //find the node to be deleted
    if (temp != NULL) {
        while (temp->data != val) {
            if (val < temp->data) {
                tempParent = temp;    //new changes for parent
                temp = temp->left;
            } else if (val > temp->data) {
                tempParent = temp;    //new changes for parent
                temp = temp->right;
            }
        }
    }

    //if leaf node
    if (temp->left == NULL && temp->right == NULL) {
        if (tempParent != NULL) {
            if (temp->data < tempParent->data) tempParent->left = NULL;
            else if (temp->data > tempParent->data) tempParent->right = NULL;
        }
        
       [B] if (temp == root) root = NULL;
        delete(temp);
        temp = NULL;[/B]
        return;
    }

    //if not a leaf node
    //find the replacement node
    treeNode* replace = temp;
    treeNode* replaceParent = NULL;    //needs to be tracked so that childen of replacement can be attached to it
    if (temp->left != NULL) {    //find largest element in left subtree
        replaceParent = replace;
        replace = replace->left;
        while (replace->right != NULL) {
            replaceParent = replace;
            replace = replace->right;
        }

        temp->data = replace->data;    //make replacement
        
        //test
        if (replaceParent != temp) {
            if (replace->left != NULL)    //if replacement is not a leaf node
                replaceParent->right = replace->left;    //attach left-children of replacement to its parent
            else
                replaceParent->right = NULL;    //now parent won't point to the deleted child(replace)
        }

        ////if replacement is a leaf node, replace->left is NULL, so this *should* work in both cases(leaf/non-leaf)
        //replaceParent->right = replace->left;
        if (replaceParent->left == replace) replaceParent->left = replace->left;

        delete(replace);
        replace = NULL;

    } else if (temp->right != NULL) {    //find smallest element in right subtree
        replaceParent = replace;
        replace = replace->right;
        while (replace->left != NULL) {
            replaceParent = replace;
            replace = replace->left;
        }

        temp->data = replace->data;

        if (replaceParent != temp) {
            if (replace->right != NULL)    //if replacement is not a leaf node
                replaceParent->left = replace->right;    //attach right-children of replacement to its parent
            else
                replaceParent->left = NULL;    //now parent won't point to the deleted child(replace)
        }

        ////if replacement is a leaf node, replace->right is NULL, so this *should* work in both cases(leaf/non-leaf)
        //replaceParent->right = replace->left;
        if (replaceParent->right == replace) replaceParent->right = replace->right;




        //replaceParent->left = replace->right;
        delete(replace);
        replace = NULL;
    }

    /**abandoned**/
    /*
    //replace
    if (replace == temp) {    //No replacement found    //leaf node
        free(temp);
        free(replace);
    } else {
        if (replace->data > temp->data) {    //largest in left subtree
            temp->data = replace->data;
            if (replace->left != NULL) {    //if the replacement had left children

            }
        }
    }
    */
}

As can be seen root is a member of BST class, so delete(temp) would just set root to some garbage value, and then if I do temp = NULL, it would just make temp point to NULL, not set root to NULL, so I have to set root to NULL in this dirty way.
 
Last edited:

rohitshubham

Ambassador of Buzz
This was the code to be prefixed, I had to design methods around it. Although recursive methods could be designed, I decided to make iterative ones.
Code:
#include <iostream>

using namespace std;

class binarySearchTree;
class treeNode{
    /*
    *with the following friend declaration
    *binarySearchTree class can also access private members of treeNode
    */
    friend class binarySearchTree;
    private:
        int data;
        treeNode * left;
        treeNode * right;
    
    public:
        treeNode(){
            left=right=NULL;
            //parent = NULL;
        }
        treeNode(int key){
            data=key;
            left=right=NULL    ;
            //parent=NULL;
        }
        int getData(){ return data;}
        void setData(int key){data=key;}
};

class binarySearchTree{
    private:
        treeNode * root;
    public:
        binarySearchTree(){root=NULL;}

/*THE ABOVE CODE WILL BE ADDED AS PREFIX TO YOUR CODE**/
/*Finish The Implementation of the binarySearchTree Class*/

This is how I implemented delete (seems to be quite a bad implementation, I'll try to improve it).

Code:
void binarySearchTree::remove(int val) {
    treeNode* temp = root;
    treeNode* tempParent = NULL;    //needs to be tracked so that parent doesn't point to a deleted node

    

    //find the node to be deleted
    if (temp != NULL) {
        while (temp->data != val) {
            if (val < temp->data) {
                tempParent = temp;    //new changes for parent
                temp = temp->left;
            } else if (val > temp->data) {
                tempParent = temp;    //new changes for parent
                temp = temp->right;
            }
        }
    }

    //if leaf node
    if (temp->left == NULL && temp->right == NULL) {
        if (tempParent != NULL) {
            if (temp->data < tempParent->data) tempParent->left = NULL;
            else if (temp->data > tempParent->data) tempParent->right = NULL;
        }
        
       [B] if (temp == root) root = NULL;
        delete(temp);
        temp = NULL;[/B]
        return;
    }

    //if not a leaf node
    //find the replacement node
    treeNode* replace = temp;
    treeNode* replaceParent = NULL;    //needs to be tracked so that childen of replacement can be attached to it
    if (temp->left != NULL) {    //find largest element in left subtree
        replaceParent = replace;
        replace = replace->left;
        while (replace->right != NULL) {
            replaceParent = replace;
            replace = replace->right;
        }

        temp->data = replace->data;    //make replacement
        
        //test
        if (replaceParent != temp) {
            if (replace->left != NULL)    //if replacement is not a leaf node
                replaceParent->right = replace->left;    //attach left-children of replacement to its parent
            else
                replaceParent->right = NULL;    //now parent won't point to the deleted child(replace)
        }

        ////if replacement is a leaf node, replace->left is NULL, so this *should* work in both cases(leaf/non-leaf)
        //replaceParent->right = replace->left;
        if (replaceParent->left == replace) replaceParent->left = replace->left;

        delete(replace);
        replace = NULL;

    } else if (temp->right != NULL) {    //find smallest element in right subtree
        replaceParent = replace;
        replace = replace->right;
        while (replace->left != NULL) {
            replaceParent = replace;
            replace = replace->left;
        }

        temp->data = replace->data;

        if (replaceParent != temp) {
            if (replace->right != NULL)    //if replacement is not a leaf node
                replaceParent->left = replace->right;    //attach right-children of replacement to its parent
            else
                replaceParent->left = NULL;    //now parent won't point to the deleted child(replace)
        }

        ////if replacement is a leaf node, replace->right is NULL, so this *should* work in both cases(leaf/non-leaf)
        //replaceParent->right = replace->left;
        if (replaceParent->right == replace) replaceParent->right = replace->right;




        //replaceParent->left = replace->right;
        delete(replace);
        replace = NULL;
    }

    /**abandoned**/
    /*
    //replace
    if (replace == temp) {    //No replacement found    //leaf node
        free(temp);
        free(replace);
    } else {
        if (replace->data > temp->data) {    //largest in left subtree
            temp->data = replace->data;
            if (replace->left != NULL) {    //if the replacement had left children

            }
        }
    }
    */
}

As can be seen root is a member of BST class, so delete(temp) would just set root to some garbage value, and then if I do temp = NULL, it would just make temp point to NULL, not set root to NULL, so I have to set root to NULL in this dirty way.
IF root is to be deleted; use the same method, find it's in-order successor and replace it with that data then, continue with process until you reach a node which has single/no child. as technically there can't be a BST without a root. so by this method you could delete the root node by freeing the last node.
 

RBX

In the zone
IF root is to be deleted; use the same method, find it's in-order successor and replace it with that data then, continue with process until you reach a node which has single/no child. as technically there can't be a BST without a root. so by this method you could delete the root node by freeing the last node.

I have problem only in the case when root itself is a leaf node i.e., it's the last node remaining in tree.
 

tanmaymohan

Geek v1.0
n00b question
I recently took PCM with CS and we have C++ in our syllabus. Our school uses only Turbo C++ which used to run in a dos screen. But it is no longer supported in x64 versions of windows and the latest 8.1 .Is there any another GUI or dos based c++ compiler which I can use?

Sometimes we use VM 's with XP to run c++
 

kunalht

In the zone
Code Blocks & Dev C++ are great IDE's
I use Code:Blocks.
& if you want to use Turbo C search on google " Turbo C for 64-bit "
 

rohitshubham

Ambassador of Buzz
code::blocks is the best one out there... it's a fully functional developing environment... and i personally like the interface of code:blocks :p
 

Neuron

Electronic.
n00b question
I recently took PCM with CS and we have C++ in our syllabus. Our school uses only Turbo C++ which used to run in a dos screen. But it is no longer supported in x64 versions of windows and the latest 8.1 .Is there any another GUI or dos based c++ compiler which I can use?

Sometimes we use VM 's with XP to run c++

Use code:blocks or visual c++ express edition and stay away from tc++.
 

tech0freak0

tech is in mah bl00d
I learned basic of C like array , string, structures..
And I also hv some knowledge of c++.
Basically I want to make android apps.
Should I move on to java or to go deep in c programming?
 

ankush28

Bazinga
I learned basic of C like array , string, structures..
And I also hv some knowledge of c++.
Basically I want to make android apps.
Should I move on to java or to go deep in c programming?

Lol you went wrong way!
Android app programming needs knowledge of Java. You can program with C/C++ but its not recommended. In that too you HAVE to learn java. Checkout Android NDK page for more info.

--update--
Plus complication while using other APIs. Better code full using Java.
 
Last edited:

tech0freak0

tech is in mah bl00d
Lol you went wrong way!
Android app programming needs knowledge of Java. You can program with C/C++ but its not recommended. In that too you HAVE to learn java. Checkout Android NDK page for more info.

--update--
Plus complication while using other APIs. Better code full using Java.

yeah....i know android programming needs knowledge of Java.
My point is I have heard that if you have strong grasp on c and c++ ....then u can easily get into java.

I should move on or learn deep into c programming i.e data structures and mangement??
 
Data structure has nothing to with c, c++ or Java. They are just concepts that can be implemented in any language. The reason we are first taught C is that we have to do everything manually, like no provision for accessing length of array other than manually stirring it. So learn a lot data structures and algorithms. If you are good at abstract maths, get "Introduction to Algorithms" by Cormen.

First implement these DS and algorithms in C/C++ then more ahead to Java. The transition will be like from walking on broken glass to gliding on silk.
 

tech0freak0

tech is in mah bl00d
I moving from c to c++.
I don't get the concept and usage of namespace std
Why we using this in cpp??

And What really meanings of gnu99 and anscii??
And What is Object Oriented Language??
 
I moving from c to c++.
I don't get the concept and usage of namespace std
Why we using this in cpp??

And What really meanings of gnu99 and anscii??
And What is Object Oriented Language??

Suppose we both were working on a single project. I created an object named 'foo'. Apparently you also created an object with the same name. Now, when we merged what you and I developed, somehow a conflict occured between the two 'foo's. But neither of us wants to change the object's name in out code. So what do we do? You create your own namespace and I create mine. You place all your code in your namepscae (eg- 'You') and I do the same with my code (eg- 'Me'). Now when we merge the code, all we have to specify is which namepsace the foo being referred to belongs to. We can write You.foo or Me.foo depending on situation. So what happens is the code got divided into two logical independent units. This way and object in your namepsace cannot cause any conflick with any object in my namespace with the same name.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I moving from c to c++.
I don't get the concept and usage of namespace std
Why we using this in cpp??

In general, a namespace is a container for a set of identifiers (also known as symbols, names).[1][2] Namespaces provide a level of indirection to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name. For example, a surname could be thought of as a namespace that makes it possible to distinguish people who have the same first name. In computer programming, namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality.

-Taken from Wikipedia.

endl, cout these belongs to std namespace. So in programs they use using namespace std.

Let me explain in a bit detail. Let's take this program.

#include <iostream>
using namespace std;
void main()
{
cout << "Hello World!" << endl; cout << "Welcome to C++ Programming" << endl;
}

If you don't use using namespace std then compiler won't be able to find cout, endl as they belongs from that namespace. You can also re-write it as

#include <iostream>
void main()
{
std::cout << "Hello World!" << std::endl; std::cout << "Welcome to C++ Programming" << std::endl;
}

:: is a scope resolution operator. Let me explain.

Under iostream header file, there's a namespace named std which means standard. Under this std namespace cout and endl is defined.

As you are coming from C, you know about scope. In c printf is defined in stdio header file and it have no concept of namespace. So writing #include<stdio.h> was enough to bring printf in scope so that compiler can understand what we are referring to.

So it's just a wrapper introduced for many reasons, which you'll learn accordingly while learning C++. So under iostream header file, there's a namespace std, under which cout and endl is defined.

So only referring iostream header file is not enough. You also need to explicitly mention under which namespace those are there.

So if you don't want to use that using namespace std then you have to use it with scope resolution operator like I did in 2nd example, std::cout. So wherever you use any materials defined in std you have to write it like this.

So it's best to use using namespace <namespace_name> above to avoid clutter and the extra typing.

And What really meanings of gnu99 and anscii??

Let someone more experienced explain this. I'm not that good in C++ :p

And What is Object Oriented Language??

If you can make out couple of hours do read this => Introduction to Object Oriented Programming Concepts (OOP) and More - CodeProject
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Thanks for explaining namespace std.
New compiler still needs namespace apart from turbo c++ ??

Don't use TurboC++. The program examples I provided above uses void main() which is wrong actually and that works only in TC++. You should use int main() instead.
 

TheSloth

The Slowest One
i have never used namespace std in my c++ programs. i use cout to print statements. though i used iostream.h.
 
Top Bottom