You are on page 1of 2

qforAnswer2.

java 15:09 ,2023 ‫ במאי‬30 ,‫יום שלישי‬

1 package binLists;
2
3 public class qforAnswer2 {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 }
8 public static int widthLevel(BinNode<Integer> t)
9 {
10 // ‫הפעולה מקבלת עץ בינארי לא ריק‬
11 // ‫הפעולה מחזירה את רמת רוחב העץ‬
12 // ‫רוחב העץ מוגדר הינו הרמה הנמוכה ביותר הכילה את מספר הצמתים הגדול ביותר‬
13
14 int nol =findHeight(t),max; // numOfLevel
15 BinNode<Integer> bt = t;
16 //Queue<BinNode<Integer>> c1 =new Queue<BinNode<>>();
17 //Queue<BinNode<T>> c2 =new Queue<BinNode<T>>();
18 int wl =0; //widthLevel
19 while(nol>-1)
20 {
21 max = level(t,nol);
22 if(max>wl)
23 wl= max;
24 nol--;
25 }
26 return wl;
27
28 }
29 public static int level(BinNode<Integer> bt , int num)
30 {
31 // ‫הפעולה מקבלת עץ ומספר שלם‬
32 // ‫הפעולה תחזיר את מספר הצמתים הנמצאים באותה הרמה של המספר המתקבל‬
33 if(bt ==null)
34 return 0;
35
36 if(num == 0)
37 {
38 return 1;
39 }
40 return level(bt.getLeft(),num-1)+ level(bt.getRight(),num-1);
41 }
42
43 public static <T> int numOfSons(BinNode<T> t)
44 {
45 // ‫הפעולה מקבלת עץ‬
46 // ‫הפעולה בודקת כמה בנים יש לחוליה בעץ‬
47 int num =0;
48 if(t.hasRight())
49 num++;
50 if(t.hasRight())
51 num++;
52 return num;
53 }
54
55 public static int findHeight(BinNode<Integer> bt)
56 {
57 // ‫הפעולה מקבלת עץ‬
58 // 0-‫הפעולה מחזירה את גובה העץ החל מ‬
59 int max;
60 if(bt==null)
61 {
62 System.out.println("Tree is empty");
63 return 0;
64 }else

Page 1
qforAnswer2.java 15:09 ,2023 ‫ במאי‬30 ,‫יום שלישי‬

65 {
66 int leftHeight =-1 , rightHeight = -1;
67 if(bt.getRight()!=null)
68 rightHeight = findHeight(bt.getRight());
69 if(bt.getLeft()!=null)
70 leftHeight = findHeight(bt.getLeft());
71 if(leftHeight>rightHeight)
72 max = leftHeight;
73 else
74 max=rightHeight;
75 return (max+1);
76 }
77
78 }
79
80 }
81

Page 2

You might also like