Advertisement
Guest User

Teori graf (c++)

a guest
Dec 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. /* A binary tree node has data, pointer to left child
  7. and a pointer to right child */
  8.  
  9. struct Node
  10. {
  11.  
  12.     int data;
  13.  
  14.     struct Node* left, *right;
  15.  
  16.     Node(int data)
  17.  
  18.     {
  19.  
  20.         this->data = data;
  21.  
  22.         left = right = NULL;
  23.  
  24.     }
  25. };
  26.  
  27.  
  28. /* Given a binary tree, print its nodes according to the
  29. "bottom-up" postorder traversal. */
  30.  
  31. void printPostorder(struct Node* node)
  32. {
  33.  
  34.     if (node == NULL)
  35.  
  36.         return;
  37.  
  38.  
  39.  
  40.     // first recur on left subtree
  41.  
  42.     printPostorder(node->left);
  43.  
  44.  
  45.  
  46.     // then recur on right subtree
  47.  
  48.     printPostorder(node->right);
  49.  
  50.  
  51.  
  52.     // now deal with the node
  53.  
  54.     cout << node->data << " ";
  55. }
  56.  
  57.  
  58. /* Given a binary tree, print its nodes in inorder*/
  59.  
  60. void printInorder(struct Node* node)
  61. {
  62.  
  63.     if (node == NULL)
  64.  
  65.         return;
  66.  
  67.  
  68.  
  69.     /* first recur on left child */
  70.  
  71.     printInorder(node->left);
  72.  
  73.  
  74.  
  75.     /* then print the data of node */
  76.  
  77.     cout << node->data << " ";
  78.  
  79.  
  80.  
  81.     /* now recur on right child */
  82.  
  83.     printInorder(node->right);
  84. }
  85.  
  86.  
  87. /* Given a binary tree, print its nodes in preorder*/
  88.  
  89. void printPreorder(struct Node* node)
  90. {
  91.  
  92.     if (node == NULL)
  93.  
  94.         return;
  95.  
  96.  
  97.  
  98.     /* first print data of node */
  99.  
  100.     cout << node->data << " ";
  101.  
  102.  
  103.  
  104.     /* then recur on left sutree */
  105.  
  106.     printPreorder(node->left);  
  107.  
  108.  
  109.  
  110.     /* now recur on right subtree */
  111.  
  112.     printPreorder(node->right);
  113. }  
  114.  
  115.  
  116. /* Driver program to test above functions*/
  117.  
  118. int main()
  119. {
  120.  
  121.     struct Node *root = new Node(1);
  122.  
  123.     root->left             = new Node(2);
  124.  
  125.     root->right         = new Node(3);
  126.  
  127.     root->left->left     = new Node(4);
  128.  
  129.     root->left->right = new Node(5);  
  130.  
  131.  
  132.  
  133.     cout << "\nPreorder traversal of binary tree is \n";
  134.  
  135.     printPreorder(root);
  136.  
  137.  
  138.  
  139.     cout << "\nInorder traversal of binary tree is \n";
  140.  
  141.     printInorder(root);  
  142.  
  143.  
  144.  
  145.     cout << "\nPostorder traversal of binary tree is \n";
  146.  
  147.     printPostorder(root);
  148.  
  149.  
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement