LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Awkward situation about Data Structure -Tree (https://www.linuxquestions.org/questions/programming-9/awkward-situation-about-data-structure-tree-4175482763/)

v3ct0r 10-30-2013 08:24 AM

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 {
        /* indicate what the Telemtype is */
        /* flag == 0: char
        * flag == 1: int
        * flag == 2: float
        * flag == 4: double;
        * flag == 8: pointer, point to somewhere else */
        int  flag;
        Telemtype data;
        //int nodenum;
        struct Btreenode *lsubtr;      /* left  subtree */
        struct Btreenode *rsubtr;      /* right subtree */
} *btrnode;

typedef struct Btree {
        btrnode root;
        btrnode last;          //easy for insertion? */
} *bintree;

Looking at this structures, How do I know WHERE TO INSERT when I want to insert a node in my tree, since what user get is just the address of "bintree".
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.

psionl0 10-30-2013 10:27 AM

Pass the root of the subtree as a parameter to your insert function.

void insert(bintree head, bintree node);

mina86 10-30-2013 10:32 AM

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.

sundialsvcs 10-30-2013 02:12 PM

... 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."

psionl0 10-30-2013 08:17 PM

Quote:

Originally Posted by sundialsvcs (Post 5055391)
There's no point in writing, and debugging, a "stock part."

If you are learning programming then there is no superior way than trying to write a "stock part".

v3ct0r 10-31-2013 03:33 AM

Quote:

Originally Posted by mina86 (Post 5055246)
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.

I'm using a flag because I just want to know if Telemtype is a pointer or not, so that we I need to free a node, I will know if I need to free data first. The declaration I use was quite prolix, In fact, more simple way is like this:
Code:

typedef struct Btreenode {
        /* indicate what the Telemtype is */
        /* ispointer == 0: nopointer
       
        * ispointer == 1: pointer, point to somewhere else */
        int  ispointer;
        Telemtype data;
        //int nodenum;
        struct Btreenode *lsubtr;      /* left  subtree */
        struct Btreenode *rsubtr;      /* right subtree */
} *btrnode;

typedef struct Btree {
        btrnode root;
} *bintree;

But hell, it's not what I asked, I mean, if I wanna insert or delete a node, apperantly I dont know where to insert, the prototype

int insert(bintree, btrnode) not gonna work!!!

millgates 10-31-2013 04:03 AM

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.

v3ct0r 10-31-2013 04:41 AM

Quote:

Originally Posted by millgates (Post 5055743)
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.

What does
Quote:

whether your new item should go
mean? Can I say to the node" Hey, you, just go right, then go left, then go right, then you will find a place for you!"
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?

millgates 10-31-2013 05:45 AM

Quote:

Originally Posted by xeechou (Post 5055760)
What does
Quote:

whether your new item should go
mean?

It means that one of the fundamental assumptions about building a binary tree is that there is some kind of comparison defined between the elements of the tree that allows us to determine the particular order of the elements in an ordered set. For example, strings may be ordered alphabetically, which means that "apple" goes before "banana", but "orange" goes after it.

Quote:

Originally Posted by xeechou (Post 5055760)
Can I say to the node" Hey, you, just go right, then go left, then go right, then you will find a place for you!"

Actually, yes you can! That's what programming is all about.

Quote:

Originally Posted by xeechou (Post 5055760)
That is definitely not possible, parameters wont be able to carry that much information.

You don't need to carry that information into the function. It's the job of the function to take care of that. All you need to tell the function is the begining (root) of the tree and the data to insert.

Quote:

Originally Posted by xeechou (Post 5055760)
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!!!

Well, the point is, the process of inserting data into a binary tree consists of two parts:
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.

sundialsvcs 10-31-2013 08:34 AM

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.

psionl0 10-31-2013 09:59 AM

Quote:

Originally Posted by xeechou (Post 5055164)
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.

It's no different to what you would have considered when you did singly-linked lists. (You did start with that didn't you?)

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.

v3ct0r 11-01-2013 10:17 PM

Quote:

Originally Posted by sundialsvcs (Post 5055879)
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.

Yeah, typical Insertion method is to compare "node->data", and decide which way to go, it's certainly easy to do so, and much like a look up algorithm. In addition, I did surf Internet for a exist algorithm and I know what they did.

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.

psionl0 11-01-2013 11:27 PM

Quote:

Originally Posted by xeechou (Post 5056892)
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.

No matter what sort of data you are dealing with, it can always be ranked - even if you have the make up the comparison rules yourself (ie write your own compare() function yourself).


All times are GMT -5. The time now is 06:11 AM.