Presentation is loading. Please wait.

Presentation is loading. Please wait.

ساختمان داده ها و الگوريتم ها

Similar presentations


Presentation on theme: "ساختمان داده ها و الگوريتم ها"— Presentation transcript:

1 ساختمان داده ها و الگوريتم ها
Binary Search Trees ساختمان داده ها و الگوريتم ها

2 درخت دودويي Binary Trees
تعريف بازگشتي درخت تهي، درختي دودويي است گرهي که فرزندان آن، دو درختچه باشند، درختي دودويي است درختي دودويي است که با شروع از 1 و با اعمال 2، به تعداد متناهي بتوان آن را ساخت. 56 26 200 18 28 190 213 12 24 27 اين درخت دو دويي است ؟

3 پيمايش درخت دودويي پيمايش درخت: انجام يک عمل ( مثل چاپ مقدار) بر روي گرههاي درخت بازگشتي Recursive تکراري Iterative پيمايش بازگشتي: پردازش گره و درختچه هاي (فرزندان) چپ و راست آن Inorder: 1-فرزند سمت چپ، 2-ريشه ، 3- فرزند سمت راست Preorder: 1- ريشه، 2- فرزند سمت چپ، 3- فرزند سمت راست Postorder:1-فرزند سمت چپ، 2- فرزند سمت راست، 3- ريشه

4 محاسبه عبارتهاي جبري (a + b) * (c + d) + e – f/g*h + 3.25
هر عبارت متشكل از سه نوع عضو زير است: عملگر:‌.Operators (+, -, /, *). عملوند ها Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.). علايم ويژه دسته بندي :‌ Delimiters ((, )).

5 درجه يك عملگر تعداد عملوندهايي كه يك عملگر نياز دارد
عملگر مثل يك تابع و عملوند، آرگومان تابع است عملگرهاي دوتايي، دو عملوند نياز دارند a + b c / d e - f عملگرهاي يكاني، يك عملوند نياز دارند + g - h نمايش عبارتهاي جبري نمايش ميانوندي نمايش پيشوندي نمايش پسوندي

6 نمايش ميانوندي عملوند بين دو عملگر قرار مي گيرد ترتيب و الويت عملگرها
a * b a + b * c a * b / c (a + b) * (c + d) + e – f/g*h ترتيب و الويت عملگرها براي تفسير درست عبارت لازم است عمليات ضرب و تقسيم بر جمع و تفريق مقدم هستند از چپ به راست است

7 علايم دسته بندي با استفاده از اين علايم، عبارتهايي ساخته مي شوند كه در عبارات بزرگتر مثل يك عملوند بكار مي روند: (a + b) * (c – d) / (e – f)

8 محاسبه عبارت براي محاسبه عبارتهاي جبري به علايم دسته بندي، اوليت بندي عملگرها نياز است براي محاسبه آنها برنامه پيچيده اي نياز داريم نمايش پيشوندي و پسوندي چنين ملزوماتي ندارند محاسبه عبارات به شكل پسوندي يا پيشوندي براي كامپيوتر راحتتر است نمايش پسوندي Postfix نمايش پسوندي يك عملگر دوتايي با دو عملوند به صورت زير است: A o B = AB o , o is the operator نمايش پيشوندي يك عملگر دوديي چنين است: A o B = oAB ,

9 Infix = (a + b) * (c – d) / (e + f)
چند نمونه عبارت پسوندي Infix = a + b * c Postfix = a b c * + Infix = a * b + c Postfix = a b * c + Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + /

10 عملگرهاي يكاني براي خوانايي عبارت از علايمي ديگر براي عملگرهاي يكاني استفاده مي كنيم + a => + a + b => b + - a => a ? - a-b => a ? b - @ and ? are new symbols that are used to denote the unary + and – operators, respectively.

11 Postfix Evaluation عبارت را از چپ به راست مي خوانيم و به هر عملوند كه مي رسيم آن را در پشته يادداشت مي كنيم با رسيدن به عملگر به تعداد لازم عملوند از پشته بر مي داريم و عمل خواسته شده را انجام مي دهيم و سپس نتيجه محاسبه را در پشته يادداشت مي كنيم نتيجه نهايي عبارت در بالاي پشته قرار دارد

12 Postfix Evaluation b + c d - * e f + / + c d - * e f + / b a stack
(a + b) * (c – d) / (e + f) a b + c d - * e f + / stack b + c d - * e f + / + c d - * e f + / b a

13 Postfix Evaluation d c c d - * e f + / (a + b) d - * e f + /
(a + b) * (c – d) / (e + f) a b + c d - * e f + / stack d c c d - * e f + / (a + b) d - * e f + / - * e f + /

14 Postfix Evaluation * e f + / (c – d) (a + b) stack
(a + b) * (c – d) / (e + f) a b + c d - * e f + / stack * e f + / (c – d) (a + b)

15 Postfix Evaluation a b + c d - * e f + / a b + c d - * e f + /
stack

16 Postfix Evaluation a b + c d - * e f + / a b + c d - * e f + /
stack

17 نمايش يك عبارت با درخت دودويي
a + b + a b - a - a Each leaf represents a variable or constant. Each nonleaf represents an operator. The left operand (if any) of this operator is represented by the left subtree, the right subtree represents the right operand of the operator.

18 نمايش يك عبارت با درخت دودويي
(a + b) * (c – d) / (e + f) / + a b - c d e f *

19 مزاياي نمايش با درخت + a b - c d e f * /
عملوندهاي راست و چپ را براحتي مي توان نشان داد با الگوريتم هاي بازگشتي براحتي مي توان عبارات را محاسبه كرد + a b - c d e f * / Evaluate an expression– if there is no root, return null; if the root is a leaf, return the value of variable or constant in the leaf; otherwise, evaluate the left and right operands recursively; compute the root operator; return the result.

20 پيمايش درخت پيمايش درخت : ملاقات گرههاي آن Preorder Inorder Postorder
هر گره را فقط يكبار مي بينيم هنگام ديدن يك گره عمل خواسته شده را روي آن انجام مي دهيم Preorder Inorder Postorder Level order

21 پيمايش پيش ترتيب public static void preOrder(BinaryTreeNode t) {
if (t != null) visit(t); preOrder(t.leftChild); preOrder(t.rightChild); } t is the root of the subtree being traversed.

22 Preorder Example (visit = print)
b c a b c

23 Preorder Example (visit = print)
b c d e f g h i j During the traversal, t starts at the root, moves to the left child b of the root, then to the left child d of b. When the traversal of the left subtree of b is complete, t, once again, points to the node b. The t moves into the right subtree of b. When the traversal of this right subtree is complete, t again points to b. Following this, t points to a. We see that t points to every node in the binary tree three times – once when you get to the node from its parent (or in the case of the root, t is initially at the root), once when you return from the left subtree of the node, and once when you return from the node’s right subtree. Of these three times that t points to a node, the node is visited the first time. a b d g h e i c f j

24 Preorder Of Expression Tree
+ a b - c d e f * / / * + a b - c d + e f Gives prefix form of expression!

25 Inorder Traversal public static void inOrder(BinaryTreeNode t) {
if (t != null) inOrder(t.leftChild); visit(t); inOrder(t.rightChild); }

26 Inorder Example (visit = print)
b c b a c

27 Inorder Example (visit = print)
b c d e f g h i j g d h b e i a f j c

28 پيمايش ميان ترتيب با تصوير هندسي درخت
a b c d e f g h i j g d h b e i a f j c

29 Inorder Of Expression Tree
+ a b - c d e f * / e a + b * c d / f - پيمايش ميان ترتيب، عبارت ميانوند را بدون پرانتز مي دهد

30 Postorder Traversal public static void postOrder(BinaryTreeNode t) {
if (t != null) postOrder(t.leftChild); postOrder(t.rightChild); visit(t); }

31 Postorder Example (visit = print)
b c b c a

32 Postorder Example (visit = print)
b c d e f g h i j g h d i e b j f c a

33 Postorder Of Expression Tree
+ a b - c d e f * / a b + c d - * e f + / Gives postfix form of expression!

34 Traversal Applications
b c d e f g h i j Make a clone using postorder traversal … clone the left subtree, clone the right subtree, clone the root in the visit step. Determine height using postorder traversal … determine the height of the left subtree, determine the height of the right subtree, in the visit step add 1 to the max of the already determined heights of the left and right subtrees. Determine number of nodes using preorder, inorder, or postorder traversal … initialize a counter to 0, add 1 to the counter in the visit step. Make a clone. Determine height. Determine number of nodes.

35 Level Order Let t be the tree root. while (t != null) { visit t and put its children on a FIFO queue; remove a node from the FIFO queue and call it t; // remove returns null when queue is empty }

36 Level-Order Example (visit = print)
b c d e f g h i j a b c d e f g h i j

37 فرض كنيد اعضاي درخت غير تكراري هستند
ساخت درخت دودويي فرض كنيد اعضاي درخت غير تكراري هستند آيا با داشتن يكي از پيمايشها، مي توان درخت را ساخت!؟ اگر پيمايش داده شده بيش از يك عضو داشته باشد؛ نمي توان درخت را به صورت منحصر بفرد ساخت آيا با داشتن دو نوع پيمايش مي توان را درخت را به صورت منحصر بفرد ساخت ؟ شايد بتوانيم!

38 Some Examples inorder = ab postorder = ab level order = ab a b a b
preorder = ab inorder = ab b a a b postorder = ab b a b a level order = ab a b a b

39 Preorder And Postorder
b a b preorder = ab postorder = ba Preorder and postorder do not uniquely define a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example).

40 Inorder And Preorder inorder = g d h b e i a f j c
preorder = a b d g h e i c f j Scan the preorder left to right using the inorder to separate left and right subtrees. a is the root of the tree; gdhbei are in the left subtree; fjc are in the right subtree. a gdhbei fjc

41 Inorder And Preorder preorder = a b d g h e i c f j
gdhbei fjc preorder = a b d g h e i c f j b is the next root; gdh are in the left subtree; ei are in the right subtree. a gdh fjc b ei

42 Inorder And Preorder preorder = a b d g h e i c f j
gdh fjc b ei preorder = a b d g h e i c f j d is the next root; g is in the left subtree; h is in the right subtree. a g fjc b ei d h

43 Inorder And Postorder inorder = g d h b e i a f j c
Scan postorder from right to left using inorder to separate left and right subtrees. inorder = g d h b e i a f j c postorder = g h d i e b j f c a Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

44 Inorder And Level Order
Scan level order from left to right using inorder to separate left and right subtrees. inorder = g d h b e i a f j c level order = a b c d e f g h i j Tree root is a; gdhbei are in left subtree; fjc are in right subtree.

45 درخت جستجويي دو دويي درخت جستجويي دو دويي = Binary Search Tree (BST)
ساختمان داده اي ديگر براي نگهداري مجموعه هاي پوياست که عمليات زير را انجام مي دهد: Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete. براي ساختن Dictionary. و Priority Queue مي توان از آن استفاده کرد زمان انجام عمليات اوليه، متناسب با عمق(ارتفاع) درخت است. O(h).

46 نمايشBST با استفاده از ساختار پيوندي گرهها نمايش داده مي شود
root(T): T اشاره گر به ريشه درخت هر گره مشخصه هاي زير را دارد: key left – pointer to left child: root of left subtree. right – pointer to right child : root of right subtree. p – pointer to parent. p[root[T]] = NIL (optional).

47 خواص BST مشخصه کليد (key) بايد اين خواص را داشته باشد 56 26 200 18 28
 y in left subtree of x, then key[y]  key[x].  y in right subtree of x, then key[y]  key[x]. 56 26 200 18 28 190 213 12 24 27

48 پيمايشInorder هزينه پيمايش چقدر است ؟
با استفاد از خاصيت BST مي توان مقادير درخت را به صورت صعودي يکنوا نشان داد Inorder-Tree-Walk (x) 1. if x  NIL then Inorder-Tree-Walk(left[p]) print key[x] Inorder-Tree-Walk(right[p]) 56 26 200 18 28 190 213 12 24 27 12 18 24 26 27 28 56 190 210 213 هزينه پيمايش چقدر است ؟

49 درستي پيمايش Inorder اثبات: درستي و پايان پذيري
با استفاده از استقراء رياضي: پيمايش درخت سمت چپ و راست به درست انجام مي شود اگر درخت تهي باشد، درستي و پايان پذيري بديهي است اگر درخت بيش از يک گره باشد درختچه سمت چپ - با استفاده از استقراء - چاپ مي شود ريشه چاپ مي شود؛ با تکيه بر خاصيت BST‌ مقدار ريشه از مقادير درختچه سمت چپ بزرگتر است بنابراين در جايگاه خود چاپ مي شود درخت سمت راست نيز بنابر استقراء بدرستي چاپ مي شود و مقادير آن از مقدار ريشه بزرگترند

50 جستجو در BST کليه عمليات مجموعه هاي پويا را مي توان با هزينه اي متناسب با عمق درخت انجام داد O(h). اگر درخت متوازن باشد( عمق درختچه هاي سمت چپو راست نزديک هم باشد) h = (lg n) در بدترين حالت: h = (n). درخت دودويي به صورت زنجيره اي از گرهها در مي آيد

51 Tree Search Tree-Search(x, k) 1. if x = NIL or k = key[x]
then return x 3. if k < key[x] then return Tree-Search(left[x], k) else return Tree-Search(right[x], k) Running time: O(h) Aside: tail-recursion

52 جستجوي تکراري Iterative
Iterative-Tree-Search(x, k) 1. while x  NIL and k  key[x] do if k < key[x] then x  left[x] else x  right[x] 5. return x Search 24 56 26 200 18 28 190 213 12 24 27 جستجوي دوديي به صورت تکراري، کاراتر از روش بازگشتي است؛ اما پياده سازي روش بازگشتي سريعتر و ساده تر است

53 يافتن Min , Max هزينه اين عمليات چقدر است ؟ ساختارBST تضمين مي کند:
Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL while right[x]  NIL do x  left[x] do x  right[x] 3. return x return x هزينه اين عمليات چقدر است ؟

54 Predecessor & Successor
Successor (x) گره بعد از x: گرهي که مقدار آن از مقدار x‌ بزرگتر ولي از ديگر کليدهاي بزرگتر از x ، کوچکتر است کوچکترين کران بالا گرهي که هنگام پيمايش inorder، بلافاصله بعد از x‌ ظاهر مي شود Predecessor(x) گره قبل از x: گرهي که مقدار آن از کليد x‌ کوچکتر ولي از ديگر کليدهاي کوچکتر ازx ، بزرگتر است بزرگترين کران پايين گرهي که هنگام پيمايش inorder، بلافاصله قبل از x‌ ظاهر مي شود

55 شبه کد Successor Code for predecessor is symmetric. Running time: O(h)
Tree-Successor(x) if right[x]  NIL then return Tree-Minimum(right[x]) y  p[x] while y  NIL and x = right[y] do x  y y  p[y] return y Successor (28) 56 26 200 18 28 190 213 12 24 27 y x Code for predecessor is symmetric. Running time: O(h)

56 افزودن يک گره به درخت Tree-Insert(T, z) y  NIL x  root[T] while x  NIL do y  x if key[z] < key[x] then x  left[x] else x  right[x] p[z]  y if y = NIL then root[t]  z else if key[z] < key[y] then left[y]  z else right[y]  z عمل افزودن بايد چنان انجام شود که خاصيت BST حفظ شود مثال: افزودن 50 به درخت زير 50 56 26 200 18 28 190 213 12 24 27

57 آناليز افزودن گره Initialization: O(1)
Tree-Insert(T, z) y  NIL x  root[T] while x  NIL do y  x if key[z] < key[x] then x  left[x] else x  right[x] p[z]  y if y = NIL then root[t]  z else if key[z] < key[y] then left[y]  z else right[y]  z Initialization: O(1) While loop in lines 3-7 searches for place to insert z, maintaining parent y.O(h) time. Lines 8-13 insert the value: O(1)  TOTAL: O(h) time to insert a node.

58 Tree-Delete (T, x) if x has no children then remove x  case 0
if x has one child then point p[x] to child  case 1 if x has two children (subtrees) then  case 2 swap x with its successor perform case 0 or case 1 Delete 28 Delete 26 56 56 26 200 26 200 190 213 190 213 18 28 18 28 12 24 12 24 27 27

59 شبه کد Successor Code for predecessor is symmetric. Running time: O(h)
Tree-Successor(x) if right[x]  NIL then return Tree-Minimum(right[x]) y  p[x] while y  NIL and x = right[y] do x  y y  p[y] return y Successor (28) 56 26 200 18 28 190 213 12 24 27 y x Code for predecessor is symmetric. Running time: O(h)

60 شبه کد حذف Tree-Delete(T, z)
/* Determine which node to splice out: either z or z’s successor. */ if left[z] = NIL or right[z] = NIL then y  z else y  Tree-Successor[z] /* Set x to a non-NIL child of x, or to NIL if y has no children. */ if left[y]  NIL then x  left[y] else x  right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ if x  NIL then p[x]  p[y] /* Continued on next slide */

61 شبه کد حذف يک گره Tree-Delete(T, z) (Contd. from previous slide)
if p[y] = NIL then root[T]  x else if y = left[p[i]] then left[p[y]]  x else right[p[y]]  x /* If z’s successor was spliced out, copy its data into z */ if y  z then key[z]  key[y] copy y’s satellite data into z. return y

62 درستي الگوريتم حذف در حالت دوم، اجراي الگوريتم به حالت 0 يا 1 منتقل مي شود؛ زيرا: اگر x دو فرزند داشته باشد، گره بعدي آن، کوچکترين گره درختچه سمت راست خواهد بود. اين گره فرزند سمت چپ ندارد. اگر فرزند راست داشته باشد، حالت 0 و اگر نداشته باشد حالت 1 اتفاق مي افتد در اين الگوريتم مي توان از گره قبل نيز استفاده کرد. بهتر است هنگام حذف گره يکي از گرههاي قبل و بعد بصورت تصادفي انتخاب شود تا شانس توليد درخت نامتوازن کاهش يابد.

63 تمرين شبه کد زير براي مرتب سازي بوسيله BST نوشته شده است. هزينه اجراي آن را در بدترين، متوسط و بهترين حالت محاسبه کنيد ارزيابي شما از کارايي اين الگوريتم در مقايسه با ديگر روشهاي مرتب سازي چيست ؟ Sort (A) for i  1 to n do tree-insert(A[i]) inorder-tree-walk(root) This presentation is mainly extracted from :


Download ppt "ساختمان داده ها و الگوريتم ها"

Similar presentations


Ads by Google