help me convert java to c++ please

Programming help to find path B to A , B to C and B to D.

inputfile : http://image.ohozaa.com/view2/xhfjIjnbQ4HVdSjM

outputfile : http://image.ohozaa.com/view2/xhfk3W4N8rko5GyN

maze : http://image.ohozaa.com/view2/xhfhacR1mVff7ug6

tree : http://image.ohozaa.com/view2/xhfhuLCzORfebDNL

I have java code but i want to c++ code. Help me convert java code to c++ please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package findexit;

import java.util.ArrayList;

public class FindExit {
	
	public static ArrayList<Path> mapPath = new ArrayList<Path>();
	
	public static void main(String args[]) {
		
		System.out.println("Start ");
		
		FindExit startPath = new FindExit();
		startPath.setItem(); // set tree
		
		ArrayList<String> root  = new ArrayList<String>();
		root.add("S1"); // Start Node
		
		startPath.findExit(root);
	}

	public void setItem() {
		mapPath.add(new Path("S0 GateZ"));
		mapPath.add(new Path("S1", "S0 GateZ", "S3"));
		mapPath.add(new Path("S2"));
		mapPath.add(new Path("S3", "S2", "S15"));
		mapPath.add(new Path("S4"));
		mapPath.add(new Path("S5", "S4", "S7"));
		mapPath.add(new Path("S6"));
		mapPath.add(new Path("S7", "S6", "S9"));
		mapPath.add(new Path("S8"));
		mapPath.add(new Path("S9", "S8", "S11"));
		mapPath.add(new Path("S10"));
		mapPath.add(new Path("S11", "S10", "S13"));
		mapPath.add(new Path("S12 GateC"));
		mapPath.add(new Path("S13", "S12 GateC", "S14 GateD"));
		mapPath.add(new Path("S14 GateD"));
		mapPath.add(new Path("S15", "S5", "S17"));
		mapPath.add(new Path("S16 GateA"));
		mapPath.add(new Path("S17", "S16 GateA", "S18"));
		mapPath.add(new Path("S18"));
		
	}

	public void findExit(ArrayList<String> rootNode) {
		
		if (rootNode == null) 
			return;
		
		// Get name of last node from Arraylist
		String lastNode = rootNode.get(rootNode.size()-1);
		// Convert String to Path Item
		Path mItem = findItem(lastNode);
		
		String root = mItem.getmPath();  
		String left = mItem.getLeftNode(); 
		String right = mItem.getRightNode(); 
		
		// if node is leaf and not exit node
		if (left.startsWith("-") && right.startsWith("-")) {
			return ;
		}
		
		ArrayList<String> leftResult  = new ArrayList<String>();
		ArrayList<String> rightResult  = new ArrayList<String>();
		
		for(int i = 0; i < rootNode.size(); i++) {
			leftResult.add(rootNode.get(i));
			rightResult.add(rootNode.get(i));
		}
		
		// travel in right side
		rightResult.add(right);
		if (right.contains("Gate")) {
			printArrayList(rightResult);
		} else {
			findExit(rightResult);
		}
		
		// travel in left side
		leftResult.add(left);
		if (left.contains("Gate")) {
			printArrayList(leftResult);
		} else {
			findExit(leftResult);
		}
		return;
	}
	
	/* Get Node Item from mapPath ArrayList */
	public Path findItem(String nameNode) {
		if (nameNode != null && !nameNode.equals("-") && !nameNode.equals("")) {
			for(int i = 0; i < mapPath.size(); i++ ) {
				if ( nameNode.equals(mapPath.get(i).getmPath())) {
					return mapPath.get(i);
				}
			}
		}
		return null;
	}
	
	public void printArrayList(ArrayList<String> findExit) {
		
		// find name of exit node
		String exitPath = findExit.get(findExit.size()-1).split(" ")[1].substring("Gate".length());
		
		System.out.print("B -> " + exitPath + ":  ");
		
		for (int i = 0; i < findExit.size(); i++) {
			if (i == findExit.size()-1) {
				System.out.print(findExit.get(i).split(" ")[0]);
				break;
			}
			System.out.print(findExit.get(i) + " -> ");
		}
		System.out.println("");
	}
	
	private class Path {
		
		private String mLeftPath;
		private String mRightPath;
		private String mRootPath;
		
		public Path(String path) {
			this(path, "-", "-");
		}
		
		public Path(String exitNode, String leftIndex, String RightPath) {
			mRootPath = exitNode;
			mLeftPath = leftIndex;
			mRightPath = RightPath;
		}
		
		public String getLeftNode() {
			return mLeftPath;
		}
		
		public String getRightNode() {
			return mRightPath;
		}
		
		public String getmPath() {
			return mRootPath;
		}
		
		
	}
}
Do you.... Know any C++?
I don't know . I try to write about this, but it also failed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
using namespace std;
int a[30];
// Node class
class Node {
    int value;
    int key;
    Node* left;
    Node* right;
public:
    Node() { key=-1; left=NULL; right=NULL; };
    void setKey(int aKey) { key = aKey; };
    void setLeft(Node* aLeft) { left = aLeft; };
    void setRight(Node* aRight) { right = aRight; };
    int Key() { return key; };
    Node* Left() { return left; };
    Node* Right() { return right; };
    bool search(int value);
    bool searchValue(int value);
};

// Tree class
class Tree {
     //int value;
     Node* root;
public:
     Tree();
     ~Tree();
     Node* Root() { return root; };
     Node* search(int key);
    
    // bool search(int value);
    // bool searchValue(int value);
     void addNode(int key);
     void inOrder(Node* n);
     void preOrder(Node* n);
     void postOrder(Node* n);
private:
     Node* search(int key, Node* leaf);   
     void addNode(int key, Node* leaf);
     void freeNode(Node* leaf);
};

// Constructor
Tree::Tree() {
     root = NULL;
}

// Destructor
Tree::~Tree() {
     freeNode(root);
}

// Free the node
void Tree::freeNode(Node* leaf)
{
    if ( leaf != NULL )
    {
       freeNode(leaf->Left());
       freeNode(leaf->Right());
       delete leaf;
    }
}

// Add a node
void Tree::addNode(int key) {
     // No elements. Add the root
     if ( root == NULL ) {
        cout << "add root node ... " << key << endl;
        Node* n = new Node();
        n->setKey(key);
        root = n;
     }
     else {
       cout << "add other node ... " << key << endl;
       addNode(key, root);
     }
}



Node* Tree::search(int key, Node* leaf)
{
  if(leaf!=NULL)
  {
    if(key==leaf->value)
      return leaf;
    if(key<leaf->value)
      return search(key, leaf->left);
    else
      return search(key, leaf->right);
  }
  else return NULL;
}

Node* Tree::search(int key)
{
  return search(key, root);
}




// Add a node (private)
void Tree::addNode(int key, Node* leaf) {
    if ( key <= leaf->Key() ) {
       if ( leaf->Left() != NULL )
          addNode(key, leaf->Left());
       else {
          Node* n = new Node();
          n->setKey(key);
          leaf->setLeft(n);
       }
    }
    else {
       if ( leaf->Right() != NULL )
          addNode(key, leaf->Right());
       else {
          Node* n = new Node();
          n->setKey(key);
          leaf->setRight(n);
       }
    }
}
// now it error
/*bool Tree::search(int value) {
      if (root == NULL)
             
            return false;
      else
            return root->searchValue(value);
}
bool Tree::searchValue(int value) {
      if (value == root->Key())
            return true;
      else if (value < root->Key()) {
            if (left == NULL)
                  return false;
            else
                  return left->search(value);
      } else if (value > root->value) {
            if (right == NULL)
                  return false;
            else
                  return right->search(value);
      }
      return false;
}*/

// error
// Print the tree in-order
// Traverse the left sub-tree, root, right sub-tree
void Tree::inOrder(Node* n) {
    if ( n ) {
       inOrder(n->Left());
       cout << n->Key() << " ";
       inOrder(n->Right());
    }
}

// Print the tree pre-order
// Traverse the root, left sub-tree, right sub-tree
void Tree::preOrder(Node* n) {
    if ( n ) {
       cout << n->Key() << " ";
       preOrder(n->Left());
       preOrder(n->Right());
    }
}

// Print the tree post-order
// Traverse left sub-tree, right sub-tree, root
void Tree::postOrder(Node* n) {
    if ( n ) {
       postOrder(n->Left());
       postOrder(n->Right());
       cout << n->Key() << " ";
    }
}


// Test main program
int main() {
   Tree* tree = new Tree();
   tree->addNode(7);
   tree->addNode(5);
   tree->addNode(3);
   tree->addNode(1);
   tree->addNode(0);
   tree->addNode(2);
   tree->addNode(4);
   tree->addNode(6);
   tree->addNode(9);
   tree->addNode(8);
   tree->addNode(11);
   tree->addNode(10);
   tree->addNode(12);
   //tree->addNode(1);
  // tree->addNode(0);


  /* cout << "In order traversal" << endl;
   tree->inOrder(tree->Root());
   cout << endl; */

   cout << "Pre order traversal" << endl;
   tree->preOrder(tree->Root());
   cout << endl;
   search(16);

  /* cout << "Post order traversal" << endl;
   tree->postOrder(tree->Root());
   cout << endl; */

   delete tree;
   system("pause");
}
Topic archived. No new replies allowed.