Binary Tree public class Binary Tree public String
Binary. Tree public class Binary. Tree { public String in. Order() { if (is. Empty()) return ""; else return root. in. Order(); } protected Binary. Node root; public Binary. Tree() { root = null; } public String pre. Order() { if (is. Empty()) return ""; else return root. pre. Order(); } public boolean is. Empty() { return root == null; } public void insert(Object to. Add) { if (is. Empty()) root = new Binary. Node(to. Add); else root. insert(to. Add); } public String post. Order() { if (is. Empty()) return ""; else return root. post. Order(); } } 11
Binary. Node public class Binary. Node { protected Object data; protected Binary. Node left; protected Binary. Node right; public Binary. Node(Object data) { this. data = data; left = null; right = null; } … } public void insert(Object to. Add) { double select = Math. random(); if (select > 0. 5) { if (left == null) left = new Binary. Node(to. Add); else left. insert(to. Add); } else { if (right == null) right = new Binary. Node(to. Add); else right. insert(to. Add); } } 12
Binary. Node public String in. Order() { String res = ""; if (left != null) res = res + left. in. Order(); res = res + " " + data + " "; if (right != null) res = res + right. in. Order(); return res; } public String pre. Order() { String res = data + " "; if (left != null) res = res + left. pre. Order(); if (right != null) res = res + right. pre. Order(); return res; } public String post. Order() { String res = ""; if (left != null) res = res + left. post. Order(); if (right != null) res = res + right. post. Order(); res = res + " " + data + " "; return res; } 13
: דוגמה לשיטות של עצים חישוב גובה בעץ בינארי כללי In Binary. Tree public int height() { if (is. Empty()) return -1; else return root. height(); } In Binary. Node public int height() { int res. Left = -1; int res. Right = -1; if (left != null) { res. Left = left. height(); } if (right != null) { res. Right = right. height(); } return Math. max(res. Left, res. Right) + 1; } 14
: מימוש BST - עץ חיפוש בינארי Binary. Tree Binary. Node BST BSN 15
BST import java. util. Comparator; public class BST extends Binary. Tree{ public class BSN extends Binary. Node { private Comparator comp; public BSN(Object data, Comparator comp) { super(data); this. comp = comp; } public BST(Comparator comp) { super(); this. comp = comp; } … // (override insert, remove, etc. ) … // (override insert only) } } 16
Step 7 1 2 3 4 5 6 import java. util. Comparator; public class Main { 50 public static void main(String[] args) { Comparator comp = new Integer. Comparator(); 40 BST tree 1 = new BST(comp); 60 tree 1. insert(new Integer(50)); // Step 1 tree 1. insert(new Integer(60)); // Step 2 30 45 65 tree 1. insert(new Integer(40)); // Step 3 tree 1. insert(new Integer(30)); // Step 4 tree 1. insert(new Integer(20)); // Step 5 20 tree 1. insert(new Integer(45)); // Step 6 tree 1. insert(new Integer(65)); // Step 7 System. out. println("In. Order: " + tree 1. in. Order()); System. out. println("Pre. Order: " + tree 1. pre. Order()); System. out. println("Post. Order: " + tree 1. post. Order()); System. out. println("Find Minimum: " + tree 1. find. Min()); System. out. println("Height: " + tree 1. height()); } } 17
Integer. Comparator public class Integer. Comparator implements Comparator { public int compare(Object o 1, Object o 2) { Integer i 1 = (Integer) o 1; Integer i 2 = (Integer) o 2; if (i 1. int. Value() > i 2. int. Value()) return 1; else if (i 1. int. Value() == i 2. int. Value()) return 0; else return -1; } } 18
Character. Comparator public class Character. Comparator implements Comparator { public int compare(Object o 1, Object o 2) { Character c 1 = (Character) o 1; Character c 2 = (Character) o 2; if (c 1. char. Value() > c 2. char. Value()) return 1; else if (c 1. char. Value() == c 2. char. Value()) return 0; else return -1; } } 19
BST Insert: Example: Insert C F B H A D K C 20
הכנסת איבר חדש לעץ In BST public void insert(Object to. Add) { if (is. Empty()) { root = new BSN(to. Add, this. comp); } else { root. insert(to. Add); } } In BSN public void insert(Object to. Add) { if (compare(to. Add, this. data) <= 0) { if (left == null) left = new BSN(to. Add, this. comp); else left. insert(to. Add); } if (compare(to. Add, this. data) > 0) { if (right == null) right = new BSN(to. Add, this. comp); else right. insert(to. Add); } } 21
מציאת קודקוד בעל מפתח מינימאלי בעץ חיפוש בינארי In BST public Object find. Min() { if (is. Empty()) { return null; // Exceptions are needed. . . } return ((BSN)root). find. Min(); } In BSN public Object find. Min() { Binary. Node t = this; while (t. left != null) t = t. left; return t. data; } מה היינו צריכים לעשות אם העץ ? BST לא היה 22
" בדיקה האם עץ בינארי הוא "מושלם : Binary. Tree במחלקה public boolean is. Perfect() { return root. is. Perfect(); } : Binary. Node במחלקה עץ בינארי מושלם public boolean is. Perfect() { int h = height(); // class method if (h==0) return true; if (h==1) return left != null && right != null; return left != null && left. height() == h - 1 && left. is. Perfect() && right != null && right. height() == h - 1 && right. is. Perfect()); } 27
: Binary. Node במחלקה public boolean is. Complete() { int h = height(); // class method if (h==0) return true; if (h==1) return left != null; // the height is at least 2: boolean has 2 Sons = left != null && right != null; boolean case 1 = false, case 2 = false; if (has 2 Sons) { int left. H = left. height(); int right. H = right. height(); case 1 = left. H == h-1 && left. is. Complete() && right. H == h-2 && right. is. Perfect(); case 2 = left. H == h-1 && left. is. Perfect () && right. H == h-1 && right. is. Complete(); } return case 1 || case 2; } 31
public class Heap { private Heap. Node m. Root; public Linked. List get. Keys. In. Range(Comparable c. First, Comparable c. Last) { // Complete here } } public class Heap. Node{ private Comparable m. Key; private Heap. Node m. Left, m. Right; public void get. Keys. In. Range(Comparable c. First, Comparable c. Last, Linked. List l. Keys ) { // Complete here } } 34
In Heap class: public Linked. List get. Keys. In. Range(Comparable c. First, Comparable c. Last ) { Linked. List l. Keys = new Linked. List(); if (m. Root != null) m. Root. get. Keys. In. Range(c. First, c. Last, l. Keys); return l. Keys; } 35
In Heap. Node class: public void get. Keys. In. Range(Comparable c. First, Comparable c. Last, Linked. List l. Keys ) { if (m. Key. compare. To(c. First) >= 0 ) { if (m. Key. compare. To(c. Last) <= 0) l. Keys. add(m. Key); if (m. Left != null) m. Left. get. Keys. In. Range(c. First, c. Last, l. Keys); if (m. Right != null) m. Right. get. Keys. In. Range(c. First, c. Last, l. Keys); } } 36
The End! 37
- Slides: 37