I am trying to create the Huffman Tree in c++. I have gotten as far as creatig the priority queue with the individual characters (read from a txt file) and now I am stuck at creating the actual tree. First of all, I know what needs to be done. I have to dequeue the first two items from the queue and then use those to make a tree and then enqueue the tree back into the queue. But how am I supposed to implement that?
Related
I just have a question about a singly linked list in C++ programming. I am trying to display the middle data item of a singly linked list of integer values with ONLY one traversal. Any hints will be very helpful !
I was able to solve this problem with more than one traversal, but I MUST only traverse the linked list once in order to display the middle data item.
Thanks !
Since there is no restriction on memory, Copy all the elements of the list into an array. Finally go to the middle of the array and get the data.
I am working on a classification problem , for which I train a decision tree and for each node in the tree I store the clusters, centroids and a bunch of other stuff to identify the node in the tree.At the end of it I have a pointer which points to all this data. I wanted to know a way to write this to a file, so i can resuse it later. Also, what is the best format to write the data.
I have a question concerning Abstract Syntax Trees generated with Boost Spirit Library.
I've found many informations about deleting nodes and subtrees in a Binary Search Tree, but I cannot find the same information for ASTs. I have a node in an AS-Tree, and this node is root to a subtree of a complete tree. Now I want to delete the node and all of its children.
I don't know how to do it, and the Boost Spirit Documentation didn't help either.
Has anyone got any tips for me?
The Tree is generated with (Boost 1.46.1):
tree_parse_info<> info = ast_parse(expression.c_str(), parser, skipparser);
And the Expression is something like this:
(variable_17 OR variable_18) AND function( variable_17) <= 30 OR function( subkey_18) <= 30
I use
tree_match<iterator_t>::tree_iterator tree_it = info.trees.begin();
to get the beginning of the tree, and then I do check if one of the subtrees is redundant (doensn't have anything to do with the deleting itself). `Then I traverse through the tree using
tree_match<iterator_t>::tree_iterator children_it = tree_it->children.begin()
and calling the same function with its children (recursive). I can't post the complete code,but that's the most important part of it. I thought, that i can traverse to the leafnodes of a redundant subtree, and set them to null, or something like this. And then I go up the tree again, and delete all other children one after another. However, nothing has worked so far.
An example for traversing the tree: The Traversing
It's the answer.
If I can't delete any nodes, does anyone has an idea, how to create a new tree, based on the existing one, skiping the redundant parts of it.
I need help with adding data to a tree. For example if i have 7+8*9-18/(1+2), how am I supposed to add it to a binary tree in a way that i can compute the result using a binary tree. I am beginner in learning tree structures so I am not very familiar with it.
After converting the post fix expression to infix, follow the below steps to construct a tree.
If it is number, add it to add to the stack.
If is is an operator, make the operator as parent node,
pop the element and make it as right child to the parent node,
pop the element and make it as left child to the parent node
and add the parent node to the stack.
See How to write an evaluator for a string like "(1+3 * ( 5 / 4)) and get a numeric result and the Shunting Yard Algorithm
I have this data that is hierarchical and so I store it in a tree. I want to provide a search function to it. Do I have to create a binary tree for that? I don't want to have thousands of nodes twice. Isn't there a kind of tree that allows me to both store the data in the order given and also provide me the binary tree like efficient searching setup, with little overhead?
Any other data structure suggestion will also be appreciated.
Thanks.
EDIT:
Some details: The tree is a very simple "hand made" tree that can be considered very very basic. The thing is, there are thousands of names and other text that will be entered as data that I want to search but I don't want to traverse the nodes in a traditional way and need a fast search like binary search.
Also, importantly, the user must be able to see the structure he has entered and NOT the sorted one. So I cant keep it sorted to support the search. That is why I said I don't want to have thousands of nodes twice.
If you don't want to change your trees hierarchy use a map to store pointers to vertexes: std::map<SearchKeyType,Vertex*> M.
Every time when you will add vertex to your tree you need to add it to your map too. It's very easy: M[key]=&vertex. To find an element use M.find(key);, or M[key]; if you are sure that key exists.
If your tree has duplicate keys, then you should use a multimap.
Edit: If your key's size is too big, than you can use pointer to key instead of key:
inline bool comparisonFunction(SearchKeyType * arg1,SearchKeyType * arg2);
std::map<SearchKeyType *, Vertex *, comparisonFunction> M;
inline bool comparisonFunction(SearchKeyType * arg1,SearchKeyType * arg2)
{
return (*arg1)<(*arg2);
}
to search Element with value V you must write following:
Vertex * v = M[&V]; // assuming that element V exists in M