Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include "tree.h"
  4. #include "LList.h"
  5. #include "Graph.cpp"
  6. using namespace std;
  7.  
  8. bool doYouHaveTheWay(graph<char> g, stack<char> st) {
  9.     bool res = true;
  10.     while (st.size() > 1)
  11.     {
  12.         char b = st.top();
  13.         st.pop();
  14.         char a = st.top();
  15.         if(!g.hasEdge(a, b))
  16.             res = false;
  17.     }
  18.     return res;
  19. }
  20.  
  21. void checkTreeWays(graph<char> const& g,tree<char> const& t, stack<char>& st, bool& res)
  22. {  
  23.     if (t.rootTree())
  24.     {
  25.         st.push(t.getRoot());
  26.  
  27.         if (!t.leftTree().rootTree() && !t.rightTree().rootTree()) {
  28.             if (doYouHaveTheWay(g, st)) {
  29.                 res = true;
  30.                 return;
  31.             }
  32.         }
  33.            
  34.         else {         
  35.             checkTreeWays(g, t.leftTree(),st,res);
  36.             checkTreeWays(g, t.rightTree(),st,res);
  37.         }
  38.         st.pop();
  39.     }
  40. }
  41.  
  42. bool finalCheck(graph<char> const& g, tree<char> const& t, stack<char>& st) {
  43.     bool res = false;
  44.     checkTreeWays(g, t, st, res);
  45.     return res;
  46. }
  47.  
  48.  
  49. int main() {
  50.     tree<char> d;
  51.     d.create();
  52.     /*
  53.         a
  54.         y
  55.         e
  56.         y
  57.         d
  58.         y
  59.         b
  60.         n
  61.         n
  62.         n
  63.         y
  64.         g
  65.         n
  66.         n
  67.         y
  68.         f
  69.         n
  70.         y
  71.         c
  72.         y
  73.         h
  74.         n
  75.         n
  76.         n
  77.     */
  78.     stack<char> st;
  79.     graph<char> g;
  80.     g.addVertex('a');
  81.     g.addVertex('b');
  82.     g.addVertex('c');
  83.     g.addVertex('d');
  84.     g.addVertex('e');
  85.     g.addVertex('f');
  86.     g.addVertex('g');
  87.     g.addVertex('h');
  88.     g.addEdge('a', 'd');
  89.     g.addEdge('a', 'f');
  90.     g.addEdge('b', 'h');
  91.     g.addEdge('c', 'h');
  92.     g.addEdge('d', 'e');
  93.     g.addEdge('e', 'h');
  94.     g.addEdge('f', 'h');
  95.     g.addEdge('f', 'c');
  96.     g.addEdge('g', 'e');   
  97.     g.addEdge('g', 'b');
  98.  
  99.     cout << finalCheck(g, d, st) << endl;
  100.  
  101.     system("pause");
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement