Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / a pointer problem and something else unrelated

Author
Message
Kryogenik
14
Years of Service
User Offline
Joined: 22nd Sep 2009
Location: Heidelberg, Germany
Posted: 3rd Jul 2010 16:20
I made my first binary tree a second ago. You can put values in, search for them, and destroy and create them froma class btree. There's also a struct called node that represents each part of the tree, it has the value, and the pointers that point the the left and right nodes, here's the code.

I tested it with the main part by putting in the first value, 15, and the left and right values, 13 and 17 respectively. The cout part prints the key value in the node that the search points to. However, if it can't find the value, it returns NULL. Which it should, but how would you test the existence of the node before cout without having to perfrom the function twice? In other words, what kind of variable do I need to put the result of the function in? I used coutt<<tree.search(12)->key_value, but since it returns NULL, it crashes the program. I tried making a pointer with int *ptr, and putting the result of the function in it, but it says, cannot convert from node to int. For giggles, I defined it with node instead of int, and I got more errors, but I didn't expect that to work anyway. So what should I do? Also, anytime I type '~', or '"', or '`' or even ''', I have to hit another key before it appears on the screen. I live in Germany, and when I started my computer for the first time, it asked me about a language zone or something. I thought it meant time zone, so I put Germany, but then I realize it had to do with stuff like the keyboard format and the language the date shows up on my computer. I can change it back to english, but anytime I minimize something or change windows, it switches back to German, making these weird keyboard things happen Anyone ever had this problem. Thanks in advance for both questions

cout<<"I'm learning C++, and this is all I know \n"
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 3rd Jul 2010 17:07 Edited at: 3rd Jul 2010 17:09
Have you tried?



This will perform a single search and return the correct node or NULL as you've specified...

Hope this helps,

JTK

EDIT: Unless this is an exercise for you to learn and understand these binary-trees, I'd suggest that you use the standard-template-library versions. std::map<>, std::set<> etc...
Kryogenik
14
Years of Service
User Offline
Joined: 22nd Sep 2009
Location: Heidelberg, Germany
Posted: 3rd Jul 2010 19:08
That works, thanks JTK But I don't know the map<> and set<> commands in your edit, I'm still a beginner. But yeah, I made the binary tree based off a tutorial. And I just fixed the keyboard thing, I had to remove the German stuff from my language options.

cout<<"I'm learning C++, and this is all I know \n"

Login to post a reply

Server time is: 2024-07-04 11:05:53
Your offset time is: 2024-07-04 11:05:53