My problem is, i want to do Mirroring a binary tree using array however after trying so many methods, i just cant get the right answer..can anyone help me?
- first printing the binary tree and then mirroring the binary tree...tnx..here's my simple code...
=--=-=---
import java.util.*;
import java.io.*;
public class mirror {
public static void main(String[] args)throws FileNotFoundException {
Scanner file = new Scanner(new FileReader("BinaryTree.txt"));
int max = 200;
int[] index = new int[max];
int[] info = new int[max];
int[] left = new int[max];
int[] right = new int[max];
int x = 0;
int z = 0;
while(file.hasNext())
{
index[x] = file.nextInt();
info[x] = file.nextInt();
left[x] = file.nextInt();
right[x] = file.nextInt();
System.out.println(index[x] + " " + info[x] +" "+ left[x] +" "+right[x]);
x++;
}
mirror(index, info, left, right);
}
public static void mirror(int[] index, int[] info,int[] left, int[] right)
{
int[] temp=new int[100];
for(int k=1;k<info.length;k++)
{
temp[k] = info[left[k]];
info[left[k]] = info[right[k]];
info[right[k]] = temp[k];
}
}
}
=-------AND this are the values in BinaryTree.txt-----===
0 13 4 2
1 31 6 -1
2 25 7 1
3 12 -1 -1
4 10 5 3
5 2 4 7
6 29 -1 -1
7 20 -1 -1
where first column is the index, second is the info, third the left, fourth the right;
the implementation should be, if left is equal to 4 the left child of the root node should be 10 as it stated on the index column and so on...
pls help..tnx..
newbie.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks