Algorithm to print the different generation of a tree
To write a function that takes the root of a family tree as an input and prints the names of individuals belonging to each generation on a separate line.
1. 1.If you have Xcode installed open the Adobe_Test.xcodeproj file. 2. 2.Hit the build button at the top right. 3. Watch the console for the output. 1. Move to the directory where main.m is situated 2. Example [DownloadedPath]/Adobe_Test/Adobe_test 3. And execute the following command. 4. clang -framework Foundation main.m Tree.m NSMutableArray+Queue.m -o output 5. Finally run the executable file. /output to see the output.Objective-C
O(n)
Size of queue(NSMutableArray) which is equivalent to maximum size of nodes in same generation.
This is a tree and not a graph. (Invalid when lower generation has a higher generation parent)
main.m : main function which builds tree and executes the algorithm. Tree.m : Model Class for Tree Structure. NSMutableArray+Queue.m : Extended NSMutableArray and added enque and deque
A
A1 A2
A11 A12 A21 A22
Generation 1: A
Generation 2: A1, A2
Generation 3: A11, A12 , A21, A22
The following test cases are already executed in the program.
- Normal flow with root!=null and many valid branches.
- root=null.
- Initialize few elements in a tree without a name.
- Tree with just one branch of children.
- Finally test with bigger load of input.