Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
here is code i need it for my project
but i am not familiar with c++
can any body change it to java plz?:(

C++
void iterativePostOrder(Node* root) {
        if (!root) {
                return;
        }
        stack<node*> nodeStack;
 
        Node* cur = root;
        while (true) {
                if (cur) {
                        if (cur->right) {
                                nodeStack.push(cur->right);
                        }
                        nodeStack.push(cur);
                        cur = cur->left;
                        continue;
                }
 
                if (nodeStack.empty()) {
                        return;
                }
 
                cur = nodeStack.top(); 
                nodeStack.pop();
 
                if (cur->right && !nodeStack.empty() && cur->right == nodeStack.top()) {
                        nodeStack.pop();
                        nodeStack.push(cur);
                        cur = cur->right;
                } else {
                        std::cout << cur->val << " ";
                        cur = NULL;
                }
        }
}
Posted

private void iterativePostOrder(Node root)
{
		if (root == null)
		{
				return;
		}
		java.util.Stack<node> nodeStack = new java.util.Stack<node>();

		Node cur = root;
		while (true)
		{
				if (cur != null)
				{
						if (cur.right)
						{
								nodeStack.push(cur.right);
						}
						nodeStack.push(cur);
						cur = cur.left;
						continue;
				}

				if (nodeStack.empty())
				{
						return;
				}

				cur = nodeStack.peek();
				nodeStack.pop();

				if (cur.right && !nodeStack.empty() && cur.right == nodeStack.peek())
				{
						nodeStack.pop();
						nodeStack.push(cur);
						cur = cur.right;
				}
				else
				{
						System.out.print(cur.val);
						System.out.print(" ");
						cur = null;
				}
		}
}</node></node>


Used this demo Converter[^]
 
Share this answer
 
How do you know it's useful when you can't even read it?

the Node* is a pointer. You can treat it here in the same way you would treat an interface.

You also might want to read this:
Tutorial on XML @ oracle.com[^]
samples on XML-API @oracle.com
 
Share this answer
 
Comments
DominoBoy 5-Dec-11 3:21am    
its from wikipedia so its usefull ;)
TorstenH. 8-Dec-11 1:48am    
The web is lying.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900