Josif_tepe

Untitled

Jan 11th, 2026
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.57 KB | None | 0 0
  1. import java.rmi.registry.LocateRegistry;
  2.  
  3. public class BTree<E> {
  4.  
  5.     public BNode<E> root;
  6.  
  7.     public BTree() {
  8.         root = null;
  9.     }
  10.  
  11.     public BTree(E info) {
  12.         root = new BNode<E>(info);
  13.     }
  14.  
  15.     public void makeRoot(E elem) {
  16.         root = new BNode<E>(elem);
  17.     }
  18.  
  19.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  20.  
  21.         BNode<E> tmp = new BNode<E>(elem, node);
  22.  
  23.         if (where == BNode.LEFT) {
  24.             if (node.left != null)  // veke postoi element
  25.                 return null;
  26.             node.left = tmp;
  27.         } else {
  28.             if (node.right != null) // veke postoi element
  29.                 return null;
  30.             node.right = tmp;
  31.         }
  32.  
  33.         return tmp;
  34.     }
  35.  
  36.     public void inorder() {
  37.         System.out.print("INORDER: ");
  38.         inorderR(root);
  39.         System.out.println();
  40.     }
  41.  
  42.     public void inorderR(BNode<E> n) {
  43.         if (n != null) {
  44.             inorderR(n.left);
  45.             System.out.print(n.info.toString()+" ");
  46.             inorderR(n.right);
  47.         }
  48.     }
  49.  
  50.     public void preorder() {
  51.         System.out.print("PREORDER: ");
  52.         preorderR(root);
  53.         System.out.println();
  54.     }
  55.  
  56.     public void preorderR(BNode<E> n) {
  57.         if (n != null) {
  58.             System.out.print(n.info.toString()+" ");
  59.             preorderR(n.left);
  60.             preorderR(n.right);
  61.         }
  62.     }
  63.  
  64.     public void postorder() {
  65.         System.out.print("POSTORDER: ");
  66.         postorderR(root);
  67.         System.out.println();
  68.     }
  69.  
  70.     public void postorderR(BNode<E> n) {
  71.         if (n != null) {
  72.             postorderR(n.left);
  73.             postorderR(n.right);
  74.             System.out.print(n.info.toString()+" ");
  75.         }
  76.     }
  77.  
  78.     public void inorderNonRecursive() {
  79.         ArrayStack<BNode<E>> s = new ArrayStack<BNode<E>>(100);
  80.         BNode<E> p = root;
  81.         System.out.print("INORDER (nonrecursive): ");
  82.  
  83.         while (true) {
  84.             // pridvizuvanje do kraj vo leva nasoka pri sto site koreni
  85.             // na potstebla se dodavaat vo magacin za podocnezna obrabotka
  86.             while (p != null) {
  87.                 s.push(p);
  88.                 p = p.left;
  89.             }
  90.  
  91.             // ako magacinot e prazen znaci deka stebloto e celosno izminato
  92.             if (s.isEmpty())
  93.                 break;
  94.  
  95.             p = s.peek();
  96.             // pecatenje (obrabotka) na jazelot na vrvot od magacinot
  97.             System.out.print(p.info.toString()+" ");
  98.             // brisenje na obraboteniot jazel od magacinot
  99.             s.pop();
  100.             // pridvizuvanje vo desno od obraboteniot jazel i povtoruvanje na
  101.             // postapkata za desnoto potsteblo na jazelot
  102.             p = p.right;
  103.  
  104.         }
  105.         System.out.println();
  106.  
  107.     }
  108.  
  109.     int insideNodesR(BNode<E> node) {
  110.         if (node == null)
  111.             return 0;
  112.  
  113.         if ((node.left == null)&&(node.right == null))
  114.             return 0;
  115.  
  116.         return insideNodesR(node.left) + insideNodesR(node.right) + 1;
  117.     }
  118.  
  119.     int listoviR(BNode<E> node) {
  120.         if (node == null)
  121.             return 0;
  122.  
  123.         if ((node.left == null)&&(node.right == null))
  124.             return 1;  // ova e list, nema deca
  125.  
  126.         //ako ne e list, ke vleze vo sledno:
  127.         return insideNodesR(node.left) + insideNodesR(node.right);
  128.     }
  129.  
  130.     int dlabocinaR(BNode<E> node) {
  131.         if (node == null)
  132.             return 0;
  133.  
  134.         if ((node.left == null)&&(node.right == null))
  135.             return 1;  // ova e list, nema deca
  136.  
  137.         //ako ne e list, ke presmeta max dlabocina od levo i desno
  138.         return Math.max(dlabocinaR(node.left) ,
  139.                 dlabocinaR(node.right))+1;
  140.     }
  141.  
  142.  
  143.     public int insideNodes() {
  144.         return listoviR(root);
  145.     }
  146.  
  147.     int leavesR(BNode<E> node) {
  148.         if (node != null) {
  149.             if ((node.left == null)&&(node.right == null))
  150.                 return 1;
  151.             else
  152.                 return (leavesR(node.left) + leavesR(node.right));
  153.         } else {
  154.             return 0;
  155.         }
  156.     }
  157.  
  158.     public int leaves() {
  159.         return leavesR(root);
  160.     }
  161.  
  162.     int depthR(BNode<E> node) {
  163.         if (node == null)
  164.             return 0;
  165.         if ((node.left == null)&&(node.right == null))
  166.             return 0;
  167.         return (1 + Math.max(depthR(node.left), depthR(node.right)));
  168.     }
  169.  
  170.     public int depth() {
  171.         return depthR(root);
  172.     }
  173.  
  174.     void mirrorR(BNode<E> node) {
  175.         BNode<E> tmp;
  176.  
  177.         if (node == null)
  178.             return;
  179.  
  180.         // simetricno preslikuvanje na levoto i desnoto potsteblo
  181.         mirrorR(node.left);
  182.         mirrorR(node.right);
  183.  
  184.         // smena na ulogite na pokazuvacite na momentalniot jazel
  185.         tmp = node.left;
  186.         node.left = node.right;
  187.         node.right = tmp;
  188.  
  189.     }
  190.  
  191.     private BNode<E> findR(BNode<E> node, E what){
  192.  
  193.         if (node==null){  //ako ne postoi jazol, vrati null
  194.             return null;
  195.         }
  196.  
  197.         // ako si go nasol toa sto go baras, vrati go jazolot
  198.         if(node.info.equals(what)){
  199.             return node;
  200.         }
  201.  
  202.         //baraj levo
  203.         BNode<E> found=findR(node.left, what);
  204.         if(found!=null){  //ako si go nasol levo
  205.             return found; //vrati go
  206.         }
  207.  
  208.         //baraj desno
  209.         found=findR(node.right, what);
  210.         if(found!=null) { //ako si go nasol desno
  211.             return found; //vrati go
  212.  
  213.         }
  214.         return null; //go nema kaj nas i kaj nasite deca, vrati null
  215.  
  216.     }
  217.  
  218.     public void preorderIterative(){
  219.         //inorder:  koren, levo, desno
  220.  
  221.         BNode<E> current=root;
  222.  
  223.         ArrayStack<BNode<E>> stack= new ArrayStack<BNode<E>>(100);
  224.  
  225.         stack.push(current);
  226.         while (!stack.isEmpty()){
  227.  
  228.  
  229.             current=stack.pop();
  230.             System.out.println(current.info.toString());//printaj go momentalniot
  231.             //prvo desno
  232.             if(current.right!=null){
  233.                 stack.push(current.right);
  234.             }
  235.             //pa levo, zatoa sto sledniot stack pop ke go izvadi prvo posledniot
  236.             if(current.left!=null){
  237.                 stack.push(current.left);
  238.             }
  239.         }
  240.     }
  241.  
  242.     public BNode<E> find(E what){
  243.         return findR(root,what);
  244.     }
  245.  
  246.     public void mirror() {
  247.         mirrorR(root);
  248.     }
  249.  
  250.     public int dijametar() {
  251.         return dijametarR(root);
  252.     }
  253.     public int visina(BNode<E> node) {
  254.         if(node == null) {
  255.             return 0;
  256.         }
  257.         return Math.max(visina(node.left), visina(node.right)) + 1;
  258.     }
  259.  
  260.     public int dijametarR(BNode<E> node) {
  261.         if(node == null) {
  262.             return 0;
  263.         }
  264.  
  265.         int leftVisina = visina(node.left);
  266.         int rightVisina = visina(node.right);
  267.  
  268.         int leftDijametar = dijametarR(node.left);
  269.         int rightDijametar = dijametarR(node.right);
  270.  
  271.         return Math.max(Math.max(leftDijametar, rightDijametar), leftVisina + rightVisina + 1);
  272.  
  273.     }
  274.  
  275.     public int countFullNodesRec(BNode<E> node) {
  276.         if(node == null) {
  277.             return 0;
  278.         }
  279.         int res = 0;
  280.         if(node.left != null && node.right != null) {
  281.             res = 1;
  282.         }
  283.         return res + countFullNodesRec(node.left) + countFullNodesRec(node.right);
  284.     }
  285.  
  286.     public int countFullNode() {
  287.         return countFullNodesRec(root);
  288.     }
  289.  
  290.     public void ZigZag() {
  291.         if(root == null) {
  292.             return;
  293.         }  
  294.  
  295.         ArrayStack<BNode<E>> level = new ArrayStack<>(100);
  296.         ArrayStack<BNode<E>> nextLevel = new ArrayStack<>(100);
  297.  
  298.         level.push(root);
  299.         boolean leftToRight = true;
  300.         while(!level.isEmpty()) {
  301.             BNode<E> node = level.pop();
  302.             System.out.print(node.info + " ");
  303.  
  304.             if(leftToRight) {
  305.                 if(node.left != null) {
  306.                     nextLevel.push(node.left);
  307.                 }
  308.                 if(node.right != null) {
  309.                     nextLevel.push(node.right);
  310.                 }
  311.             }
  312.             else {
  313.                 if(node.right != null) {
  314.                     nextLevel.push(node.right);
  315.                 }
  316.                 if(node.left != null) {
  317.                     nextLevel.push(node.left);
  318.                 }
  319.             }
  320.  
  321.             if(level.isEmpty()) {
  322.                 leftToRight = !leftToRight;
  323.                 ArrayStack<BNode<E>> tmp = level;
  324.                 level = nextLevel;
  325.                 nextLevel = tmp;
  326.                 System.out.println();
  327.  
  328.             }
  329.         }
  330.     }
  331.  
  332.     public BNode<E> LCA(E A, E B) {
  333.         return LCArec(root, A, B);
  334.     }
  335.  
  336.     private LCArec(BNode<E> node, E A, E B) {
  337.         if(node == null) {
  338.             return null;
  339.         }
  340.  
  341.         if(node.info.equals(A) || node.info.equals(B)) {
  342.             return node;
  343.         }
  344.  
  345.         BNode<E> leftSearch = LCARec(node.left, A, B);
  346.         BNode<E> rightSearch = LCArec(node.right, A, B);
  347.  
  348.         if(leftSearch != null && rightSearch != null) {
  349.             return node;
  350.         }
  351.  
  352.         if(leftSearch != null) {
  353.             return leftSearch;
  354.         }
  355.         return rightSearch;
  356.  
  357.  
  358.     }
  359.  
  360.     public boolean isBalanced() {
  361.         return isBalanced(root);
  362.     }
  363.  
  364.     private boolean isBalanced(BNode<E> node) {
  365.         if(node == null) {
  366.             return true;
  367.         }
  368.         int l = depthR(node.left);
  369.         int r = depthR(node.right);
  370.  
  371.         if(Math.abs(l - r) <= 1 && isBalanced(node.left) && isBalanced(node.right)) {
  372.             return true;
  373.         }
  374.         return false;
  375.     }
  376.  
  377. }
  378.  
Advertisement
Add Comment
Please, Sign In to add comment