From this post and this codebase, I know that there are pointers for
Youngest child
Youngest sibling
Oldest sibling.
So with Oldest child, how do I get?
I am thinking of access "children" pointer (current->children) and traverse to the end of that doubly linked list.
Get the oldest sibling of the youngest child:
current->p_cptr->p_osptr
Related
I understand the logic of using a queue to visit the nodes in a binary search tree level by level.
However i tried to implemment in C but i'am stuck because i don't know how to enqueue them properly. Starting with the root i can create a Queue but after that if i add the children of the root to the queue i will lose the children of those new nodes since iam modifying the connections in the Queue every time a add a new node.
I could create a new data type that has one more link to use in the linked list Queue, that should work. What is the best approach here?
visit[ing] the nodes in a binary search tree level by level
has a name: it is called a "breadth-first" traversal of the tree. Starting with an empty queue, you enqueue the root node and then repeatedly dequeue the first node in the queue, process it somehow, and enqueue all that node's children, until there are no more nodes enqueued. When exactly you should enqueue a node's children relative to other processing of that node may depend on exactly what processing you intend to perform, especially if it involves structurally modifying the tree.
As long as the per-node processing can affect only the subtree rooted at the then-current node, this is all fine. If you need to be able to affect other parts of the overall tree, however, then a breadth-first traversal probably is not appropriate for your task.
You said
[I] don't know how to enqueue them properly. Starting with the root [I] can create a Queue but after that if [I] add the children of the root to the queue [I] will lose the children of those new nodes since [I] am modifying the connections in the Queue every time a add a new node.
The key concept here is that membership and position in the queue are separate and independent from membership and position in the tree. You could manage that by adding additional links to the node structures themselves, or by creating a new structure for the queue elements that contains a pointer to the enqueued BST node. The latter decouples the tree from the queue, which many, including me, would consider preferable for most purposes.
I have some code where the user can delete one child record of a parent record one at a time. I'm detecting when there are no children left. At that point, I'm deleting the parent record as well. When coming up with a name for the variable, I realized I don't know what you call a parent with no children.
Is there a single, accepted name that I haven't heard of (or can't remember)?
Leaf: (no children)
Terminal: (never children)
Vacant: (had children)
Orphan: (no parent)
Leaf seems pretty standard according to wikipedia. "Terminal Node" on wikipedia will re-direct you to leaf node.
Vacant node in more detail:
Your orphanage had a child. The child got adopted and left. Your orpahage is now vacant.
Terminal node in more detail:
Example in OpenGL:
An EBO references a VAO.
A VAO references a VBO.
A VBO is raw data and references nothing else.
( EBO --> VAO --> VBO )
Thinking of the VBO as a "terminal node" would make more sense than thinking of it
as a leaf. Because it will not and cannot have children.
There are two common computer science notions of parent: relative/transitive and absolute/non-transitive. One node can be the parent of another (a child). (Hence, object of a transitive "is" or "parents"). A node with no children is a leaf node; a node with children is a parent node. (Hence, object of a non-transitive "is"). Neither of these involve the non-instantaneous notion of ever once having had children, eg as in the real world.
Each node of the type you are describing is during processing (before and after pruning) both a parent and not a parent. So you are using something like the non-instantaneous/real-world third meaning, something like "was a parent but isn't any more". (Think about what you're saying: it's a parent in the sense that it used to be a parent node even though it's not a parent node. So it's a parent ...because... it's not a parent.)
Once it's going to be deleted, how about NoLongerParent? But if you're just using a name for that node for all the processing, and it starts as a non-leaf, why not just Parent?
My question is : how do you read the version of a (child) frozen node?
More details :
- Lets imagine the following situation : node A has two children node B and node C.
- All three nodes are versionable nodes (have the mixin type)
- Let's say node A has 7 versions from 1.0 to 1.6 : at version 1.2 we added node B, at version 1.4 we added node C.
I have a routine that prints the "output" of node A at version 1.5.
//I already obtained the Version object version associated to node A version 1.5
NodeIterator nodeIterator = version.getNodes();
while (nodeIterator.hasNext()) {
Node currentNode = nodeIterator.nextNode();
System.out.println("Node: "+currentNode.getPath());
// I can see that some of the nodes correspond to children (versions) of
//node B and C ..how can I get their version?
}
Thank you.
Each version in the history of the parent will contain a "frozen node" that represents a snapshot of the parent as it existed when the version was created (e.g., when the parent node was checked in). This frozen node contains child nodes that are either snapshots of the child (if the child was not versionable) or a "nt:versionedChild" node (if the child was versionable). This "nt:versionedChild" node contains a single "jcr:childVersionHistory" REFERENCE property that points to the version history of the child.
Section 3.13.10 of the JSR-283 specification contains a pretty good diagram that shows this structure.
Note that the "nt:versionedChild" node does not point to the particular version in the child's version history, which means that you have to determine which version of the child existed at the time the parent was checked in. After all, in this case of a versionable child, the parent and child version histories are independent and the nodes are checked in independently. (The reason might also stem from the fact that the application can change both the parent and child nodes in a single session, and then either check the parent in followed by checking the child in, or checking the child in followed by checking the parent in.)
One way to do this is by looking at the creation date of each version in the child's version history. This isn't ideal, as it will only tell you which child version existed at the time the parent was created. This could be ambiguous, since the comparison of timestamps depends on whether the parent was checked in before or after the versioned child. Within your application, you might have conventions that help you get around these ambiguities.
Perhaps a better option is to use version labels and apply the same label to the parent version and child version. (This is probably easier to do just after checking in the nodes rather than at a later time.) Then once you have a particular version of the parent, obtain its label and use it to find the corresponding version of the child in the child's version history (using VersionHistory.getVersionByLabel(String)).
I have a tree, each node contains an array children nodes (Node *children_nodes), as well as the name (char *node_name) and parent (Node *parent). each of these are dynamic.
I want to delete a child node from the *children_nodes array, freeing the memory allocated to it's name and children, (let's pretend we are deleting a child with no children), and move the location of the last child of the list to the location of the one we just deleted. how can I do this without making the last node get changed if I want to use the location it was in.
Example- I have a node with three children, I want to free children_nodes[0]'s allocated memory and put children_nodes[2] in that spot, preferably just making children_nodes[0] point to the node of children_nodes[2] and then making children_nodes[2] point to nothing without messing with the node itself.
It's difficult to tell for sure without seeing some code, but I believe you want a Node** children_nodes, so you can just do something like this:
free(children_nodes[0]);
children_nodes[0] = children_nodes[2];
children_nodes[2] = 0;
Question: how can we display tree nodes level by level ?. could you please give me time and space efficient solution .
Example :
A
/ \
B C
/ \ / \
D E F G
void PrintTree(struct tree *root);
Output:
You have to print tree nodes level by level
A
B C
D E F G
If you're feeling brutish, and want to think very simply about the level you are at...
You will need:
Two queues
A slight twist on Jack's approach
So, start with root.
Tack its children onto the first queue.
Step through them, tacking their children onto the second queue as you go.
Switch to the second queue, step through, pushing their children onto the first queue.
Wax on, wax off.
Really it's just a slight expansion of the same idea, the breadth first search or sweep, which is worth thinking about as a pattern, since it applies to a variety of data structures. Almost anything that's a tree or trie, and a few things that aren't, in fact!
To save space and time on SO:
http://thecodecracker.com/c-programming/bfs-and-dfs/
This kind of visit is called Breadth-first or Level Order. You can see additional infos here.
Basically you
first visit the current node
then all the children of that node
then all the children of every children and so on
This should be achieved easily with a FIFO structure:
push the root
until queue is empty
take first element, visit it, and push all its children to the end of the queue
repeat