Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. const int MAX_SIZE = 100;
  5.  
  6. class Tree{
  7. public:
  8. void createTree();
  9. void printInorder(int index);
  10. int getRootIndex();
  11. private:
  12. int data[MAX_SIZE];
  13. int leftNodes[MAX_SIZE];
  14. int rightNodes[MAX_SIZE];
  15. int rootIndex;
  16. };
  17.  
  18. void Tree::createTree()
  19. {
  20.  
  21. int n;
  22. cout<<"Enter length of nodes array: ";
  23. cin>>n;
  24. cout<<"Enter nodes array:"<<endl;
  25. for(int i = 0; i < n; i++){
  26. cout<<"Node on index " << i <<": ";
  27. cin>>data[i];
  28. }
  29.  
  30. cout<<"Enter index of the root:"<<endl;
  31. cin>>rootIndex;
  32.  
  33. cout<<"Enter left nodes indexes:"<<endl;
  34. for(int i = 0; i < n; i++){
  35. cout<<"Node on index " << i <<": ";
  36. cin>>leftNodes[i];
  37. }
  38.  
  39. cout<<"Enter right nodes indexes:";
  40. for(int i = 0; i < n; i++){
  41. cout<<"Node on index " << i <<": ";
  42. cin>>rightNodes[i];
  43. }
  44. }
  45.  
  46. int Tree::getRootIndex()
  47. {
  48. return rootIndex;
  49. }
  50.  
  51. void Tree::printInorder(int index)
  52. {
  53.  
  54. if(leftNodes[index] != -1){
  55. printInorder(leftNodes[index]);
  56. }
  57. cout<<data[index]<<" ";
  58.  
  59. if(rightNodes[index] != -1){
  60. printInorder(rightNodes[index]);
  61. }
  62.  
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68. Tree testTree;
  69.  
  70. testTree.createTree();
  71. cout<< testTree.getRootIndex()<<endl;
  72. testTree.printInorder(testTree.getRootIndex());
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement