For the following problem:
The output/correct result is specified as:
Output: [3,9,20,null,null,15,7]
I'm not sure what that output actually represents. I tried to scan it by level . E.g 3 is the root, then its children are 9 and 20 (which does not work). So then what is the actual tree?
It's how the binary tree is represented.
Output is a list of nodes where for node i (starting from index 0), node 2*i+1 is its left child and node 2*i+2 is its right child. So if those nodes do not exist, the corresponding value in the list is represented as NULL.
In this case, node 0 has a value of 3, and its left child is shown in node 1 (Output[1]) with value 9, while its right child is shown in node 2 (Output[2]) with value 20.
However, node 2 (Output[2] with value 20) does not have any children so the values corresponding to its children (Output[3], Output[4]) are shown as Null.
I wrote the following XPath and expected the previous td to be selected. However, it skips that one and selects the previous one + 1.
//td[#class='event_cell event_description']/preceding-sibling::td
preceding-sibling::td selects all of the preceding td sibling elements.
To select just the nearest one, use:
//td[#class='event_cell event_description']/preceding-sibling::td[1]
The numerical index here counts from nearest to farthest. So the nearest is [1], second-nearest is [2], and so on.
My prof gave me an exercise to do with prolog. Given this goal i have to build the corrispondent 234 tree:
write(tree(1,tree(11,tree(111,n,n),tree(112,tree(1121,n,n),n)),tree(12,tree(121,n,n),tree(122,n,n),
tree(123,n,n),tree(124,tree(1241,n,n),tree(1242,n,n)))))
The result should be something like this:
Are you asking what is my problem ?
I studied what is a 234 tree, but i dont understand why the tree i represented can be considered a 234 tree, what i see are numbers that range from 1 to 1242. Should a 234 tree be something like this ?
Here's your given term, pretty printed for clarity:
tree(1, % has 2 child nodes
tree(11, % has 2 child nodes
tree(111,n,n), % a leaf
tree(112, % has 2 child nodes
tree(1121,n,n), % a leaf
n)), % empty
tree(12, % has 4 child nodes
tree(121,n,n), % a leaf
tree(122,n,n), % a leaf
tree(123,n,n), % a leaf
tree(124, % has two child nodes
tree(1241,n,n), % a leaf
tree(1242,n,n)))) % a leaf
It is clear that the "numbers" 1, 11, 12, ..., 1242 aren't used for their numeric value, but just as stand-ins. In other words, the values are unimportant. A valid tree has already been built.
This tree's nodes each have either 2 or 4 child nodes (possibly empty, signified by n). That is why it is considered to be a 2-3-4 tree, where each node is allowed to have 2, 3, or 4 child nodes (possibly empty).
Your question then becomes, given a 2-3-4 tree represented by a Prolog compound term like the one above, print the tree in the nice visual fashion as shown in your attached image.
This is achieved simply by swapping the printing of nested sub-trees with the printing of the node's value:
print_tree( n ).
print_tree( tree(A,B,C) ) :- print_tree(B),
print_node_value(A),
print_tree(C).
print_tree( tree(A,B,C,D) ) :- print_tree(B),
print_node_value(A),
print_tree(C),
print_tree(D).
print_tree( tree(A,B,C,D,E) ) :- print_tree(B),
print_tree(C),
print_node_value(A),
print_tree(D),
print_tree(E).
You will have to augment this by passing in the desired indentation level, and incrementing it, by the same amount, when printing the child nodes.
How can I go about listing the words a TST contains in an alphabetical order?
Unlike BSTs where an in-order traversal will do the trick, this won't work TST. Neither would pre-order nor post-order traversals.
More so, the nodes of TST contain alphabets not words as opposed to the nodes of some BST implementation. And there are some alphabets which not be included when moving from the left nodes to right nodes.
I can seem to get my head around it.
The figure below show the list of words of a TST in alphabetical order.
Image from: http://www.geeksforgeeks.org/ternary-search-tree/
You can think of a ternary search tree as a hierarchy of different binary search trees - the black lines connect together nodes in the same BST, and the dotted lines link different BSTs together.
You can list off all the words in a TST by doing a modified inorder traversal. Specifically, do an inorder traversal, and at each node, recursively list off all of the words in the TST linked to the current node, prefixed by whatever letters are on the path so far.
Here's some pseudocode:
function tst_inorder_traversal(node) {
_tst_inorder_traversal_aux(node, "");
}
function _tst_inorder_traversal_aux(node, prefix) {
if (node is not null) {
/* Start normal in-order traversal.
This prints all words that come alphabetically before the words rooted here.*/
_tst_inorder_traversal_aux(node->left, prefix);
/* List all words starting with the current character. */
if (node marks the end of the word)
print(prefix + tree->character);
_tst_inorder_traversal_aux(node->middle, prefix + node->character);
/* Finish the in-order traversal by listing all words that come after this word.*/
_tst_inorder_traversal_aux(node->right, prefix);
}
}
Hope this helps!
I am trying to solve a problem of finding a max repetitive sub-tree in an object tree.
By the object tree I mean a tree where each leaf and node has a name. Each leaf has a type and a value of that type associated with that leaf. Each node has a set of leaves / nodes in certain order.
Given an object tree that - we know - has a repetitive sub-tree in it.
By repetitive I mean 2 or more sub-trees that are similar in everything (names/types/order of sub-elements) but the values of leaves. No nodes/leaves can be shared between sub-trees.
Problem is to identify these sub-trees of the max height.
I know that the exhaustive search can do the trick. I am rather looking for more efficient approach.
you could implement a dfs traversal generating a hash value for each node. Store these values with the node height in a simple array. Sub-tree candidates are duplicate values, just check that the candidates are ok since two different sub-trees could yield same hash value.
Assuming the leafs and internal nodes are all of type Node and that standard access and traversal functions are available :
procedure dfs_update( node : Node, hashmap : Hashmap )
begin
if is_leaf(node) then
hashstring = concat("LEAF",'|',get_name_str(node),'|',get_type_str(node))
else // node is an internal node
hashstring = concat("NODE",'|',get_name_str(node))
for each child in get_children_sorted(node)
dfs_update(child,hashmap)
hashstring = concat(hashstring,'|',get_hash_string(hashmap,child))
end for
end if
// only a ref to node is added to the hashmap, we could also add
// the node's height, hashstring, whatever could be useful and inapropriate
// to keep in the Node ds
add(hashmap, hash(hashstring),node)
end
The tricky part is after a dfs_update, we have to get the list of collinding nodes in the hasmap by descending height and check two by two they are really repetitive.