I don't ensure whether "0" or "NULL" is better to be an empty point's value in C++. Could anybody can tell me,and why?
Then how about it in C, why?
Thank you!
As answered by Stroustrup:
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
Just because it is possible to push twigs along the ground with one’s nose does not necessarily mean that that is the best way to collect firewood.
I am sorry for that I can't understand what's this sentence's meaning? My English is poor.
Can you tell me what is your opinion in whether use "NULL" in C++?
Thank you very much !
>I am sorry for that I can't understand what's this sentence's meaning?
Where did you find that sentence? It's nowhere in this thread.
>Can you tell me what is your opinion in whether use "NULL" in C++?
0 and NULL are the same thing in C++. Pick one and use it consistently.
> Where did you find that sentence? It's nowhere in this thread.
Yes, its my damned signature. :D
Get a more interesting signature! If I don't notice it, it's not worth reading.
Pushing twigs with one's nose never was interesting... :-)
Plus, the more interesting my signatures become, the more debate it sparks and more are the threads created in the Moderators Place. ;-)
The Moderator's Place has been too slow anyway. If you want, I can suggest one:
I hate VB.NET’s continuous bloody interference. I HADN’T FINISHED TYPING YET YOU STUPID COMPILER! CAN’T YOU SEE THAT? DOES IT LOOK TO YOU LIKE I’M DONE TYPING? DID IT NOT OCCUR TO YOU THAT THE REASON YOU’VE FOUND ALL THOSE ERRORS IS BECAUSE I’M NOT FINISHED YET?!! I’LL TELL YOU WHEN I WANT YOU TO CHECK MY WORK, AND NOT BEFORE!
-- Ian Griffiths
Ah, that guy can totally read my mind. ;)
Heh good one. But since it seems that I seem to be the center of most discussions there, maybe its time someone else started playing the part of a scape goat. ;-)
I can't imagine why you're so popular. I'm several orders of magnitude worse than you and nobody bats an eyelash. :icon_rolleyes: Maybe you're too nice. Try being meaner and you'll be accepted.
> I can't imagine why you're so popular.
Friendly would be more like it.
> I'm several orders of magnitude worse than you and nobody bats an eyelash.
Maybe they have got used to you.
> Maybe you're too nice. Try being meaner and you'll be accepted.
Maybe an official training from you would do the job. ;-)
:sweat: En,this,that......English sometimes is more important than C++,Ha!
But I have another intresting questions:
What's the differences between struct and class(all member is public)?
Look at these codes:
BinarySearchTree.h
BinarySearchTree.cpp
main.cpp
When I compiled them with Dev-cpp(gcc4.1 compiler) in windows,the command line is like this:> g++ -Wall -o main.exe main.cpp BinarySearchTree.cpp
BinarySearchTree.cpp:116: error: expected constructor,destructor,or type conversion before '' token
BinarySearchTree.cpp:123: error: expected constructor,destructor,or type conversion before '' token
BinarySearchTree.cpp:165: error: expected constructor,destructor,or type conversion before '*' token
I think the errors from the class BinaryNode. When Weiss write it,he use the "BinaryNode sturct",most of the others are the same.
My friends told me to study OOP, so I didn't use the template for simple. And he say that the "BinaryNode sturct is too 'C' ",then I rewrite it to class. But the problems comes
I need your help to know :
1.What's the differences between struct and class(all members is public)?
2.How to make this program runs?
Thanks!
#include "BinarySearchTree.h"
#include <iostream>
using namespace std;
//In all of the following algorithms,
//you will see that Mr Weiss used recursive
//functions incisively and vividly!
class BinarySearchTree;
BinarySearchTree::BinarySearchTree()
{
root = new BinaryNode();
root->left = NULL;
root->right = NULL;
}
//////////////////////////
BinarySearchTree::BinarySearchTree(const BinarySearchTree & rhs){
if(this != &rhs ){
makeEmpty();
root = clone(rhs.root);
}
}
//////////////////////////
const BinarySearchTree & BinarySearchTree
::operator=(const BinarySearchTree &rhs)
{
if(this != &rhs){
makeEmpty();
root = clone(rhs.root);
}
return *this;
}
//////////////////////////
BinarySearchTree::~BinarySearchTree(){
makeEmpty();
}
//////////////////////////
const int & BinarySearchTree::findMin() const {
return findMin(root)->element;
}
//////////////////////////
const int & BinarySearchTree::findMax() const {
return findMax(root)->element;
}
//////////////////////////
bool BinarySearchTree::contains(const int & x) const {
return contains(x,root);
}
//////////////////////////
bool BinarySearchTree::isEmpty() const {
return (root == NULL);
}
//////////////////////////
void BinarySearchTree::printTree() const {
if(root==NULL)
return;
if(root->left!=NULL){
cout<<root->element;
printTree(root->left);
}
if(root->right!=NULL){
cout<<root->element;
printTree(root->right);
}
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
void BinarySearchTree::insert(const int & x,BinaryNode * & t) const{
if( t==NULL )
t = new BinaryNode(x,NULL,NULL);
if(x<t->element)
insert(x,t->left);
if(x>t->element)
insert(x,t->right);
}
//////////////////////////
void BinarySearchTree::remove(const int & x,BinaryNode * & t)const{
if(t==NULL)
return;
else if(x<t->element)
remove(x,t->left);
else if(t->element<x)
remove(x,t->right);
//Author used a beautiful algorithm !I can't do that!
//This tell me don't think "while" and "for" only!
//When you using recursion,you will find that "go
//to the next step" is more usefull. Weiss called
//this "making progress".
else if(t->left!=NULL && t->right!=NULL){
t->element=findMin(t->right)->element;
remove(t->element,t->right);
}
else{
BinaryNode * oldNode = t;
t=(t->left!=NULL)?t->left:t->right;
delete oldNode;
}
}
//////////////////////////
BinaryNode * BinarySearchTree::findMin(BinaryNode *t){
if(t!=NULL)
while(t->right != NULL)
t=t->left;
return t;
}
//////////////////////////
BinaryNode * BinarySearchTree::findMax(BinaryNode *t){
if(t!=NULL)
while(t->right != NULL)
t=t->right;
return t;
}
//////////////////////////
bool BinarySearchTree::contains(const int & x, BinaryNode *t)const{
if(t==NULL)
return false;
//Weiss put the following two sentences to the end of this
//codeblock with an "else".But I think put it here can make
//the code more readable.
if(x == t->element)
return true;
//When I implemented these, I haven't added
//"return" before recursive functions.
//Why must add "return" in the following sentences?
//How many times will the "return" really
//return when the promgram run?
//What will be happened,if I delete these "return"?
if(x < t->element)
return contains(x,t->left);
else if(t->element < x )
return contains(x,t->right);
}
//This method used the postorder traversal
//to make the tree empty.Postorder traversal
//means the order: left->right->root.
//////////////////////////
void BinarySearchTree::makeEmpty(BinaryNode * & t){
if(t!=NULL){
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
//////////////////////////
BinaryNode * BinarySearchTree::clone(BinaryNode *t) const{
if(t==NULL)
return NULL;
//It's great beautiful here!
return new BinaryNode (t->element,clon(t->left),clon(t->rihght) );
}
// Data Structure: Binary Search Tree
// This file is named BinarySearchTree.h
#ifndef MYT_BSTREE_H
#define MYT_BSTREE_H
class BinarySearchTree
{
public:
BinarySearchTree();
BinarySearchTree(const BinarySearchTree & rhs);
~BinarySearchTree();
const int & findMin() const;
const int & findMax() const;
bool contains(const int & x) const;
bool isEmpty() const;
void printTree() const;
void makeEmpty();
void insert(const int & x);
void remove(const int & x);
const BinarySearchTree & operator=(const BinarySearchTree & rhs);
class BinaryNode
{
int element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(){}
BinaryNode(const int & theElement,BinaryNode *lt=0,
BinaryNode *rt=0)
: element(theElement),left(lt),right(rt){ }
friend class BinarySearchTree;
};
protected:
BinaryNode *root;
void insert(const int & x,BinaryNode * & t) const;
void remove(const int & x,BinaryNode * & t) const;
BinaryNode * findMin(BinaryNode *t) const;
BinaryNode * findMax(BinaryNode *t) const;
bool contains(const int & x,BinaryNode *t) const;
void makeEmpty(BinaryNode * & t);
void printTree(BinaryNode *t) const;
BinaryNode * clone(BinaryNode *t) const;
};
#endif
#include <iostream>
#include "BinarySearchTree.h"
using namespace std;
int main(void){
BinarySearchTree mytest;
mytest.insert(9);
mytest.insert(3);
mytest.insert(45);
mytest.insert(7);
mytest.printTree();
}
>What's the differences between struct and class(all member is public)?
Default access to a class is private and default access to a struct is public.
>And he say that the "BinaryNode sturct is too 'C'
It sounds like he doesn't know what he's talking about. Rather than follow bad advice, first learn how trees work and then look at the different ways you can implement them.
>2.How to make this program runs?
My recommendation is to stop using classes. Trying to contain a tree structure into a class just complicates everything by adding an extra layer of fat. It's easier to learn how to use trees with just a structure and a few functions and then add a class framework later.
>What's the differences between struct and class(all member is public)?
Default access to a class is private and default access to a struct is public.>And he say that the "BinaryNode sturct is too 'C'
It sounds like he doesn't know what he's talking about. Rather than follow bad advice, first learn how trees work and then look at the different ways you can implement them.>2.How to make this program runs?
My recommendation is to stop using classes. Trying to contain a tree structure into a class just complicates everything by adding an extra layer of fat. It's easier to learn how to use trees with just a structure and a few functions and then add a class framework later.
I believe that it is a good recommendation to stop using classes.
But I want to prictice OOP with C++ as well as implement the tree.
I want to write ALV tree whic will inherit form the BinarySearchTree.
I kown it's a foolish design to do so,but I haven't write a program with herit ever. So I want to try to do it.
Ah,do you know that C and C++ is looked as the water and fire in China! If someone use C style code in *.cpp,he will be considered as a laypeople.
Could you tell me why the code can't pass the compiler?
Thanks.
You're making everything harder by trying to do too much, too soon. I don't care if you try, but I'm not going to help you run yourself into a wall.
>Ah,do you know that C and C++ is looked as the water and fire in China!
>If someone use C style code in *.cpp,he will be considered as a laypeople.
Not just China. Clueless people all over the world seem to think that C-style code in C++ is somehow less "pure". They're wrong, of course, and you shouldn't listen to them.
>Could you tell me why the code can't pass the compiler?
Do a Google search for PEBKAC and you'll discover the problem.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.