Linked Lists, Binary Trees

Started by
8 comments, last by small_duck 18 years, 6 months ago
Hello, me again! A few weeks ago I finished a game which was a hefty project of mine. Here's some background information that pertains to my question: Its a simple game, where there are two teams, 13 guys to a team, and 3 different types of guys, big, medium, and small. I'm very creative with that. What is somewhat strange, I suppose, is that the "big, medium, and small" guys are all based off the exact same class; there isn't even inheritance or anything, just a class, and depending on what number they are in a team, they get a size. Oh, and there is considerable amount of collision-testing in the game, so they have data compared to one another and to various obstacles on the "map". As far as how all the units are declared, there is a global array declared near the beginning, which is: "unit army[2][13]". 2 teams, 13 guys to a team. That concludes the excessive background information. Essentially, what I'm trying to do is learn about linked lists and binary trees. In a backup copy of the game, I've successfully replaced the declaration of an array with the class "list", and created a linked list. However, the linked list has as many nodes as there are units; there is no organization, so nothing is really gained from that except a lot more pointers. So, I went about trying to learn about binary trees. I set up the classes, pointers, and functions necessary for a binary tree, but I'm still not quite sure as to how to execute it. I've looked for tutorials and such, and I have an amazing book I borrowed indefinitely :) from a friend, but I don't quite understand how to organize a binary tree, and I'm at a loss as to what kind of organization is expected (by size, team, or what?), and I'm once again at a loss as to how to use this new-found organization to save time, typing, or what-have you. Any help in any of those matters would be much appreciated. :) Oh, and its in C++ and OpenGL, if that matters. Thank you! - Gaj.
Advertisement
Well, why do you need a binary tree to organize your units or whatever? Just as a guess, I don't think the data structure will help you much, and may make things more difficult for you. I mean, if you need to manage all the units on a team, I suggest you make some kind of team manager class that stores all the members of a team in a vector, and manages rendering, AI, or whatever, internally. I vector allows random access fairly efficiently, which is probably going to be more important than the speed of adding more objects to a linked list.

The thing is that you want to use the simplest, easiest way to do anything, as long as it will meet your needs. The binary tree structure is not going to be a good choice for this problem. You're making things too complicated =)

So before you go trying to implement some complex data structure, make sure that it will meet your needs - and know exactly how it will.

By the way, as long as the different sizes of enemies don't act too differently, then it is fine to use a single class for all of them, keeping track of what is what with an integer or something. In half-life, this wouldn't cut it. However, for simple games, this is what I would do.

Good luck with your game.
my siteGenius is 1% inspiration and 99% perspiration
I would have to agree with silverphyre673. A binary tree is more useful when looking for ordered data...that doesn't seem to be an issue here.

You do seem to have issues with inheritence though...do all members have the same AI?..They all have to be drawn..so a generic draw function might be good. I suppose what I'm saying is what of you'd like to extend your game in the future. Right now all players have the same AI funtion...but based on a number you have different stuff happening to the individual player. The interaction may not differ wildly now, but what about in the future. What if you want one player fall over and die if something happens BUT another type hit back, get the "ball" and do a special move if the same thing happens. What if you get to a stage where you want to download new player kinds or just add them without rewriting loads of apis...inheitence is our friend.

Just some thoughts on this..hope you get it all sorted out soon.
Gary.Goodbye, and thanks for all the fish.
If two classes have the same "behavior" and the only difference is the values of the data, then they should be the same class. So, rather than have Big, Medium, and Small classes, you should have a single class with a member variable whose value is Big, Medium, or Small -- as you have done.

As far as your data structures, you are going about it backwards. Rather than taking a data structure and trying to fit it into your design, you must look at your design and decide what data structure it needs. Arrays are useful when you want to be able to quickly access any element. Lists are useful when you want to add and remove elements from the middle. Trees are useful for implementing dynamic hierarchies (among other things). So, which fits your design the best?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
How about an organisation like this:
#include <boost/shared_ptr.hpp>struct Army {  std::string name; //Name of the Army  std::list< boost::shared_ptr<Unit> > units;};std::list< boost::shared_ptr<Army> > armies;

You can use boost::shared_ptr to handle deletion of units and armies automagically and avoid unnecessary copying of Units.
Army contains the name of the army to show that you can place extra data in there.
Of course you should do some data encapsulating in your game.
Thanks for all the replies! But, flowingooze, I don't even know where to begin to understand what you wrote. :( What's a .hpp file? It looks like you are using commands I never saw before; where are they from? But that's also a lot less than what the book used as an example, so it looks cool - what is it? :)

As for the other replies:
Well, I know that binary trees are usually for lots of information (I heard google uses something like them, or something...), but this is for educational purposes, if you will, so even if it does become less efficient, I still have the old copy. :)

And there currently is no AI. 0_o. Me and my friends get to test it. I usually win ;)

I'm just kinda looking for suggested readings, and suggested methods of organization just for the hell of it, namely because there are 2 teams, which I can see as an orgnizational thingy, but 3 sizes; trinary tree? :) I found that I learn best when, rather than copying something out of a book and see if it works, make my own version of it. Usually that just involves changing the variable names, but still... >.>

But, all of this inheritance and useful-implementation stuff is also very useful to know, but first I would like to learn how to use binary trees. :)

As a side note: I don't really think this game will be a major project (e.g., everything in the game is a circle. 0_o), but I find it is very useful to learn about new things with. :)

(Editing because I left out a few things)
Quote:Original post by Gaj
Thanks for all the replies! But, flowingooze, I don't even know where to begin to understand what you wrote. :( What's a .hpp file? It looks like you are using commands I never saw before; where are they from? But that's also a lot less than what the book used as an example, so it looks cool - what is it? :)

Since you're still very much a beginner, I won't scare you with too much details. Boost is a collection of helpful libraries for C++.
.hpp extension is often used to indicate that the file is a C++ header, as opposed to a C header. The compiler doesn't care what extensions are used, or even if you don't use any at all (as the standard library does).
boost::shared_ptr is a smart pointer class that counts references to objects. The boost documentation is a bit technical, but shared_ptr is basically really easy to use and by using it, you don't need to worry about deleting objects so much.

Binary trees are usually used as a data structure to make searches quicker.
Here's an animated applet. I'm sure you'll find plenty of information on trees using Google. If you wish to use binary search trees in C++, you should probably use std::set as it's (usually) implemented as a balanced binary tree. (See red-black tree)
Here is a site that has descriptions of STL standard containers and how to use them. You should definitely learn how to use std::vector, and std::map, at the very least. They are two of the most useful things I have ever used. Not to mention std::string.
my siteGenius is 1% inspiration and 99% perspiration
Quote:Original post by silverphyre673
Here is a site that has descriptions of STL standard containers and how to use them.

That's a pretty good site, but some of the information is outdated. This is a much better reference to STL.
Gaj,

A binary tree is used to store sorted data. This means that if you want to put your team-members into a binary tree, you have to give to each one some parameters to sort him.

Depending on the tree, these parameters might have the right to be equal for two different nodes, or might have to be always different. The main idea behind the binary tree is to be able to access quickly (complexity O(log(n))) any node or collection of nodes from a given parameter.

I suggest you give a look into std::set, std::map, and std::multimap, which are generally implemented as binary trees in the STL. Once you've used them a bit (there is a fair amount of tutorials out there about them), you'll probably have a better idea on what they are used for, and how you might want to implement one.

This topic is closed to new replies.

Advertisement