אתם מתרגלים שאלה מתוך בגרות מדעי המחשב — מבני נתונים (שאלון 899271)מבחן 2017 · קיץ מועד א · שאלה 6כל שאלות המבחן ←
עצים›עצים בינאריים

לפניך הפעולה check(t1, t2) ב-Java. הפעולה מקבלת שני עצים בינריים לא ריקים מטיפוס שלם, t1 ו-t2, ומחזירה רשימה המכילה את כל המספרים הנמצאים בעץ t1 ואינם נמצאים בעץ t2. הפעולה מזמנת פעולה נוספת המקבלת שלושה פרמטרים.

public class BinNode<T> {
    private T value;
    private BinNode<T> left;
    private BinNode<T> right;
    public BinNode(T value) { this.value = value; this.left = null; this.right = null; }
    public T getValue() { return value; }
    public void setValue(T value) { this.value = value; }
    public BinNode<T> getLeft() { return left; }
    public void setLeft(BinNode<T> left) { this.left = left; }
    public BinNode<T> getRight() { return right; }
    public void setRight(BinNode<T> right) { this.right = right; }
}

public class Node<T> {
    private T value;
    private Node<T> next;
    public Node(T value) { this.value = value; this.next = null; }
    public T getValue() { return value; }
    public void setValue(T value) { this.value = value; }
    public Node<T> getNext() { return next; }
    public void setNext(Node<T> next) { this.next = next; }
}

// הפעולה check(t1,t2) הבאה נתונה (אינה חלק מהמימוש):
public static Node<Integer> check(BinNode<Integer> t1, BinNode<Integer> t2) {
    Node<Integer> first = new Node<Integer>(-1);
    first = check(t1, t2, first);
    return first.getNext();
}
סעיף א

ממש פעולה חיצונית exist. הפעולה מקבלת עץ בינרי t וערך x, ומחזירה true אם x קיים בעץ, אחרת false.

public static boolean exist (BinNode<Integer> t, int x)

סעיף ב

לפניך הפעולה check(t1, t2) (הפעולה מקבלת שני עצים בינריים לא ריקים t1 ו-t2, ומחזירה רשימה חדשה (Node<Integer>) המכילה את כל המספרים הנמצאים בעץ t1 ואינם נמצאים בעץ t2). הפעולה נעזרת בפעולה נוספת בעלת שלושה פרמטרים. ממש את הפעולה: public static Node<Integer> check(BinNode<Integer> t1, BinNode<Integer> t2, Node<Integer> list). אתה יכול להשתמש בפעולה שמימשת בסעיף א.

public static Node<Integer> check (BinNode<Integer> t1, BinNode<Integer> t2, Node<Integer> list)

סעיף ג

מהי סיבוכיות זמן הריצה של הפעולה שמימשת בסעיף ב? נמק את תשובתך.

שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי