I wrote a tree in which each node has a list of his children. So my questions are : How can I compute the number of levels of my tree? Can anyone give me some documentation about this ?. Thank you :).
There could be different ways to solve this issue. One solution could be like count the nodes using a counter variable and increment the counter until the leaf node reaches. But you have to take care of
1. Longest Chain
2. Redundancy in couting
Second if each node has a list of children then count the nodes through this list present in root node.
A very appropriate way would be to define a variable by name Level into the struct node
typedef struct node {
...
Other members
...
int node_level;
} NODE;
and initialize it with 1 when root or any other node is created. Then update its value on each insertion into the tree.
By doing so you can see the level of any child tree, whenever you need to find. Also note each inserted node would have a level 1 and its ancestors will have greater level.
Related
I have many subtrees saved in a file and I want to search them to find many things for each one of these subtrees like : the number of nodes, the number of leafs and the number of levels a subtree consist of...
To be more precise, the difference between a node and a leaf in my work; a node is any vertex in a subtree that could be a parent or a child where as a leaf is only a child vertex i.e every leaf is a node and the opposite is not true.
I am facing many problems in this work, the first one: the file containing the subtrees is not showing the rooted node and is not differentiating between parents and children..
The second problem: I read that for searching a tree programmers usually use a recursive method so I tried to search through the INTERNET for references or algorithms or pseudo-codes but all what I found is dealing with binary tree which is not my case (I am dealing with all configurations of a subtree) !!!
So could anyone kindly help me by giving a reference, an algorithm or an example for searching a tree to find the previous characteristics for such a subtree??
Another question: Is it possible to do this work using R ??
I will use any program to write the code but mainly I am interested in C.
Again,, please my subtree is not a binary one
UPDATE:
Each subtree is represented in my file as a set of edges, You can see below an example of a subtree of size 4:
44180 0
44180 18238
44180 13362
69677 44180
UPDATE: Sorry for the new update but Can I use R in my case even if there is a huge number of subtrees like 100000 subtrees each one with 20 edges (100000*20) ??
A Tree is well defined.
I am going to assume that each file describes exactly one tree. It also seems reasonable to assume that the line 44180 0 means an edge from node 44180 to node 0. You should double check these assumptions.
With these assumptions, you can parse the file into the following data structure:
Node
int id (id of self)
Node* parent (pointer to parent Node)
Node** children (list of children Nodes)
Or even simpler:
Node
int id
int parent (id of the parent node)
int* children (ids of all children)
Once you have the entire file parsed into a list/array of Nodes, then pick ANY node, and recursively traverse up the parent node until you hit a node that has no parent. Then the final node must be the root of the tree.
Now that you have the pointer to the root Node, you should be able to apply other algorithms.
When I am adding a value to an AVL tree, how do I know where to insert it? I'm not talking about the coding aspect, but more of a pictorial representation. I'm assuming the insertion is done at the first available external node, but I'm unsure, as the example given in my textbook inserts at a random external node.
AVL tree is a self-balancing binary search tree and binary search trees have following property: the left subtree of every node contains only nodes with keys less than the node's key and the right subtree of every node contains only nodes with keys greater than the node's key. So you have to insert new node in such way that stated property remains after insertion: you start with root node and working towards leaf by comparing new node key and current node key if the new node key is greater you visit right child else you visit left child next and when there is no child to visit in that place you insert new node.
As mentioned in the Title , I have a Binary search tree. I want to convert it to sorted doubly linklist using recursion.
My code
for each node in tree
find max of left sub-tree and assign its right to present node ,present node left to max
find min of right sub-tree and assign its left to present node ,present node right to max
and now recursively do same thing to other nodes in BST .
but this solution is not efficient as it reaches each node more than one time .In my quest of optimized code i got a link from google greatTreeList sollution . I have searched the same in SO both sollutions are same and worked for me. I didnt understand the append function of the sollution as it contains code
join(alast,b)
join(blast,a)
For tree whose nodes are inserted in following order 10,5,9,6,8,7,12,11,13,14
can anyone please explain how
join(alast,b)
join(blast,a)
are linking node in each recursion call.
I think you are over thinking this actually quite easy task - extracting the data from a binary tree in order is as simple as doing a depth first traversal - this is the point of a binary tree that it very efficiently gives you the elements in the sorted order.
So what you need to do is a standard depth first walk of the tree and each time you find a node add it to you linked list.
This in order depth first recursion is fairly straight forward is pseudocode:
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
I suggest you try this manually on you example so you see how it works.
In order to convert a binary search tree to a sorted doubly linked list, one typically performs an inorder depth-first traversal, building the list along the traversal.
Try to come up with code that performs an inorder depth-first traversal and prints the items of the binary tree in sorted order. From there, it should be easy to complete your task.
Expanding on Elemental's answer
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
To add N to the linked list,
your linked list class or data structure should have an append() method or similar.
Use your imagination, but something like this:
def append(N):
new_tail = node(val=N, prev=self.tail, next=None)
self.tail.next = new_tail
self.tail = new_tail
of course you also need to add self.head = self.tail the first time you append.
Guys I am new to data structures.Most of the time in books and references i see this structure for a binary tree
struct btree {
int data;
struct btree *left;
struct btree *right;
};
But in above image it would be like
struct btree
{
int data;
struct btree *left;
struct btree *right;
struct btree *parent;
};
So my question is that is it dependent on programmer to choose the structure of a node of a tree (for e.g also including a pointer to the parent )or we can have only two pointers one to the left child and other to the right child.
It is up to you whether you include parent pointers. They require more work to maintain, but some tree operations (like removing a node given just that node rather than its parent) become much easier.
It's up to you. They're generally not required for most tree operations, but they can speed up some of them, at the expense of the memory required to store the extra link. On the few occasions I've had to write a binary tree myself I've never used a parent link.
Absent something specifically saying otherwise, a binary tree doesn't normally contain pointers to parents as shown in that diagram. The "pointer to parent" is normally stored implicitly on the stack as you traverse the tree recursively.
There is a rather more common variant -- the "threaded binary tree" -- in this case, the leaf nodes where a normal binary tree would have NULL pointers instead have pointers to the next node in order and the previous node in order. This lets you walk through the tree in forward or reverse order without recursion.
The model without the parent node is better suited for recursive divide-and-conquer-style algorithms [for example, an pre-order or post-order traversal]. For more advanced operations, the parent pointer is needed.
What are you trying to do?
I am creating a student list (linked list) that can add, view and edit student information. I have two fields namely Student Name and Student Grade and I add new students in the list in a way that it is sorted according to the student's grades in descending order.
I have finished doing the add and view portion. The problem is on the edit part because I need to edit the information, then I need to sort it again so that it would be on the proper location of the list.
For example, I have 3 students information arranged according to their grades:
student1 90 -> student2 85 -> student3 80 -> NULL
Then I need to edit student2's grade to 75 so the edited linked list should now be arranged as follows:
student1 90 -> student3 80 -> student2 75 -> NULL
How should I do that? You don't need to give me any code. I just want some advices on how I can implement the edit part of my program. I am thinking of creating a new node (with the edited info), delete the old node and insert the edited node into the list. Is my logic correct? or is there a better way on solving my problem.
You can accomplish your goal by
Removing target node
Editing target node data
Reinsert the node using your existing logic for inserting nodes.
Singly linked list?
Find the node you want to edit, and either keep a pointer to the previous node or write a routine to retrieve the previous node.
Remove your node from the linked list (by setting previous_node->next to thisOne->next)
Make your edits.
Insert the new node in the right place in the list (by traversing the list unti the next node is less than your edited value.
Splice edited into the list ( editedNode->next = nextNode; current->next = editedNode)
With doubly linked lists you can just use the "other"/back/up link to find the previous node
Basically your idea is correct except that I wouldn't create a new node. What I would do would be:
Identify if value has increased or decreased (and update node).
Unlink node from the list.
Search forwards or backwards (depending on increase or decrease of grade) for correct location.
Link node into new location.
Note that indexing the list into an array, etc. may give a quicker search than a linear traversal. If you already have such a mechanism it may be quicker to use that when finding the location to re-insert the node.
You could do a function that edits a specified node. Scan the list until you find that node and then directly edit it. Of course you will use a pointer. For the sorting part, say you have n nodes, compare every node i with the nodes after it and swap them if the one you are comparing with is larger:
for every node n1 in list
for every remaining node n2 in list
if n2->grade > n1->grade
swap 'em
you can swap them copying their memory, so you won't need to change any pointer.