Mitrezzz

[АПС] Лаб 7: Дрва Збир на елементи во бинарно дрво

Dec 19th, 2018 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. class BNode<E> {
  6.  
  7.     static int LEFT = 1;
  8.     static int RIGHT = 2;
  9.     public E info;
  10.     public BNode<E> left;
  11.     public BNode<E> right;
  12.  
  13.     public BNode(E info) {
  14.         this.info = info;
  15.         left = null;
  16.         right = null;
  17.     }
  18.  
  19.     public BNode() {
  20.         this.info = null;
  21.         left = null;
  22.         right = null;
  23.     }
  24.  
  25.     public BNode(E info, BNode<E> left, BNode<E> right) {
  26.         this.info = info;
  27.         this.left = left;
  28.         this.right = right;
  29.     }
  30.  
  31. }
  32.  
  33. class BTree<E extends Comparable<E>> {
  34.  
  35.     public BNode<E> root;
  36.  
  37.     public BTree() {
  38.         root = null;
  39.     }
  40.  
  41.     public BTree(E info) {
  42.         root = new BNode<E>(info);
  43.     }
  44.  
  45.     public void makeRoot(E elem) {
  46.         root = new BNode(elem);
  47.     }
  48.  
  49.     public void makeRootNode(BNode<E> node) {
  50.         root = node;
  51.     }
  52.  
  53.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  54.  
  55.         BNode<E> tmp = new BNode<E>(elem);
  56.  
  57.         if (where == BNode.LEFT) {
  58.             if (node.left != null)  // veke postoi element
  59.                 return null;
  60.             node.left = tmp;
  61.         } else {
  62.             if (node.right != null) // veke postoi element
  63.                 return null;
  64.             node.right = tmp;
  65.         }
  66.  
  67.         return tmp;
  68.     }
  69.  
  70.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  71.  
  72.         if (where == BNode.LEFT) {
  73.             if (node.left != null)  // veke postoi element
  74.                 return null;
  75.             node.left = tmp;
  76.         } else {
  77.             if (node.right != null) // veke postoi element
  78.                 return null;
  79.             node.right = tmp;
  80.         }
  81.  
  82.         return tmp;
  83.     }
  84.  
  85.     public BNode<Integer> search(BNode<Integer> jazol, int el) {
  86.         BNode<Integer> res = null;
  87.  
  88.            /*
  89.         if(jazol.left != null)
  90.             res = search(jazol.left, el);
  91.         if(jazol.info == el)
  92.             return jazol;
  93.         if(jazol == null & jazol.right != null)
  94.             res = search(jazol.right, el);
  95.          */
  96.         if (jazol == null)
  97.             return null;
  98.         if (jazol.info == el)
  99.             return jazol;
  100.         if (jazol.left != null)
  101.             res = search(jazol.left, el);
  102.         if (res == null)
  103.             res = search(jazol.right, el);
  104.  
  105.         return res;
  106.     }
  107.  
  108.     int zbirLevo(BNode<Integer> jazol, int el) // pomali od nego
  109.     {
  110.         int s = 0;
  111.  
  112.         if (jazol == null)
  113.             return 0;
  114.         if (jazol.info < el)
  115.             s += jazol.info;
  116.         if (jazol.left != null)
  117.             s += zbirLevo(jazol.left, el);
  118.         if (jazol.right != null)
  119.             s += zbirLevo(jazol.right, el);
  120.         return s;
  121.     }
  122.  
  123.     int zbirDesno(BNode<Integer> jazol, int el) // pogolemi od nego
  124.     {
  125.         int s = 0;
  126.  
  127.         if (jazol == null)
  128.             return 0;
  129.         if (jazol.info > el)
  130.             s += jazol.info;
  131.         if (jazol.left != null)
  132.             s += zbirDesno(jazol.left, el);
  133.         if (jazol.right != null)
  134.             s += zbirDesno(jazol.right, el);
  135.         return s;
  136.     }
  137. }
  138.  
  139. public class BinaryTreeSum {
  140.  
  141.  
  142.     public static void main(String[] args) throws Exception {
  143.         int i, j, k;
  144.         int index;
  145.         String action;
  146.  
  147.         String line;
  148.  
  149.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  150.         StringTokenizer st;
  151.  
  152.         int N = Integer.parseInt(br.readLine());
  153.  
  154.         BNode<Integer> nodes[] = new BNode[N];
  155.         BTree<Integer> tree = new BTree<Integer>();
  156.  
  157.         for (i = 0; i < N; i++)
  158.             nodes[i] = new BNode<Integer>();
  159.  
  160.         for (i = 0; i < N; i++) {
  161.             line = br.readLine();
  162.             st = new StringTokenizer(line);
  163.             index = Integer.parseInt(st.nextToken());
  164.             nodes[index].info = Integer.parseInt(st.nextToken());
  165.             action = st.nextToken();
  166.             if (action.equals("LEFT")) {
  167.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  168.             } else if (action.equals("RIGHT")) {
  169.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  170.             } else {
  171.                 // this node is the root
  172.                 tree.makeRootNode(nodes[index]);
  173.             }
  174.         }
  175.  
  176.         int baranaVrednost = Integer.parseInt(br.readLine());
  177.  
  178.         br.close();
  179.  
  180.         // vasiot kod ovde
  181.         BNode<Integer> jazol = tree.search(tree.root, baranaVrednost);
  182.         System.out.println(tree.zbirLevo(jazol.left, jazol.info) + " " + tree.zbirDesno(jazol.right, jazol.info));
  183.     }
  184. }
Add Comment
Please, Sign In to add comment