Transcript פתרון

Slide 1

‫תרגול ‪5‬‬

‫‪ - Trees, BSTs‬עצים ועצי חיפוש בינאריים‬


Slide 2

‫ עץ‬- Tree
ADT that stores elements hierarchically.
With the exception of the root, each node in the tree has a parent and zero or more
children nodes.
Access methods in the tree ADT:
root() – returns the root of the tree
parent(node v) – returns the parent of node v
children(node v) – returns an iterator of the
children of node v.
depth(node v)
The depth of node v = the number of ancestors of v
• depth(root) = 0
• depth(v ≠ root) = depth(v.parent) + 1
height(node v)
The maximum height of a node v in T
• height(v) = 0 , if v is a leaf
• height(v) = 1 + maximum-height of a child of v, if v is not a leaf.
height()
The height of the root of T


Slide 3

‫ – עץ בינארי‬Binary Tree
• Each node has at most two children, left-son and right-son.
• Full Binary Tree – A binary tree in which each node has
exactly zero or two children
– A full binary tree with n leaves has n-1 inner nodes.

• Complete Binary Tree - A binary tree in which every level,
except possibly the deepest, is completely filled. At depth
n, the height of the tree, all nodes must be as far left as
possible.
– A complete binary tree of height h has between 2h and 2h+1-1
nodes.
– The height of a complete binary tree with n nodes is log 𝑛


Slide 4

k-Tree
• Each node has at most k children.
• Representation option: “left child - right
sibling”,
– each node has the following pointers:
• parent
• left child - a pointer to the left most child.
• right-sibling - a pointer to the next sibling on the
right.


Slide 5

‫תרגיל ‪1‬‬
‫• לעץ בינארי ‪ T‬נגדיר‬
‫– )‪ – L(T‬מספר העלים בעץ ‪T‬‬
‫– )‪ – D2(T‬מספר הקודקודים ב‪ T‬עם דרגה ‪ 2( 2‬ילדים בדיוק)‬

‫הוכיחו שבכל עץ בינארי ‪ T‬עם ‪ n‬קודקודים מתקיים‬
‫‪D2(T)=L(T)-1‬‬
‫הערה‪ :‬בעץ בינארי מלא )‪ (Full‬מספר העלים הוא מספר‬
‫הקודקודים הפנימיים ‪1 +‬‬
‫פתרון‪:‬‬
‫הוכחה באינדוקציה על מספר הקודקודים בעץ‪ .‬מקרה‬
‫בסיס‪D2(T)=0 ,L(T)=1 ,n=1 :‬‬


Slide 6

‫תרגיל ‪1‬‬
‫הוכחה באינדוקציה על מספר הקודקודים בעץ‪.‬‬
‫• צעד האינדוקציה‪ :‬נניח שהטענה נכונה לכל ‪k‫• נסתכל על השורש בעץ עם ‪ n‬קודקודים‬
‫– ‪ 2‬מקרים אפשריים‪:‬‬

‫מקרה ראשון‪ :‬דרגת השורש היא ‪.1‬‬
‫‪ .1‬ל ‪ T‬ול ‪ T1‬יש אותו מספר עלים‬
‫‪ .2‬ל ‪ T‬ול ‪ T1‬יש אותו מספר קודקודים עם דרגה ‪2‬‬
‫‪𝐷2(𝑇) = 𝐷2(𝑇1) = 𝐿(𝑇1) – 1 = 𝐿(𝑇) – 1‬‬

‫‪T1‬‬
‫‪1‬‬

‫הנחת‬
‫האינדוקציה‬

‫‪2‬‬


Slide 7

‫תרגיל ‪1‬‬
‫הוכחה באינדוקציה על מספר הקודקודים בעץ‪.‬‬
‫• צעד האינדוקציה‪ :‬נניח שהטענה נכונה לכל ‪k‫• נסתכל על השורש בעץ עם ‪ n‬קודקודים‬
‫– ‪ 2‬מקרים אפשריים‪:‬‬

‫מקרה שני‪ :‬דרגת השורש היא ‪.2‬‬
‫‪ .1‬מספר העלים ב ‪ T‬הוא סכום העלים ב ‪ T1‬וב ‪T2‬‬
‫‪ .2‬מספר הקודקים עם דרגה ‪ 2‬ב ‪ T‬הוא סכום הקודקודים‬
‫עם דרגה ‪ 2‬ב ‪ T1‬וב ‪ T2‬ועוד קודקוד ‪( 1‬השורש)‬

‫‪T2‬‬

‫‪T1‬‬

‫ע "פ ‪2‬‬

‫‪= 𝐿(𝑇) – 1‬‬

‫ע "פ ‪1‬‬

‫‪𝐷 2 𝑇 = 𝐷 2 𝑇1 + 𝐷 2 𝑇2 + 1‬‬
‫‪= 𝐿 𝑇1 − 1 + 𝐿 𝑇2 − 1 + 1‬‬

‫ע"פ הנחת‬
‫האינדוקציה‬


Slide 8

‫תרגיל ‪2‬‬
‫• ‪ T‬עץ בינארי עם ‪ n‬קודקודים‪ .‬לכל קודקוד ‪ x‬יש את השדות‬
‫הבאים‪:‬‬
‫–‬
‫–‬
‫–‬
‫–‬

‫‪ – x.key‬מספר טבעי‬
‫‪ – x.left‬מצביע לבן השמאלי‬
‫‪ – x.right‬מצביע לבן הימני‬
‫‪ – x.val‬מספר טבעי לשימוש כללי (אינו בשימוש ב‪ T‬במקור)‬

‫• נגדיר את )‪ maxPath(T‬כסכום המקסימלי של מפתחות‬
‫במסלול (פשוט) מהשורש לעלה כלשהו‪.‬‬
‫• נגדיר )‪ Path(T‬כמסלול כלשהו עם סכום זה‪.‬‬
‫• דוגמא‪:‬‬
‫‪maxPath(T)=23‬‬
‫‪Path(T) = left, left‬‬


Slide 9

‫תרגיל ‪2‬‬
‫• ‪ T‬עץ בינארי עם ‪ n‬קודקודים‪ .‬לכל קודקוד ‪ x‬יש את השדות‬
‫הבאים‪:‬‬
‫–‬
‫–‬
‫–‬
‫–‬

‫‪ – x.key‬מספר טבעי‬
‫‪ – x.left‬מצביע לבן השמאלי‬
‫‪ – x.right‬מצביע לבן הימני‬
‫‪ – x.val‬מספר טבעי לשימוש כללי (אינו בשימוש ב‪ T‬במקור)‬

‫• נגדיר את )‪ maxPath(T‬כסכום המקסימלי של מפתחות‬
‫במסלול (פשוט) מהשורש לעלה כלשהו‪.‬‬
‫• נגדיר )‪ Path(T‬כמסלול כלשהו עם סכום זה‪.‬‬
‫‪ (a‬תארו אלגוריתם למציאת הערך‬
‫)‪ maxPath(T‬בזמן )‪O(n‬‬
‫‪ (b‬כיצד ניתן לשנות את האלגוריתם כך‬
‫שגם ידפיס את המסלול המקסימלי?‬


Slide 10

‫תרגיל ‪2‬‬
‫‪ (a‬תארו אלגוריתם למציאת הערך )‪ maxPath(T‬בזמן )‪O(n‬‬
‫‪ (b‬כיצד ניתן לשנות את האלגוריתם כך שגם ידפיס את המסלול‬
‫המקסימלי?‬
‫פתרון‪:‬‬
‫‪ (a‬מחשבים רקורסיבית את ‪ maxPath‬של הבן השמאלי וכן את‬
‫‪ maxPath‬של הבן הימני‪ ,‬ומוסיפים את המפתח של הקודקוד‬
‫למקסימלי מביניהם‬
‫{) ‪maxPath( T‬‬
‫)‪if (T == null‬‬
‫‪return 0‬‬
‫))‪lmax = maxPath(left-sub-tree(T‬‬
‫))‪rmax = maxPath(right-sub-tree(T‬‬
‫)‪m = Max(lmax, rmax‬‬
‫‪return m + T.key‬‬
‫}‬


Slide 11

2 ‫תרגיל‬
O(n) ‫ בזמן‬maxPath(T) ‫( תארו אלגוריתם למציאת הערך‬a
‫( כיצד ניתן לשנות את האלגוריתם כך שגם ידפיס את המסלול‬b
?‫המקסימלי‬
:‫פתרון‬
‫ אם‬0‫ ל‬x.val ‫ רק שנעדכן את‬,‫ כמו קודם‬maxPath ‫( נחשב את‬b
.‫ אם מהשמאלי‬1‫עץ השמאלי ו‬-‫הסכום המקסימלי בא מתת‬
‫ להחליט לאיזה כיוון‬x.val ‫בהדפסת המסלול נשתמש בערך‬
.‫להמשיך‬
maxPath( T ){
if (T == null)
return 0;
lmax = maxPath(left-sub-tree(T))
rmax = maxPath(right-sub-tree(T))
m = Max(lmax, rmax)
if (m == lmax)
T.val = 0
else
T.val = 1
return m + T.key
}

PrintPath(T){
While (T.left ≠ null or T.right ≠ null )
if (T.val == 0)
print("left ")
T = T.left
else
print("right ")
T = T.right
}


Slide 12

‫תרגיל ‪2‬‬
‫‪ (a‬תארו אלגוריתם למציאת הערך )‪ maxPath(T‬בזמן )‪O(n‬‬
‫‪ (b‬כיצד ניתן לשנות את האלגוריתם כך שגם ידפיס את המסלול‬
‫המקסימלי?‬
‫ניתוח זמן‪:‬‬
‫‪ (a‬מבקרים בכל קודקוד פעם אחת‪ ,‬לכן זמן הריצה )‪O(n‬‬
‫‪ (b‬מבקרים פעם אחת בכל קודקוד במסלול‪ ,‬ומספר הקודקודים‬
‫במסלול חסום ע"י ‪ .n‬לכן סה"כ )‪O(n‬‬


Slide 13

‫סריקות בעץ בינארי‬
• Inorder – inorder on the left sub tree, visit the
current node and finally, inorder on the right
sub tree.
15

Rec. call on left child

Write current node
Rec. call on
right child
Return

7

8
3

12

Inorder: 3, 8, 12, 15, 7, 13

13


Slide 14

‫סריקות בעץ בינארי‬
• Preorder – visit the current node, preorder on
its left sub tree and finally, preorder on its
right sub tree.
15

Write current node
Rec. call on left child
Rec. call on
right child
Return

7

8
3

12

Preorder: 15, 8, 3, 12, 7, 13

13


Slide 15

‫סריקות בעץ בינארי‬
• Postorder - postorder on the left sub tree,
postorder on the right sub tree and finally,
visit the current node.
15

Rec. call on left child
Rec. call on right child

7

8

Write current node
and Return
3

12

Postorder: 3, 12, 8, 13, 7, 15

13


Slide 16

3 ‫תרגיל‬
Preorder‫ מסריקות ה‬T ‫שחזרו את העץ הבינארי‬
:‫ הנתונות שלו‬Inorder ‫ו‬
Inorder: c,d,b,a,h,g,j,e,f.

Preorder: a,b,c,d,e,g,h,j,f.


Slide 17

‫תרגיל ‪3‬‬
‫שחזרו את העץ הבינארי ‪ T‬מסריקות ה‪Preorder‬‬
‫ו ‪ Inorder‬הנתונות שלו‪:‬‬
‫‪Preorder: a,b,c,d,e,g,h,j,f.‬‬
‫שורש העץ‬

‫‪Inorder: c,d,b,a,h,g,j,e,f.‬‬
‫אחרי השורש –‬
‫תת‪-‬עץ ימני‬

‫לפני השורש –‬
‫תת‪-‬עץ שמאלי‬

‫‪a‬‬
‫‪h,g,j,e,f‬‬

‫‪c,d,b‬‬


Slide 18

3 ‫תרגיל‬
Preorder‫ מסריקות ה‬T ‫שחזרו את העץ הבינארי‬
:‫ הנתונות שלו‬Inorder ‫ו‬
Inorder: c,d,b,a,h,g,j,e,f.

Preorder: a,b,c,d,e,g,h,j,f.

Inorder ‫ ו‬Preorder ‫ אנחנו יודעים את ההליכות‬b,c,d ‫עץ‬-‫לתת‬
:T‫כיוון שהן ההליכות המושרות מ‬

a
c,d,b

Inorder: c,d,b.

h,g,j,e,f

Preorder: b,c,d.


Slide 19

3 ‫תרגיל‬
Preorder‫ מסריקות ה‬T ‫שחזרו את העץ הבינארי‬
:‫ הנתונות שלו‬Inorder ‫ו‬
Inorder: c,d,b,a,h,g,j,e,f.

Preorder: a,b,c,d,e,g,h,j,f.

Inorder ‫ ו‬Preorder ‫ אנחנו יודעים את ההליכות‬b,c,d ‫עץ‬-‫לתת‬
:T‫כיוון שהן ההליכות המושרות מ‬
a

a

bb

c,d,b
c,dc
d

Inorder: c,d,b.
h,g,j,e,f
h,g,j,e,f

h,g,j,e,f

Preorder: b,c,d.


Slide 20

3 ‫תרגיל‬
Preorder‫ מסריקות ה‬T ‫שחזרו את העץ הבינארי‬
:‫ הנתונות שלו‬Inorder ‫ו‬
Inorder: c,d,b,a,h,g,j,e,f.
a

Inorder: h,g,j,e,f. Preorder: e,g,h,j,f.
h,g,j,e,f
ee

bbb

h,g,j
g

ccc
d

dd

Preorder: a,b,c,d,e,g,h,j,f.

h

ff

j


Slide 21

‫תרגיל ‪3‬‬
‫שחזרו את העץ הבינארי ‪ T‬מסריקות ה‪Preorder‬‬
‫ו ‪ Inorder‬הנתונות שלו‪:‬‬
‫‪Preorder: a,b,c,d,e,g,h,j,f.‬‬

‫‪Inorder: c,d,b,a,h,g,j,e,f.‬‬

‫טענה‪ :‬משתי סריקות שונות בעץ ‪ ,T‬כאשר אחת מהן היא‬
‫‪ ,Inorder‬ניתן לשחזר את העץ‪.‬‬
‫אחת הדרכים להוכיח טענה זו היא ע"י שימוש בתהליך בו‬
‫השתמשנו לפתרון התרגיל‪.‬‬


Slide 22

‫תרגיל ‪4‬‬
‫• האם תמיד ניתן לשחזר עץ בינארי מסריקות‬
‫ה‪ Preorder‬וה‪ Postorder‬שלו?‬
‫– הוכיחו את הטענה או תנו דוגמא נגדית‬

‫פתרון‪ :‬לא תמיד‬
‫דוגמא‪Postorder: BA Preorder: AB :‬‬
‫ישנן שתי אפשרויות לעץ‪:‬‬


Slide 23

‫ – עץ חיפוש בינארי‬Binary Search Tree
BST
property

In a BST T, for any node x in T:
1. If y is a node in the left sub-tree of x then
key(y) < key(x).
2. if y is a node in the right sub-tree of x then
key(y) ≥ key(x).

Minimal
key

The key of the leftmost node.

Maximal
key

The key of the rightmost node.


Slide 24

‫ – עץ חיפוש בינארי‬Binary Search Tree
Predecessor x is a node in a BST T.
The predecessor of x is the node preceding x in an in-order traversal on T.
 If x has left son  the maximum in the left sub-tree of x.
 Otherwise, predecessor is the lowest ancestor of x whose
right child is also ancestor of x.
Successor

x is a node in a BST T.
The successor of x is the node which succeeds x in an in-order traversal on T.
 If x has right son  the minimum in the right sub-tree of x.
 Otherwise, successor is the lowest ancestor of x whose left
child is also ancestor of x.


Slide 25

‫ – עץ חיפוש בינארי‬Binary Search Tree
Delete

Delete node x from a BST T:
 If x is a leaf and the left child of his parent then x.parent.left ← null
 If x is a leaf and the right child of his parent then x.parent.right ← null
 If x has only one child, update x.parent to point to x's child instead of x.
update the parent of the child of x to be the former parent of x
 If x has 2 children, replace x with node y, its successor (y has no left
child) Note that y might have a right child. If so, then apply the same
process of deleting a node with only one child to y (see previous section).


Slide 26

‫‪ – Binary Search Tree‬עץ חיפוש בינארי‬
‫הדגמת מחיקות מעץ חיפוש בינארי‪:‬‬
‫‪15‬‬

‫‪20‬‬

‫‪8‬‬
‫‪3‬‬

‫‪12‬‬

‫‪23‬‬

‫‪10‬‬

‫‪13‬‬
‫‪11‬‬


Slide 27

‫‪ – Binary Search Tree‬עץ חיפוש בינארי‬
‫הדגמת מחיקות מעץ חיפוש בינארי‪:‬‬
‫‪15‬‬

‫‪20‬‬

‫‪8‬‬
‫‪3‬‬

‫‪12‬‬

‫‪23‬‬

‫‪10‬‬

‫‪13‬‬
‫‪11‬‬


Slide 28

‫תרגיל ‪5‬‬
‫נתון ‪ T BST‬בגודל ‪ n‬בו כל המפתחות שונים זה מזה‪ ,‬בו לכל‬
‫קודקוד ‪ x‬יש שדה נוסף של ‪ – x.size‬מספר המפתחות של תת‪-‬‬
‫העץ של ‪( x‬כולל ‪ x‬עצמו)‬
‫הציעו אלגוריתם בזמן )‪( O(h‬כאשר ‪ h‬גובה העץ) למימוש‬
‫)‪ – Greater(T,k‬מציאת מספר המפתחות שממש גדולים מ‪k‬‬
‫הדגמה‪k = 10 :‬‬
‫‪key=15‬‬
‫הקודקוד‬
‫‪size= 36‬‬
‫‪Greater(T,k)=15+1+4=20‬‬
‫‪key=20‬‬
‫‪size= 15‬‬
‫כל תת‪-‬העץ‬

‫‪key=8‬‬
‫‪size= 20‬‬

‫כל תת‪-‬העץ‬
‫‪key=10‬‬
‫‪size= 8‬‬
‫‪key=12‬‬
‫‪size= 4‬‬

‫‪key=4‬‬
‫‪size= 11‬‬


Slide 29

‫תרגיל ‪5‬‬
‫נתון ‪ T BST‬בגודל ‪ n‬בו כל המפתחות שונים זה מזה‪ ,‬בו לכל‬
‫קודקוד ‪ x‬יש שדה נוסף של ‪ – x.size‬מספר המפתחות של תת‪-‬‬
‫העץ של ‪( x‬כולל ‪ x‬עצמו)‬
‫הציעו אלגוריתם בזמן )‪( O(h‬כאשר ‪ h‬גובה העץ) למימוש‬
‫)‪ – Greater(T,k‬מציאת מספר המפתחות שממש גדולים מ‪k‬‬
‫)‪Greater(T, k‬‬
‫פתרון‪:‬‬
‫‪keys = 0‬‬
‫זמן ריצה‪O(h) :‬‬
‫) ‪while (T != Null‬‬
‫)‪if (k < T.key‬‬
‫הערה‪ h=O(n) :‬במקרה הגרוע‬
‫‪keys ← keys + T.right.size + 1‬‬
‫ביותר‪ ,‬ו)‪ O(logn‬במקרה הממוצע‬
‫‪T = T.left‬‬
‫)‪else if (k > T.key‬‬
‫‪T = T.right‬‬
‫‪{*} else // k = T.key‬‬
‫‪keys ← keys + T.right.size‬‬
‫;‪break‬‬
‫‪return keys‬‬


Slide 30

‫תרגיל ‪6‬‬
‫נתון עץ חיפוש בינארי ‪ T‬עם גובה ‪ h‬שמכיל קודקוד‬
‫עם ערך ‪ a‬וקודקוד עם ערך ‪ .b‬מצאו אלגוריתם‬
‫שמחזיר את כל המפתחות בטווח ]‪ )a‫)‪ ,O(h+k‬כאשר ‪ k‬מספר המפתחות בטווח‪.‬‬

‫‪x‬‬

‫‪b‬‬
‫‪a‬‬


Slide 31

6 ‫תרגיל‬
:‫פתרון‬
• Range(a, b)
1.
2.
3.

4.

Find nodes containing a and b in the tree, let Pa and Pb be the
search path from the root to a and b respectively.
Let x be the node where Pa and Pb split.
For every node v in the search path from a to x do:
if (a < v){
print v;
inorder(v.right);
}
For every node v in the search path from x to b do:
if (b > v){
inorder(v.left);
x
print v;
}

Time complexity:
Pa and Pb are at most O(h) long each.
In-order travel is O(n) for a tree with n nodes. We are printing k
numbers. So the inorder traversals cost O(k). Total time O(h+k)

b
a


Slide 32

‫תרגיל ‪7‬‬
‫• נתונים שני עצי חיפוש בינאריים ‪ T1‬ו‪ T2‬עם גבהים ‪ h1‬ו‪h2‬‬
‫בהתאמה (‪ h1‬ו‪ h2‬נתונים)‪.‬‬
‫• נתון בנוסף שכל הערכים ב‪ T1‬קטנים ממש מכל הערכים‬
‫ב‪ .T2‬הניחו שכל ערכי המפתחות שונים‪.‬‬
‫‪ .1‬כיצד ניתן לאחד את שני העצים לעץ אחד המכיל את איחוד‬
‫הערכים בזמן ))‪ ,O(min(h1,h2‬כך שגובה עץ האיחוד יהיה‬
‫))‪?O(max(h1,h2‬‬
‫‪ .2‬מה יהיה גובה עץ האיחוד?‬


Slide 33

7 ‫תרגיל‬
‫ כיצד ניתן לאחד את שני העצים לעץ אחד המכיל את איחוד‬.1
‫ כך שגובה עץ האיחוד יהיה‬,O(min(h1,h2)) ‫הערכים בזמן‬
?O(max(h1,h2))
case a: h1 ≤ h2
:‫פתרון‬
Extract from T1 the element v with the
maximum key in T1 in O(h1) time.
v.left ← T1
v.right ← T2
v is the root of the new merged tree.

T1
max(T1)

T2


Slide 34

7 ‫תרגיל‬
‫ כיצד ניתן לאחד את שני העצים לעץ אחד המכיל את איחוד‬.1
‫ כך שגובה עץ האיחוד יהיה‬,O(min(h1,h2)) ‫הערכים בזמן‬
?O(max(h1,h2))
case a: h1 ≤ h2
:‫פתרון‬
Extract from T1 the element v with the
maximum key in T1 in O(h1) time.
v.left ← T1
v.right ← T2
v is the root of the new merged tree.

case b: h1 > h2
Extract from T2 the element v with the
minimum key in T2 in O(h2) time.
v.left ← T1
v.right ← T2
v is the root of the new merged tree.

T2
T1
min(T2)


Slide 35

‫תרגיל ‪7‬‬
‫‪ .2‬מה יהיה גובה עץ האיחוד?‬
‫פתרון‪max(h1,h2)+1 :‬‬


Slide 36

‫תרגיל ‪8‬‬
‫• מצאו אלגוריתם הבודק אם עץ בינארי נתון ‪ T‬הוא‬
‫‪.BST‬‬
‫– הניחו שכל הערכים הינם מספרים שלמים שונים זה מזה‬
‫– הניחו שלכל קודקוד יש מצביע לשני הילדים אך לא‬
‫מצביע להורה‪.‬‬
‫פתרון (שגוי)‪:‬‬
‫נבדוק בכל קודקוד בצורה רקורסיבית שערך הבן השמאלי‬
‫קטן יותר מהערך הנוכחי וערך הבן הימני גדול יותר מהערך‬
‫הנוכחי‪.‬‬
‫פתרון זה אינו מספיק – מצאו דוגמא נגדית‬


Slide 37

‫תרגיל ‪8‬‬
‫פתרון (שגוי)‪:‬‬
‫נבדוק בכל קודקוד בצורה רקורסיבית שערך הבן השמאלי‬
‫קטן יותר מהערך הנוכחי וערך הבן הימני גדול יותר מהערך‬
‫הנוכחי‪.‬‬
‫פתרון זה אינו מספיק – מצאו דוגמא נגדית‬
‫‪15‬‬

‫‪8‬‬

‫‪20‬‬


Slide 38

8 ‫תרגיל‬
:)‫פתרון (מתוקן‬
‫נשתמש בפונקציית עזר ששומרת את הגבולות העליונים והתחתונים‬
‫העץ‬-‫של הערכים המורשים בתת‬
boolean isValid(Node root) {
return isValidHelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE)
}
boolean isValidHelper(Node curr, int min, int max) {
if(curr.value < min || curr.value > max)
return false
if (curr.left != null && !isValidHelper(curr.left, min, curr.value))
return false
if (curr.right != null && !isValidHelper(curr.right, curr.value, max))
return false
return true
}

.O(n) ‫זמן הריצה הינו‬
.‫ ובדיקה שהיא ממוינת‬Inorder‫פתרון אפשרי נוסף הוא מציאת הליכת ה‬
.O(n) ‫זמן ריצה‬


Slide 39

‫שאלות נוספות )אם יש זמן(‬
‫• הוכיחו או הפריכו את הטענות הבאות‬
‫נכון – הקודקוד הראשון בסריקת ‪ Preorder‬הוא הקודקוד‬
‫האחרון בסריקת ‪.Postorder‬‬
‫לא נכון – הקודקוד במיקום ה ‪ i‬בסריקת ‪ Preorder‬הוא הקודקוד‬
‫במיקום ה )‪ (n+1-i‬בסריקת ‪.Postorder‬‬
‫לאנכון‬
‫נכון – לכל קודקוד ‪ v‬במסלול הארוך ביותר מהשורש לעלה‬
‫בעץ ‪ T‬מתקיים )‪Height(v)+Depth(v)=Height(T‬‬
‫לא נכון– בעץ חיפוש בינארי‪ ,‬אם לשורש יש שני ילדים‪ ,‬אז‬
‫המסלול של ‪ maxPath‬עובר דרך הבן הימני‪.‬‬
‫נכון – בעץ חיפוש בינארי תמיד ניתן לשחזר את העץ מידיעת‬
‫סריקת ה‪ Preorder‬בלבד‪.‬‬