Awkward situation about Data Structure -Tree
Hey guys, I'm building my Data structures for Lesson's need.
What I met was the insertion problem of tree. It's about a travelsal problem of binary tree, yeah, a typical Travelsal Algorithm is reasonable simple, but I found I dont know how to insert a node in tree, my tree was declared in this way. Code:
typedef struct Btreenode { So that if I got no tree, how can I do a traversal? Just give me a hint, I think I can finish rest of it. Thanks for all your help. |
Pass the root of the subtree as a parameter to your insert function.
void insert(bintree head, bintree node); |
This is a bit off topic, but since all values of “flag” are mutually exclusive, there is no need to use powers of two, and furthermore, you might consider using and enum.
|
... also, use a descriptive name, not something like flag. You definitely should declare an enumerated type, and give the variable a meaningful name.
Also ... remember that "a B-tree has been done to death." Look for an existing library, or C++ class, to do the majority of the work for you. There's no point in writing, and debugging, a "stock part." |
Quote:
|
Quote:
Code:
typedef struct Btreenode { int insert(bintree, btrnode) not gonna work!!! |
It's the same as searching. Start at the top of the tree and go left or right based on whether your new item should go before or after the node you're currently at. When you get to a NULL pointer, that's where you insert the new item.
|
Quote:
Quote:
That is definitely not possible, parameters wont be able to carry that much information. Think about when we use touch and mkdir Either I know the abosulte name of a file, or we just create a file under my current location!!! That's a nature way! So, I dont get what you are trying to tell me, please be a little patient? |
Quote:
Quote:
Quote:
Quote:
1) finding the correct place to insert the data 2) inserting data into a specific place in a tree I could very well write a script in bash that will create a hierarchy of directories with a structure equivalent to a binary tree. I could write code to create (insert) a new directory in the correct place in the hierarchy with just the root directory of the hierarchy and the name of the new directory as parameters. The actual execution of mkdir will only constitute the second part of the process. |
A binary tree seeks to split the data constantly in-half. It is far easier for you to surf the Internet for an existing example of tree algorithms, complete with code-snippet examples, than for you to blunder through figuring it all out on your own. (Trees, especially balanced trees, can become quite complicated indeed, very fast ...)
Even if you are "learning programming," you are not the first to be doing so, and "the right answer(s)" are thoroughly and completely known. Therefore, a good on-line textbook on Data Structures is what you need. (In my college days, that was an entire 15-week and very difficult course.) Yes, you are "learning." But, be efficient about doing so. And, again, if you simply want to use a tree of a particular type, a (say ...) C++ "class" is the way to go. Devise custom classes that descend from the provided ancestors, and, as the old bus-line commercial say, "Leave the Driving to Us." Peek inside the source-code if you are truly interested, to see how they did it. |
Quote:
It's a question of comparing newItem.data with treeNode.data and then deciding which direction to take for the next treeNode. In the case where this is the first node to be inserted into the tree, the new node becomes the address of the root node. Just pass a pointer to the root node pointer so you can store this address there. |
Quote:
But what if my "node->data" was not build for that easy case, data is not in order(just like a directory tree), traversal() and lookup() still works, but Insertion failed anyway. |
Quote:
|
All times are GMT -5. The time now is 06:11 AM. |