How to think of the summary algorithm
Stay in a company for a long time, do not go out to see, you never know your level, how much you value. That is, as a technician, should be every 4, 5 months, go out to participate in several interviews, to see if their technical level and it circle disjointed. But more is looking for a better chance to find a job that is more suitable for you and more expensive. Well, from today on, every day to summarize a small data structure and algorithm knowledge, one to expand their knowledge, and you understand. two traversal of a fork tree
For the binary tree, there are three kinds of traversal methods: pre-sequence traversal; sequential traversal;
For these three types of traversal, I'll give you a detailed introduction to my approach by following this diagram. Pre-sequence traversal: Traversal of the left subtree, which accesses the root node, traversal of the right subtree: traversing the left subtree, accessing the root node, traversing the right subtree post-order traversal: Traversing the left subtree, traversing the right subtree, accessing the root node
For the two-fork tree in the image above, the results of using three kinds of traversal respectively are: pre-sequence traversal: A->c->d->g->h->s sequence Traversal: D->c->g->a->h->s post-order traversal:d-> G->c->s->h->a
One of the main uses of the binary tree is in the compiler design field, as shown in the figure below for the two-fork tree:
This is an expression tree, which is followed by a subsequent traversal of the binary tree, resulting in:
A B + C d e + d * *
This expression is also called a suffix expression, and many times we need to re-establish a binary tree based on the suffix expression. This requirement is implemented using code as follows. To create an expression tree from a suffix expression
For example, you now have one of the following suffix expressions:
A B + C d e + * *
Based on this suffix expression, a two-fork expression tree is created, with the following algorithm:
1. Read the expression sequentially;
2. In the case of an operand, press the operand into the stack;
3. If it is an operator, the two operands in the pop-up stack, the first one to eject as the right child, the second to eject the operand as the left child, and then the operator to be pushed into the stack.
This way, you can create a complete expression tree. Simple Code Implementation
The following is a simple implementation using C + + code.
Defines the tree node information.
typedef struct BINARYNODE
{
char data;
Binarynode *left;
Binarynode *right;
} TreeNode;
Creates a new node.
treenode* createtreenode (char ch)
{
TreeNode *pnode = new TreeNode;
Pnode->data = ch;
Pnode->left = NULL;
Pnode->right = NULL;
return pnode;
}
Reads an expression, creating an expression tree.
Stack<treenode *> Nodestack;
char ch;
while ((ch = getchar ()) = ' \ n ')
{
TreeNode *pnode = Createtreenode (CH);
if (ch = = ' + ' | | ch = = '-' | | ch = = ' * ' | | ch = = '/')
{
TreeNode *pright = Nodestack.top ();
Nodestack.pop ();
TreeNode *pleft = Nodestack.top ();
Nodestack.pop ();
Pnode->right = pright;
Pnode->left = Pleft;
Nodestack.push (Pnode);
}
else
{
Nodestack.push (pnode);
}
}
Article Source: http://www.jellythink.com/archives/688