Advertisement
mvCode

zbir na pat vo drvo AIPS kolokvium

Jan 26th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. /*
  2. Dadeno e binarno drvo i nekoj broj "suma". Potrebno e da se najde dali vo drvoto postoi pat megju bilo koi dva jazli, taka sto zbirot na
  3. site jazli koi se vkluceni vo patot e ednakov na suma. Pocetniot jazel MORA da e roditel na krajniot jazel vo patot, znaci deka patot mora
  4. da odi nadolu.
  5. patot moze da se sostoi i samo od eden jazel.
  6. * */
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.StringTokenizer;
  10.  
  11. class BNode<E> {
  12.  
  13.     public E info;
  14.     public BNode<E> left;
  15.     public BNode<E> right;
  16.  
  17.     static int LEFT = 1;
  18.     static int RIGHT = 2;
  19.  
  20.     public BNode(E info) {
  21.         this.info = info;
  22.         left = null;
  23.         right = null;
  24.     }
  25.  
  26.     public BNode() {
  27.         this.info = null;
  28.         left = null;
  29.         right = null;
  30.     }
  31.  
  32.     public BNode(E info, BNode<E> left, BNode<E> right) {
  33.         this.info = info;
  34.         this.left = left;
  35.         this.right = right;
  36.     }
  37.  
  38. }
  39.  
  40. class BTree<E> {
  41.  
  42.     public BNode<E> root;
  43.  
  44.     public BTree() {
  45.         root = null;
  46.     }
  47.  
  48.     public BTree(E info) {
  49.         root = new BNode<E>(info);
  50.     }
  51.  
  52.     public void makeRoot(E elem) {
  53.         root = new BNode(elem);
  54.     }
  55.  
  56.     public void makeRootNode(BNode<E> node) {
  57.         root = node;
  58.     }
  59.  
  60.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  61.  
  62.         BNode<E> tmp = new BNode<E>(elem);
  63.  
  64.         if (where == BNode.LEFT) {
  65.             if (node.left != null)  // veke postoi element
  66.                 return null;
  67.             node.left = tmp;
  68.         } else {
  69.             if (node.right != null) // veke postoi element
  70.                 return null;
  71.             node.right = tmp;
  72.         }
  73.  
  74.         return tmp;
  75.     }
  76.  
  77.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  78.  
  79.         if (where == BNode.LEFT) {
  80.             if (node.left != null)  // veke postoi element
  81.                 return null;
  82.             node.left = tmp;
  83.         } else {
  84.             if (node.right != null) // veke postoi element
  85.                 return null;
  86.             node.right = tmp;
  87.         }
  88.  
  89.         return tmp;
  90.     }
  91.     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92.  
  93. }
  94.  
  95.  
  96.  
  97. public class najdiZbirNaNodesEdnakovSoNekojBrVoBTree {
  98.  
  99.  
  100.         public static void main(String[] args) throws Exception {
  101.             int i, j, k;
  102.             int index;
  103.             String action;
  104.  
  105.             String line;
  106.  
  107.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  108.             StringTokenizer st;
  109.  
  110.             int N = Integer.parseInt(br.readLine());
  111.  
  112.             BNode<Integer> nodes[] = new BNode[N];
  113.             BTree<Integer> tree = new BTree<Integer>();
  114.  
  115.             for (i=0; i<N; i++)
  116.                 nodes[i] = new BNode<Integer>();
  117.  
  118.             for (i = 0; i < N; i++) {
  119.                 line = br.readLine();
  120.                 st = new StringTokenizer(line);
  121.                 index = Integer.parseInt(st.nextToken());
  122.                 nodes[index].info = Integer.parseInt(st.nextToken());
  123.                 action = st.nextToken();
  124.                 if (action.equals("LEFT")) {
  125.                     tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  126.                 } else if (action.equals("RIGHT")) {
  127.                     tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  128.                 } else {
  129.                     // this node is the root
  130.                     tree.makeRootNode(nodes[index]);
  131.                 }
  132.             }
  133.  
  134.  
  135.         int suma = Integer.parseInt( br.readLine() );
  136.  
  137.         System.out.println( hasPathSum( tree.root, suma ));
  138.  
  139.     }
  140.  
  141.     public static boolean hasPathSum(BNode root, int sum ) {
  142.  
  143.         if( root == null )
  144.             return false;
  145.  
  146.         if ( hasPathSum(root,sum, 0) )
  147.             return true;
  148.  
  149.         return hasPathSum(root.left,sum) || hasPathSum(root.right,sum);
  150.     }
  151.     public static boolean hasPathSum(BNode root, int sum, int momentSum ){
  152.  
  153.         if( root == null )
  154.             return false;
  155.         if( (int)root.info + momentSum == sum )
  156.             return true;
  157.  
  158.         return( hasPathSum( root.left, sum, (int)root.info + momentSum) ||
  159.                 hasPathSum( root.right, sum, (int)root.info + momentSum));
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement