views:

1212

answers:

7

Hello everyone, I'm trying to learn about trees by implementing one from scratch. In this case I'd like to do it in C# Java or C++. (without using built in methods)

So each node will store a character and there will be a maximum of 26 nodes per node.

What data structure would I use to contain the pointers to each of the nodes?

Basically I'm trying to implement a radix tree from scratch.

Thanks,

+4  A: 

What data structure would I use to contain the pointers to each of the nodes?

A Node. Each Node should have references to (up to) 26 other Nodes in the Tree. Within the Node you can store them in an array, LinkedList, ArrayList, or just about any other collection you can think of.

Bill the Lizard
+1  A: 

It doesn't really matter. You can use a linked list, an array (but this will have a fixed size), or a List type from the standard library of your language.

Using a List/array will mean doing some index book-keeping to traverse the tree, so it might be easiest to use just keep references to the children in the parent.

Gabe Moothart
+1  A: 

Here's one I found recently that's not a bad API for trees - although I needed graphs it was handy to see how it was set up to separate the data structure for the data it was holding, so you could have a tree-equivalent to Iterator to navigate through the tree, and so on.

https://jsfcompounds.dev.java.net/treeutils/site/apidocs/com/truchsess/util/package-summary.html

JeeBee
A: 

If you are actually more interested in speed than space, and if each node represents exactly one letter (implied by your max of 26) then I'd just use a simple array of 26 slots, each referencing a "Node" (the Node is the object containing your array).

The nice thing about a fixed-sized array is that your look up would be much quicker. If you were looking up char "c" that was already guaranteed to be a lower cased letter, the look up would be as easy as:

nextNode=nodes[c-'a'];

A recursive lookup of a string would be trivial.

Bill K
A: 

Check out this Simeon Pilgrim Blog, the "Code Camp Puzzle Reviewed". One of the solutions uses a Radix in C# and you can download the solution.

daduffer
+1  A: 

What you describe isn't quite a radix tree... in a radix tree, you can have more than one character in a node, and there is no upper bound on the number of child nodes.

What you're describing sounds more limited by the alphabet... each node can be a-z, and can be followed by another letter, a-z, etc. The distinction is critical to the data structure you choose to hold your next-node pointers.

In the tree you describe, the easiest structure to use might be a simple array of pointers... all you need to do is convert the character (e.g. 'A') to its ascii value ('65'), and subtract the starting offset (65) to determine which 'next node' you want. Takes up more space, but very fast insert and traversal.

In a true radix tree, you could have 3, 4, 78, or 0 child nodes, and your 'next node' list will have the overhead of sorting, inserting, and deleting. Much slower.

I can't speak to Java, but if I were implementing a custom radix tree in C#, I'd use one of the built-in .NET collections. Writing your own sorted list isn't really helping you learn the tree concepts, and the built-in optimizations of the .NET collections are tough to beat. Then, your code is simple: Look up your next node; if exists, grab it and go; if not, add it to the next-node collection.

Which collection you use depends on what exactly you're implementing through the tree... every type of tree involves tradeoffs between insertion time, lookup time, etc. The choices you make depend on what is most important to the application, not the tree.

Make sense?

snogfish
+1  A: 

Hello everyone, Thanks for the quick replies.

Yes was snogfish said was correct. Basically, its a tree with 26 nodes (A-Z) + a bool isTerminator.

Each each node has theses values and they are linked to each other.

I have not learned pointers in depth yet so my tries today to implement this from scratch using unsafe code in C# where futile.

Therefore, I'd be grateful if someone could provide me with the code to get started in C# using the internal tree class. Once I can get it started I can port the algorithms to the other languages and just change it to use pointers.

Thanks very much, Michael

edude05