Bin Tree NodeInteger bt 1 new Bin Tree
בניית עץ חוליות בינרי Bin. Tree. Node<Integer> bt 1 = new Bin. Tree. Node<Integer>(3); bt 1. set. Left(new Bin. Tree. Node<Integer>(5)); bt 1. set. Right(new Bin. Tree. Node<Integer>(7)); bt 1. get. Left(). set. Left(new Bin. Tree. Node<Integer>(2)); bt 1 Bin. Tree. Node<Integer> left info right null 3 null Bin. Tree. Node<Integer> 12 left info right null 2 null Bin. Tree. Node<Integer> left info right null 5 null 7 null " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
תנועה על עץ חוליות בינרי temp Bin. Tree. Node<Integer> temp = bt 1; temp=bt 1. get. Left(); temp=temp. get. Left(); temp 13 Bin. Tree. Node<Integer> left info right null 2 null info right 3 Bin. Tree. Node<Integer> temp null bt 1 Bin. Tree. Node<Integer> left info right null 5 null 7 null " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
שינוים על עץ חוליות בינרי x=3 y=5 z=2 int x=bt 1. get. Info(); int y=bt 1. get. Left(). get. Info(); int z=bt 1. get. Left(). get. Info(); bt 1. set. Info(x+y+z); bt 1 Bin. Tree. Node<Integer> left 14 left info right null 2 null right 10 3 Bin. Tree. Node<Integer> info Bin. Tree. Node<Integer> left info right null 5 null 7 null " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
שינוי ערכים ושינוי מבנה Bin. Tree. Node<Integer> temp = bt 1. get. Left(); bt 1 Bin. Tree. Node<Integer> bt 1. set. Left (bt 1. get. Right()); left info right bt 1. set. Right (temp); null 3 null temp ? האם שני קטעים שקולים Bin. Tree. Node<Integer> left info right null 5 null 7 null int temp=bt 1. get. Left(). get. Info(); bt 1. get. Left(). set. Info(bt 1. get. Right(). get. Info()); bt 1. get. Right(). set. Info(temp); 15 Bin. Tree. Node<Integer> " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
הכנסה : הכנסה בקצה העץ Bin. Tree. Node<Integer> node = new Bin. Tree. Node<Integer>(15); Bin. Tree. Node<Integer> pos = bt 1. get. Right(); bt 1 pos. set. Left (node); Bin. Tree. Node<Integer> left info right 3 null info right null 5 null node 16 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר pos Bin. Tree. Node<Integer> left info right null 7 Bin. Tree. Node<Integer> left n Bin. Tree. Node<Integer> left info right null 15 null
הכנסה : הכנסה בקצה העץ n Bin. Tree. Node<Integer> node = new Bin. Tree. Node<Integer>(15); Bin. Tree. Node<Integer> pos = bt 1. get. Right(); bt 1 pos. set. Left (node); Bin. Tree. Node<Integer> left info right 3 null Bin. Tree. Node<Integer> node = new Bin. Tree. Node<Integer>(15); bt 1. set. Right(node); Bin. Tree. Node<Integer> left info right null 7 Bin. Tree. Node<Integer> left info right null 5 null node 17 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר Bin. Tree. Node<Integer> left info right null 15 null
הוצאה bt 1 ? הוצאת עץ עלה ? הוצאת תת עץ במלואו ? הוצאת צומת בודד Bin. Tree. Node<Integer> left info right 3 null Bin. Tree. Node<Integer> left info right null 7 Bin. Tree. Node<Integer> left info right null 5 null Bin. Tree. Node<Integer> 18 n n n למשל כיצד מוציאים את ? הצומת הזה Bin. Tree. Node<Integer> left info right null 15 null 7 null " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
שאלה public static int num. Nodes (Bin. Tree. Node<Integer> bt) { if (bt == null) return 0; return 1+ num. Nodes(bt. get. Left()) + num. Nodes(bt. get. Right()); } ? 0 תחזיר num. Nodes(…) האם ייתכן כי הפעולה !! לא 23 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
הדפסת ערכי העץ public static void print. Nodes (Bin. Tree. Node<Integer> bt) סריקה { !! תחילית if (bt != null) { System. out. print (bt. get. Info() + " "); print. Nodes (bt. get. Left()); print. Nodes (bt. get. Right()); } } 25 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
מחרוזת המתארת את העץ public static String preorder. String (Bin. Tree. Node<String> bt) { if (bt == null) return ""; return bt. get. Info() + " " + preorder. String (bt. get. Left()) + preorder. String (bt. get. Right()); } 29 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
מחרוזת המתארת את העץ public static String inorder. String (Bin. Tree. Node<String> bt) { if (bt == null) return ""; return inorder. String (bt. get. Left()) + bt. get. Info() + " " + inorder. String (bt. get. Right()); } public static String postorder. String (Bin. Tree. Node<String> bt) { if (bt == null) return ""; return postorder. String (bt. get. Left()) + postorder. String (bt. get. Right() + bt. get. Info() + " " ); } 30 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
בינרי - חיפוש - איתור ערך בעץ public static boolean exists. In. BST (Bin. Tree. Node<Integer> bst, int x) { if (bst == null) הפרמטר הוא return false; בינרי - חיפוש - עץ if (bst. get. Info() == x) return true; if (bst. get. Info() < x) return exists. In. BST(bst. get. Left(), x); return exists. In. BST (bst. get. Right(), x); } 33 " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
שימוש בספריית עזר import unit 4. collections. Lib. *; import unit 4. utils. Lib. *; class test{ public static int num. Leaves (Bin. Tree. Node<Integer> bt) { if (bt == null) return 0; if (bt. get. Left()==null && bt. get. Right()==null) return 1; return num. Leaves(bt. get. Left()) + num. Leaves(bt. get. Right()); } public static void main(String [] args){ Bin. Tree. Node<Integer> t= Bin. Tree. Utils. build. Random. Tree)8, 1, 10); Bin. Tree. Utils. show. Tree)t, "Random. Tree"); System. out. println("Num of Leaves="+ num. Leaves(t)); } } " "עיצוב תוכנה מבוסס עצמים - השתלמות ב 2008 – תשמ"ח שבע - באר
- Slides: 41