# آموزش ساختمان داده ها - بخش ششم

• View
20

3

Embed Size (px)

### Text of آموزش ساختمان داده ها - بخش ششم

1. 1. faradars.org/fvds9402 : ( : ( ): ) ( ) 1 FaraDars.org
2. 2. faradars.org/fvds9402 2 1-:--- 2-:- FaraDars.org
3. 3. faradars.org/fvds9402 3 . . () FaraDars.org
5. 5. faradars.org/fvds9402 . . 5 FaraDars.org
6. 6. faradars.org/fvds9402 :(). :. 6 FaraDars.org
7. 7. faradars.org/fvds9402 :. :314: :2h:n = 2h+1 1:floor (n/2) 7 FaraDars.org
9. 9. faradars.org/fvds9402 k: 2:3: 9 1...)2()1( 210 nnknkn kk 120 nn 3,2 02 nn11,8,1 023 nnn 12 230 nnn FaraDars.org
10. 10. faradars.org/fvds9402 i: 1)i=1. 2)i< >1i/2. 3)2i right ); cout

left ); cout right ); } pre (p){ if (p==null) return; cout data; pre ( p -> left ); pre ( p -> right ); } 22 FaraDars.org

11. 23. faradars.org/fvds9402 : Preorder : a b d f c e g Inorder : d f b a e g c 23 FaraDars.org
12. 24. faradars.org/fvds9402 preorderpostorder. Preorderpostorder .. : Preorder : 8 5 3 4 2 6 1 9 Postorder : 4 3 5 6 9 1 2 8 (53) , (34) (19) . 24 FaraDars.org
13. 25. faradars.org/fvds9402 . preorderpostorder .:^k2(k:) : Preorder : A B C D E F G H I Postorder : C E D B H I G F A : 25 FaraDars.org
14. 26. faradars.org/fvds9402 (Level Order) . level oder. . 26 FaraDars.org
15. 27. faradars.org/fvds9402 . inorderinfix.A-(C/5*2)+(D*5%4) preorderprefix. postorderpostfix. 27 FaraDars.org
16. 28. faradars.org/fvds9402 n inorder1n. ii-1n-i. 1 2 3 i-1 i i+1 . n 28 n n n bn 2 1 1 1,1 10 1 1 bb bbb in n i in FaraDars.org
17. 29. faradars.org/fvds9402 3. : : - - - : 29 5 4 45 4 !3!3 !3456 4 !3!3 !6 4 3 6 21101102 bbbbb FaraDars.org
18. 30. faradars.org/fvds9402 (( A Threaded Binary Tree n2n. n-1n+1. . . H D I B E A F C G 30 FaraDars.org
19. 31. faradars.org/fvds9402 struct node{ struct node *left ; struct node *right ; char data ; short int lflag ; short int rflag ; }; lflag=1left. rflag=1right. 31 FaraDars.org
20. 32. faradars.org/fvds9402 () . "" . 32 FaraDars.org
21. 33. faradars.org/fvds9402 1-(). 2-. . 33 FaraDars.org
22. 34. faradars.org/fvds9402 (Forest) 34 n.(n>=0) FaraDars.org
23. 35. faradars.org/fvds9402 35 : 1-. 2-. . FaraDars.org
24. 36. faradars.org/fvds9402 (2-Tree) 02. . 2. I. E. LI. LE. 36 5221 EL I2LL IE 3 2 E I 110 IL FaraDars.org
26. 38. faradars.org/fvds9402 : struct node{ int data; struct node* left; struct node* right; }; newnode: struct node* newnode(int item){ struct node* node = (struct node*) malloc(sizeof(struct node)); node ->data = item; node ->left = NULL; node ->right = NULL; return(node); } 38 FaraDars.org
27. 39. faradars.org/fvds9402 unsigned int count (struct node* root) { if (root == NULL) return (0); return (1 + count ( root->left ) + count ( root->right ) ); } 39 FaraDars.org
28. 40. faradars.org/fvds9402 bool isleaf (struct node *n) { if (n== NULL) return false; if ((n ->left == NULL) && (n ->right == NULL) ) return true; return false; } 40 FaraDars.org
29. 41. faradars.org/fvds9402 int main(void) { struct node* nr = NULL; struct node* root = newnode (2); root ->left = newnode (7); root -> right = newnode (5); root -> left -> right = newnode (6); root -> left -> right -> left = newnode (1); root -> right -> right = newnode (9); } 41 FaraDars.org
30. 42. faradars.org/fvds9402 bool f(struct Node* root, unsigned int i , unsigned int n) { if (root == NULL) return (true); if (i >= n) return (false); return( f(root->left, 2*i , n) && f (root->right , 2*i + 1 , n) ); } 42 FaraDars.org
31. 43. faradars.org/fvds9402 struct node* remove (struct node* root){ if (root==NULL) return NULL; root->left = remove (root->left); root->right = remove (root->right); if ((root->left==NULL) && (root->right==NULL) ) return root; if (root->left==NULL) { struct node* n = root->right; free(root); return n; } if (root->right==NULL) { struct node* n = root->left; free(root); return n; } return root; } 43 FaraDars.org
32. 44. faradars.org/fvds9402 levelorder void LevelOrder( struct node* root ){ int rear, front; struct node ** queue = createQueue( &front , &rear ); struct node * t = root; while(t) { cout data ; if (t ->left ) enQueue( queue, &rear, t -> left ); if (t ->right ) enQueue (queue, &rear, t -> right ); t = deQueue (queue, &front ); } } 44 FaraDars.org
33. 45. faradars.org/fvds9402 struct node** createQueue(int *front , int *rear) { struct node ** queue = (struct node **) malloc (sizeof(struct node*) * MAX); *front = *rear = 0; return queue; } void enQueue(struct node **queue, int *rear, struct node *newnode) { queue[*rear] = newnode; (*rear)++; } struct node * deQueue(struct node **queue, int *front) { (*front)++; return queue[*front - 1]; } 45 FaraDars.org
34. 46. faradars.org/fvds9402 int sum ( node *root ){ int s = 0; if ( root != NULL ) { if ( isLeaf (root ->left ) ) s = s + root ->left ->data; else s = s+ sum (root ->left); s = s + sum (root -> right); } return s; } 46 FaraDars.org
36. 48. faradars.org/fvds9402 (BST) ((Binary Search Tree: 1-. 2-. 3-. 4-. 48 FaraDars.org
37. 49. faradars.org/fvds9402 BST lgnn. 49 nhn lg FaraDars.org
38. 50. faradars.org/fvds9402 BST 3,2,1BSTBST : : 123 , 132 , 321 , 312 , 231 , 213 213231.5. 35. 50 FaraDars.org
39. 51. faradars.org/fvds9402 BST : 1-. 2-. 3-. 51 FaraDars.org
40. 52. faradars.org/fvds9402 :. 52 18 24 212 32 36 310 314 FaraDars.org
41. 53. faradars.org/fvds9402 search( t , key) { if ( t==NULL or key == t -> data ) return t ; if ( key < t -> data ) return search ( t -> left , key ) ; else return search ( t -> right , key ) ; } 53 FaraDars.org
42. 54. faradars.org/fvds9402 BST"x"()x . :45:< 60 , 25 , 50 , 30 , 45 > 54 FaraDars.org
43. 55. faradars.org/fvds9402 BST . :45: BSTnhO(h). 55 FaraDars.org
44. 56. faradars.org/fvds9402 BST item t insert( t , item ) { p = search( t , item ); if ( p or !t ) { n = malloc(sizeof(t)); n -> data = item ; n -> left = NULL; n -> right = NULL; if ( t ) if (item < p -> data ) p -> left = n; else p -> right = n; else t = n; } } 56 FaraDars.org
45. 57. faradars.org/fvds9402 ninorder .Tree Sort. inorder : 20 , 25 , 30 , 45 , 50 , 60 , 65 , 70 57 FaraDars.org
46. 58. faradars.org/fvds9402 BST : 1-. 2-. 3-inorder. (.) 58 FaraDars.org
48. 60. faradars.org/fvds9402 BST BSTNULL. min( t ) { while ( t -> left != NULL ) t = t -> left; return t; } t: max( t ) { while ( t -> right != NULL ) t = t -> right; return t; } 60 FaraDars.org
50. 62. faradars.org/fvds9402 (Heap) Heap: 1-Max-Heap: . :. 62 FaraDars.org
51. 63. faradars.org/fvds9402 ((Heap 2-Min-Heap: . Minheap:. 63 FaraDars.org
52. 64. faradars.org/fvds9402 heap v[i]: :v[2i+1]:v[2i+2]: 64 FaraDars.org
53. 65. faradars.org/fvds9402 Heap () ReHeap(.) :5: 65 FaraDars.org
54. 66. faradars.org/fvds9402 Heaps 66 19 4222127 23 45 35 42 FaraDars.org
55. 67. faradars.org/fvds9402 Heaps 67 19 4222142 23 45 35 27 FaraDars.org
56. 68. faradars.org/fvds9402 Heaps 68 19 4222135 23 45 42 27 FaraDars.org
58. 70. faradars.org/fvds9402 Heap . 70 FaraDars.org
59. 71. faradars.org/fvds9402 Heaps 71 19 4222127 36 45 35 3 : FaraDars.org
60. 72. faradars.org/fvds9402 Heaps 72 19 4222127 36 3 35 : FaraDars.org
61. 73. faradars.org/fvds9402 Heaps 73 19 4222127 3 36 35 : FaraDars.org
62. 74. faradars.org/fvds9402 Heaps 74 19 432127 22 23 35 : FaraDars.org
63. 75. faradars.org/fvds9402 (Priority Queue) . . . . : 1-(max-priority queue) 2-(min-priority queue) 75 FaraDars.org
64. 76. faradars.org/fvds9402 1- . :O(1):O(n) 2- . :O(n):O(1) 3-heap)) :O(logn):O(logn) . 76 FaraDars.org
65. 77. faradars.org/fvds9402 (). : . 77 MAXIMUM(S) O(1) INSERT(S,x) xO(lgn) EXTRACT-MAX(S) O(lgn) INCREASE-KEY(S,x,k) x kO(lgn) FaraDars.org
66. 78. faradars.org/fvds9402 -(Min-Max Heap) 78 . minmax.min. min:. max:. FaraDars.org
67. 79. faradars.org/fvds9402 - 80: 79 FaraDars.org
68. 80. faradars.org/fvds9402 - 5: 80 FaraDars.org
70. 82. faradars.org/fvds9402 Deap : 1-. 2-minheap. 3-maxheap. 4- . :O(lgn) :O(lgn) deap.n-1. 82 FaraDars.org
71. 83. faradars.org/fvds9402 deap iminheapmaxheap. jnij/2. 83 1log2 2 i ij FaraDars.org
72. 84. faradars.org/fvds9402 Deap 4: 84 FaraDars.org
73. 85. faradars.org/fvds9402 Treap BSTHEAP. . BST. MinHeap. :logn 85 FaraDars.org

Documents
Documents
Documents
Education
Documents
Documents
Education
Documents
Documents
Documents
Science
Documents
Spiritual
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Education
Education
Engineering
Documents
Documents
Internet
Documents
Documents
Documents