Advertisement
Guest User

פעולות לבגרות

a guest
May 12th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 21.41 KB | None | 0 0
  1. public class Helpy
  2. {
  3.     // -------------------------------------------------------------
  4.     //                      ARRAY
  5.     //create a new arr: {type}[] myIntArray = new {type}[3];
  6.     //
  7.     //
  8.     // כדי לשנות למינימום צריך לשנות את הסימן
  9.     // מוצא את ה אינדקס של האיבר הכי גדול
  10.     public static int maxI(int[] arr) {
  11.         int max = arr[0];
  12.         for (int i=1;i<arr.length;i++) {
  13.             if (max<arr[i]) max = arr[i];
  14.         }
  15.         for (int i=1;i<arr.length;i++) {
  16.             if (max == arr[i])  return (i);
  17.  
  18.         }
  19.         return (1234);
  20.     }
  21.  
  22.     public static int[] copyArr(int[]arr) {
  23.         int[]copyArr = new int[arr.length];
  24.         for (int i=0; i<arr.length; i++) {
  25.             copyArr[i] = arr[i];
  26.         }
  27.         return copyArr;
  28.     }
  29.    
  30.    
  31.     // בודק אם בסדר מסוים כדי לשנות סדר צריך לשנות את הסימן
  32.     public static boolean inOrder(int[]arr) {
  33.         for (int i = 0; i <arr.length-1;i++) {
  34.             if (arr[i]<arr[i+1]) return (false);}
  35.         return (true);}
  36.     // מחליף בין 2 איברים
  37.     public static void change (int[] arr,int x , int y ) {
  38.     int temp = arr[x];
  39.     arr[x] = arr[y];
  40.     arr[y] = temp;
  41. }
  42.  
  43.     // מסדר
  44.     public static void arrange ( int[] arr) {
  45.  
  46.        
  47.        
  48.     // left - big   right - small
  49.         int counter = 0;
  50.         for (int i = 1; i <arr.length;i++) {
  51.             counter++;
  52.             if (i==arr.length-1) i=1;
  53.             if (arr[i]<arr[i+1]) change(arr,i,i+1);
  54.             if (arr[i]>arr[i-1]) change(arr,i,i-1);
  55.            
  56.             if (inOrder(arr)) break;
  57.             hhjhjk
  58.            
  59.         }
  60.         System.out.println(counter);
  61.         }
  62.    
  63.    
  64.    
  65.    
  66.  
  67.  
  68.     //  ------------------------------------------------------------
  69.     //                      LIST / NODE
  70.     //create new node: Node <Integer> name = new Node<Integer>
  71.     //
  72.    
  73.     // מעתיק node
  74.     public static Node<Integer> copyList(Node<Integer> l) {
  75.         Node<Integer> newList = null;
  76.         while(l != null){
  77.             newList = new Node<Integer>(l.getValue(), newList);
  78.             l = l.getNext();
  79.         }
  80.         return newList;
  81.     }
  82.  
  83.     //list length
  84.     public static int listLength(Node<Integer> l) {
  85.         int m = 1;
  86.         while (l.hasNext()) {
  87.             m++;
  88.             l = l.getNext();
  89.         }
  90.         return m;
  91.     }
  92.  
  93.     //מוסיף לרשימה ערך
  94.     public static void addToList(Node<Integer> list, int n){
  95.         Node<Integer> node = new Node<Integer>(n);
  96.         while (list.hasNext()){
  97.             list = list.getNext();
  98.         }
  99.         list.setNext(node);
  100.     }
  101.     // לוקח איבר מחזרים אם קיים ברשימה
  102.     public static boolean existInList(Node<Integer> l, int n){
  103.         Node<Integer> pos = l;
  104.         while (pos != null)
  105.         {
  106.             if (pos.getValue() == n)
  107.                 return true;
  108.             pos = pos.getNext();
  109.         }
  110.         return false;}
  111.         // סכום רשימה
  112.    
  113.     public static int sumList(Node<Integer> l){
  114.         int sum = 0;
  115.         Node<Integer> first = l;
  116.         while (first != null)
  117.         {
  118.             sum += first.getValue();
  119.             first = first.getNext();
  120.         }
  121.         return sum;
  122.     }
  123.        
  124.     public static int placeInList(Node<Integer> node, int value) {
  125.         //       טענת כניסה: הפעולה מקבלת שרשרת חוליות של מספרים שלמים ומספר שלם
  126.         //       טענת יציאה: הפעולה מחזירה את המקום של המספר השלם בשרשרת. אם הוא לא קיים יוחזר הערך 1
  127.         //       סיבוכיות זמן ריצה: O(n)
  128.         Node<Integer> p = node;
  129.         int i=0;
  130.         while (p!=null){
  131.             if (p.getValue()==value)
  132.                 return i;
  133.             i++;
  134.             p=p.getNext();
  135.         }
  136.         return -1;
  137.     }
  138.  
  139.  
  140.  
  141.     public static <T> Node<T> removeNodeFromList(Node<T> list, Node<T> p) {
  142.         //       p טענת כניסה: הפעולה מקבלת שרשרת חוליות וחוליה להסרה
  143.         //       טענת יציאה: הפעולה מסירה את החוליה מהשרשרת
  144.         //       סיבוכיות זמן ריצה: O(n)
  145.         if (list.getValue() == p.getValue()) {
  146.             list = list.getNext();
  147.             return list;
  148.         }
  149.         else {
  150.             Node<T> prev = list;
  151.             while (prev!= null && prev.hasNext()) {
  152.                 if (prev.getNext().getValue()==p.getValue())
  153.                     prev.setNext(prev.getNext().getNext());
  154.                 prev = prev.getNext();
  155.             }
  156.             return list;
  157.         }
  158.     }
  159.  
  160.     public static Node<Integer> insertSortedList(Node<Integer> l, int x) {
  161.         //       טענת כניסה: הפעולה מקבלת שרשרת חוליות ממויינת ומספר שלם
  162.         //       טענת יציאה: הפעולה מכניסה באופן ממויין את המספר לשרשרת
  163.         //       סיבוכיות זמן ריצה: O(n)
  164.         if (l==null) {
  165.             l = new Node<Integer>(x);
  166.             return l;
  167.         }
  168.         Node<Integer> p = l, p1 = l.getNext();
  169.         Node<Integer> tmp = new Node<Integer>(x);
  170.         if (x<l.getValue()) {
  171.             tmp.setNext(l);
  172.             l=tmp;
  173.             return l;
  174.         }
  175.         while (p1!=null && p1.getValue()<x){
  176.             p = p1;
  177.             p1 = p1.getNext();
  178.         }
  179.         tmp.setNext(p1);
  180.         p.setNext(tmp);
  181.         return l;
  182.     }
  183.  
  184.     public static Node<Integer> sortList(Node<Integer> l) {
  185.         //       טענת כניסה: הפעולה מקבלת שרשרת חוליות של מספרים שלמים
  186.         //       טענת יציאה: הפעולה ממיינת את השרשרת בסדר עולה
  187.         //       סיבוכיות זמן ריצה: O(n^2)
  188.         Node<Integer> l1 = null, p=l;
  189.         int x;
  190.         while (p!=null) {
  191.             x = p.getValue();
  192.             l1 = insertSortedList(l1, x);  // !
  193.             p = p.getNext();
  194.         }
  195.         return l;
  196.     }
  197.  
  198.     // בודק כמה פעמים מספר מופיע ברשימה
  199.     public static int howMuchInList(Node<Integer> l, int n){
  200.         int count = 0;
  201.         Node<Integer> pos = l;
  202.         while (pos != null)
  203.         {
  204.             if (pos.getValue() == n)
  205.                 count++;
  206.             pos = pos.getNext();
  207.         }
  208.         return count;
  209.     }
  210.     // איבר הכי גדול ברשימה
  211.     public static int MaxList(Node<Integer>l) {
  212.         int max = l.getValue();
  213.         while (l != null) {
  214.             if (l.getValue() > max)
  215.                 max = l.getValue();
  216.             l = l.getNext();
  217.         }
  218.         return max;
  219.     }
  220.     // מעביר רשימה למחסנית  
  221.     public static Stack<Integer> listToStuck(Node<Integer> l){
  222.         Stack<Integer> s = new Stack<Integer>();
  223.         while(l.hasNext())
  224.         {
  225.             s.push(l.getValue());
  226.             l = l.getNext();
  227.         }
  228.         return s;
  229.     }
  230.    
  231.    
  232.     // בודק אם הסדרה מסודרת בסדר עולה או יורד מסויים
  233.     public static int difference(int num1, int num2) {  
  234.         return (num1-num2);
  235.     }
  236.  
  237.    
  238.     public static boolean chain(Node<Integer>n)  {
  239.         int old_num=n.getValue();
  240.         int old_sum =difference(old_num,n.getNext().getValue());
  241.         while (n.hasNext()) {  
  242.             n=n.getNext();
  243.             int num=n.getValue();
  244.             int  sum = difference(old_num,num);
  245.             if (sum!=old_sum) return (false);
  246.             old_num=num;
  247.             old_sum = sum;}
  248.         return (true);
  249.     }
  250.    
  251.  
  252.    
  253.    
  254.     //     ------------------------------------------------------------
  255.     //                          STACK    
  256.    
  257.     //מעתיק מחסנית
  258.     public static <T> Stack<T> copyStack(Stack<T>s ) {
  259.         Stack <T> newS = new Stack<T>();
  260.         Stack <T> tmp = new Stack<T>();
  261.         while(!s.isEmpty())
  262.             tmp.push(s.pop());
  263.         while (!tmp.isEmpty())
  264.         { newS.push(tmp.top()); s.push(tmp.pop()); }
  265.         return (newS);
  266.  
  267.     }
  268.  
  269.    
  270.    
  271.     // הופך מחסנית
  272.     public static Stack<Integer> flipStack(Stack<Integer> s){
  273.         Stack<Integer> newS = new Stack<Integer>();
  274.         Stack<Integer> tmp = copyStack(s); //!
  275.         while (!tmp.isEmpty())
  276.             newS.push(tmp.pop());
  277.         return newS;
  278.     }
  279.     // בודק אם איבר קיים ברשימה
  280.     public static boolean isExistStack(Stack<Integer> s, int n)
  281.     {
  282.         Stack<Integer> sCopy = copyStack(s); //!
  283.         while (!sCopy.isEmpty())
  284.             if (sCopy.pop() == n)
  285.                 return true;
  286.         return false;
  287.     }
  288.     // בודק אם איבר קיים בצורה רקורסיבית
  289.     public static boolean isExistStackRec(Stack<Integer> s, int n)
  290.     {
  291.         if (s.isEmpty())
  292.             return false;
  293.         int x = s.pop();
  294.         boolean exists = (x == n || isExistStackRec(s, n));
  295.         s.push(x); //לאחר הקריאה הרקורסיבית, האיבר שנשלף נדחף בחזרה אל תוך המחסנית
  296.         return exists;
  297.     }
  298.     // סכום stack
  299.     public static int sumStack(Stack<Integer> s)
  300.     {
  301.         int sum = 0;
  302.         Stack<Integer> sCopy = copyStack(s); //!
  303.         while (!sCopy.isEmpty())
  304.             sum += sCopy.pop();
  305.         return sum;
  306.     }
  307.     // אורך של המחסנית
  308.     public static int lengthStack(Stack<Integer> s)
  309.     {
  310.         int length = 0;
  311.         Stack<Integer> sCopy = copyStack(s); //!
  312.         while (!sCopy.isEmpty())
  313.         {
  314.             length++;
  315.             sCopy.pop();
  316.         }
  317.         return length;
  318.     }
  319.     // חותך חלק עליון של stack
  320.     public static Stack<Integer> cutUpperStack(Stack<Integer> s){
  321.         Stack<Integer> temp = new Stack<Integer>();
  322.         Stack<Integer> newS = new Stack<Integer>();
  323.  
  324.         temp = copyStack(s);
  325.         int length = lengthStack(s);
  326.         for (int i=0; i<length/2; i++) {
  327.             newS.push(temp.pop());
  328.  
  329.         }
  330.         return newS;
  331.     }
  332.         // חותך חלק תחתון של מחסנית
  333.     public static Stack<Integer> cutLowerStack(Stack<Integer> s){
  334.         Stack<Integer> temp = new Stack<Integer>();
  335.         Stack<Integer> newS = new Stack<Integer>();
  336.  
  337.         temp = copyStack(s);
  338.         temp = flipStack(temp);
  339.         int length = lengthStack(s);
  340.         for (int i=0; i<length/2; i++) {
  341.             newS.push(temp.pop());
  342.  
  343.         }
  344.         return newS;
  345.     }
  346.     // כמה פעמים איבר מופיע במחסנית
  347.     public static int howManyStack(Stack<Integer> s, int n)
  348.     {
  349.         int count = 0;
  350.         Stack<Integer> tmp = copyStack(s); //!
  351.         while (!tmp.isEmpty())
  352.             if (tmp.pop() == n)
  353.                 count++;
  354.         return count;
  355.     }
  356.     // מוחק איבר אחרון במחסנית
  357.     public static void removeLastStack(Stack<Integer> s){
  358.         flipStack(s);
  359.         s.pop();
  360.         flipStack(s);
  361.  
  362.     }
  363.     // מוחק איבר ממחסנית
  364.     public static void removeFromStack(Stack<Integer> s, int n)
  365.     {
  366.         Stack<Integer> tmp = flipStack(s);
  367.         while (!s.isEmpty())
  368.             s.pop();
  369.         while (!tmp.isEmpty())
  370.         {
  371.             int x = tmp.pop();
  372.             if (x != n)
  373.                 s.push(x);
  374.         }
  375.     }
  376.  
  377.     public static int maxStack(Stack<Integer> s) {
  378.         //       טענת כניסה: הפעולה מקבלת מחסנית מכל סוג שניתן להשוואה // (
  379.         //       טענת יציאה: הפעולה מחזירה את האיבר הגדול ביותר, או את האיבר שמגיע אחרון במילון //
  380.         //       סיבוכיות זמן ריצה: O(n)
  381.         Stack<Integer> temp = copyStack(s);
  382.         int max = temp.top();
  383.         while (!temp.isEmpty()) {
  384.             if (temp.top() > max)
  385.                 max = temp.top();
  386.             temp.pop();
  387.         }
  388.         return max;
  389.     }
  390.  
  391.     public static void sortStack(Stack<Integer> s) {
  392.         //       טענת כניסה: הפעולה מקבלת מחסנית
  393.         //       טענת יציאה: הפעולה ממיינת את המחסנית כך שהערכים הגדולים נמצאים למטה
  394.         //        O(n^2)
  395.         Stack <Integer> s1 = new Stack<Integer>();
  396.         Stack <Integer> s2 = new Stack<Integer>();
  397.         while(!s.isEmpty()) {
  398.             if (s1.isEmpty()) s1.push(s.pop());
  399.             else {
  400.                 while(!s1.isEmpty() && s.top() < s1.top())
  401.                     s2.push(s1.pop());
  402.                 s1.push(s.pop());
  403.                 while (!s2.isEmpty()) s1.push(s2.pop());
  404.             }
  405.         }
  406.         while(!s1.isEmpty()) s.push (s1.pop());
  407.     }
  408.  
  409.     //   ------------------------------------------------------------
  410.     //                      QUEUE
  411.    
  412.  
  413.     // מעתיק תור
  414.     public static <T> Queue<T> copyQueue(Queue<T> q)
  415.     {
  416.         Queue<T> newQ = new Queue<T>();
  417.         Queue<T> tmp = new Queue<T>();
  418.         while (!q.isEmpty())
  419.         {
  420.             newQ.insert(q.head());
  421.             tmp.insert(q.remove());
  422.         }
  423.         while (!tmp.isEmpty())
  424.             q.insert(tmp.remove());
  425.         return newQ;
  426.     }
  427.     // בודק אם איבר קיים בתור    
  428.     public static boolean existInQueue(Queue<Integer> q, int n)
  429.     {
  430.         Queue<Integer> qCopy = copyQueue(q); //!
  431.         while (!qCopy.isEmpty())
  432.             if (qCopy.remove() == n)
  433.                 return true;
  434.         return false;
  435.     }
  436.     // מחזיר סכום של ה q
  437.     public static int sumQueue(Queue<Integer> q)
  438.     {
  439.         int sum = 0;
  440.         Queue<Integer> qCopy = copyQueue(q); //!
  441.         while (!qCopy.isEmpty())
  442.             sum += qCopy.remove();
  443.         return sum;
  444.     }
  445.     // מחזיר אורך תור
  446.     public static int lengthQueue(Queue<Integer> q)
  447.     {
  448.         int length = 0;
  449.         Queue<Integer> qCopy = copyQueue(q); //!
  450.         while (!qCopy.isEmpty())
  451.         {
  452.             length++;
  453.             qCopy.remove();
  454.         }
  455.         return length;
  456.     }
  457.  
  458.     // מחזיר כמה המספר מופיע בתור
  459.     public static int howManyQueue(Queue<Integer> q, int n)
  460.     {
  461.         int count = 0;
  462.         Queue<Integer> tmp = copyQueue(q); //!
  463.         while (!tmp.isEmpty())
  464.             if (tmp.remove() == n)
  465.                 count++;
  466.         return count;
  467.     }
  468.  
  469.     // מוחק מהתור איבר
  470.     public static void removeFromQueue(Queue<Integer> q, int n)
  471.     {
  472.         Queue<Integer> tmp = copyQueue(q); //!
  473.         while (!q.isEmpty())
  474.             q.remove();
  475.         while (!tmp.isEmpty())
  476.         {
  477.             int x = tmp.remove();
  478.             if (x != n)
  479.                 q.insert(x);
  480.         }
  481.     }
  482.    
  483.    
  484.    
  485.     // בודק אם התור עולה
  486.     public static boolean QGoesUp(Queue<Integer> q) {
  487.         Queue<Integer> a = copyQueue(q); // change the sign if u want it to go down
  488.         int old_q = a.remove();
  489.         while (!a.isEmpty()) {
  490.             int q1 = a.remove();
  491.  
  492.             if (old_q>q1) return (false);
  493.             old_q=q1;
  494.  
  495.         }
  496.         return (true);
  497.     }
  498.     // מחזיר את האיבר הכי גדול או קטן בטור
  499.     //1 הכי גדול
  500.     //-1 בשביל הכי קטן
  501.     /// מחזריר אינדקס
  502.     public static int getMaxOrLowQ(Queue<Integer>q,int sign) {
  503.         Queue<Integer>q1 =copyQueue(q);
  504.         int mon=q1.remove();
  505.         while (q1.isEmpty()) {
  506.             if (sign==1)mon=(Math.max(mon,q1.remove()));
  507.             if (sign==-1)mon=(Math.min(mon,q1.remove()));
  508.         }
  509.         q1 =copyQueue(q);
  510.         int c=0;
  511.         // מחזיר אינדקס
  512.         //
  513.         while (q1.isEmpty()) {
  514.             int x=q1.remove();
  515.             c=c++;
  516.             if (x==mon);
  517.                 return (c);
  518.         }
  519.         return (c);
  520.     }
  521.    
  522.    
  523.     public static void flipQueue(Queue<Integer> q) {
  524.         Stack <Integer> temp = new Stack<Integer>();
  525.         while (!q.isEmpty())
  526.             temp.push(q.remove());
  527.         while(!temp.isEmpty())
  528.             q.insert(temp.pop());
  529.     }  
  530.    
  531.     // מסדר תור לפי גודל
  532.     public static void sortQueue(Queue<Integer> q) {
  533.         Queue<Integer> temp;
  534.         temp = copyQueue(q);
  535.  
  536.         while(!q.isEmpty()) // Empty Queue
  537.             q.remove();
  538.  
  539.         while (!temp.isEmpty()) { // Insert from high to low
  540.             int max = maxQueue(temp), howMany = howManyQueue(temp, max);
  541.             for (int i=0; i<howMany; i++)
  542.                 q.insert(max);
  543.             removeFromQueue(temp, max);
  544.         }
  545.     }
  546.    
  547.     public static boolean isSorted(Queue<Integer> q) {
  548.         // Checks if the queue is sorted high --> low
  549.         Queue<Integer> a = copyQueue(q);
  550.         int old_q = a.remove();
  551.         while (!a.isEmpty()) {
  552.             int q1 = a.remove();
  553.             if (old_q>q1)
  554.                 return false;
  555.             old_q=q1;
  556.         }
  557.         return true;}
  558.    
  559.    
  560.    
  561.  
  562.     //   ------------------------------------------------------------
  563.     //                          BINNODE
  564.  
  565.  
  566.     // מעתיק עץ
  567.     public static BinNode<Integer> copyTree(BinNode<Integer> bt)
  568.     {
  569.         if (bt == null)
  570.             return null;
  571.         BinNode<Integer> left = copyTree(bt.getLeft());
  572.         BinNode<Integer> right = copyTree(bt.getRight());
  573.         return new BinNode<Integer>(left, bt.getValue(), right); //להחליף ימין ושמאל לקבלת עץ מראה
  574.     }
  575.     // קיים בעץ
  576.     public static boolean isExistTree(BinNode<Integer> bt, int n)
  577.     {
  578.         if (bt == null)
  579.             return false;
  580.         return (bt.getValue() == n || isExistTree(bt.getLeft(), n) || isExistTree(bt.getRight(), n));
  581.     }
  582.  
  583.    
  584.     // סכום עץ
  585.     public static int sumTree(BinNode<Integer> bt)
  586.     {
  587.         if (bt == null)
  588.             return 0;
  589.         return bt.getValue() + sumTree(bt.getLeft()) + sumTree(bt.getRight());
  590.     }
  591.  
  592.    
  593.     // אין בנים (עלה)
  594.     public static boolean isLeaf(BinNode<Integer> bt)
  595.     {
  596.         return (bt.getLeft() == null && bt.getRight() == null);
  597.     }
  598.     //
  599.     public static void printPreOrder(BinNode<Integer> bt)
  600.     {
  601.         if (bt != null)
  602.         {
  603.             System.out.println(bt.getValue());
  604.             printPreOrder(bt.getLeft());
  605.             printPreOrder(bt.getRight());
  606.         }
  607.     }
  608.  
  609.     public static void printInOrder(BinNode<Integer> bt)
  610.     {
  611.         if (bt != null)
  612.         {
  613.             printInOrder(bt.getLeft());
  614.             System.out.println(bt.getValue());
  615.             printInOrder(bt.getRight());
  616.         }
  617.     }
  618.    
  619.     public static void printPostOrder(BinNode<Integer> bt)
  620.     {
  621.         if (bt != null)
  622.         {
  623.             printPostOrder(bt.getLeft());
  624.             printPostOrder(bt.getRight());
  625.             System.out.println(bt.getValue());
  626.         }
  627.     }
  628.    
  629.     public static void printByLevels(BinNode<Integer> t) {
  630.         //       טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים
  631.         //       טענת יציאה: הפעולה מדפיסה את ערכי הצמתים בעץ לפי הרמה שלהם - כל צמתי העץ ברמה כלשהי יודפסו
  632.         //       אחד אחרי השני
  633.         //       סיבוכיות זמן ריצה: O(n)
  634.         Queue<BinNode<Integer>> q = new Queue<BinNode<Integer>>();
  635.         q.insert(t);
  636.         while (!q.isEmpty()) {
  637.             BinNode<Integer> current = q.remove();
  638.             System.out.println(current.getValue());
  639.             if (current.getLeft() != null)
  640.                 q.insert(current.getLeft());
  641.             if (current.getRight() != null)
  642.                 q.insert(current.getRight());
  643.         }
  644.     }
  645.  
  646.     public static void printValuesInLevel(BinNode<Integer> t, int k) {
  647.         //      טענת כניסה: הפעולה מקבלת עץ בינארי ומספר שלם k
  648.         //      טענת יציאה: הפעולה מדפיסה את הערכים בעץ ברמה k
  649.         //      סיבוכיות זמן ריצה: O(n) //
  650.         if (t==null)
  651.             System.out.println("null");
  652.         if (k==0)
  653.             System.out.print(t.getValue()+" ");
  654.         if (k>0) {
  655.             printValuesInLevel(t.getLeft(), k-1);
  656.             printValuesInLevel(t.getRight(), k-1);
  657.         }
  658.     }
  659.     // סופר כמה איברים יש
  660.     public static int countNodes(BinNode<Integer> bt)
  661.     {
  662.         if (bt == null)
  663.             return 0;
  664.         return 1 + countNodes(bt.getLeft()) + countNodes(bt.getRight());
  665.     }
  666.     // סופר עלים
  667.     public static int countLeaves(BinNode<Integer> bt)
  668.     {
  669.         if (bt == null)
  670.             return 0;
  671.         if (isLeaf(bt)) //!
  672.             return 1;
  673.         return countLeaves(bt.getLeft()) + countLeaves(bt.getRight());
  674.     }
  675.     // סופר את הגובה של העץ המקסימלי
  676.     public static int height(BinNode<Integer> bt)
  677.     {
  678.         if (bt == null)
  679.             return -1;
  680.         return 1 + Math.max(height(bt.getLeft()), height(bt.getRight()));
  681.     }
  682.  // מוצא ערך מקסימלי בעץ
  683.     public static int maxValue(BinNode<Integer> bt)
  684.     {
  685.         if (isLeaf(bt)) //!
  686.             return bt.getValue();
  687.         int maxLeft = maxValue(bt.getLeft());
  688.         int maxRight = maxValue(bt.getRight());
  689.         return Math.max(bt.getValue(), Math.max(maxLeft, maxRight));
  690.     }
  691.     // מחזיר את הערך שמעל האיבר
  692.     public static BinNode<Integer> getFather(BinNode<Integer> source, BinNode<Integer> son)
  693.     {
  694.         if (source == null)
  695.             return null;
  696.         if (source.getLeft() == son || source.getRight() == son)
  697.             return source;
  698.         BinNode<Integer> left = getFather(source.getLeft(), son);
  699.         if (left != null)
  700.             return left;
  701.         return getFather(source.getRight(), son);
  702.     }
  703.  
  704.     //מדפיס איברים בעץ
  705.     public static int howManyTree(BinNode<Integer> bt, int n)
  706.     {
  707.         if (bt == null)
  708.             return 0;
  709.         if (bt.getValue() == n)
  710.             return 1 + howManyTree(bt.getLeft(), n) + howManyTree(bt.getRight(), n);
  711.         return howManyTree(bt.getLeft(), n) + howManyTree(bt.getRight(), n);
  712.     }
  713.  
  714.     // מדפיס מספר איברים בכל רמה
  715.     public static int nodesInLevel(BinNode<Integer> bt, int level)  // Level 0 = first (root)
  716.     {
  717.         if (bt == null)
  718.             return 0;
  719.         if (level == 0)
  720.             return 1;
  721.         return nodesInLevel(bt.getLeft(), level - 1) + nodesInLevel(bt.getRight(), level - 1);
  722.     }
  723.     // מוחק איבר מעץ
  724.     public static BinNode <Integer> RemoveFromBin(BinNode<Integer> p, int num)
  725.     {
  726.         BinNode <Integer>temp;
  727.         while (p !=null && p.hasRight()) {
  728.             if (num==p.getRight().getValue()){
  729.                 temp=p.getRight().getRight();
  730.                 p.setRight(temp);
  731.                 temp=null;
  732.             }
  733.             else p=p.getRight();
  734.         }
  735.         return p;
  736.     }
  737.  
  738.    
  739.     public static boolean isBST(BinNode<Integer> bt)
  740.     {
  741.         if (bt == null)
  742.             return true;
  743.         if (bt.getLeft() != null && bt.getLeft().getValue() >= bt.getValue())
  744.             return false;
  745.         if (bt.getRight() != null && bt.getRight().getValue() < bt.getValue())
  746.             return false;
  747.         return isBST(bt.getLeft()) && isBST(bt.getRight());
  748.     }
  749.     // טענת כניסה: הפעולה מקבלת עץ בינארי של מספרים שלמים ומספר שלם
  750.     // טענת יציאה: הפעולה מוסיפה את המספר שהתקבל לעץ החיפוש הבינארי
  751.     public static void addToBST(BinNode<Integer> bt, int n)
  752.     {
  753.         if (bt.getLeft() == null && n < bt.getValue())
  754.             bt.setLeft(new BinNode<Integer>(n));
  755.         else if (bt.getRight() == null && n >= bt.getValue())
  756.             bt.setRight(new BinNode<Integer>(n));
  757.         else if (n < bt.getValue())
  758.             addToBST(bt.getLeft(), n);
  759.         else addToBST(bt.getRight(), n);
  760.     }
  761.  
  762. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement