Advertisement
darkhelmet125

Exam Review 2

Nov 3rd, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. Review Exam #2
  2.  
  3. I. LIFO Stacks
  4. LIFO - Last In First Out
  5. Ex: Stack of books
  6. 1)Stack
  7. a)Applications: infix to postfix
  8. b)Data Members
  9. 1) A place to hold a collection of like items
  10. -array
  11. -linked list
  12. 2) A place to hold the position stack top
  13. 3) Max Size of the stack
  14. c)methods
  15. Handout
  16. d)growth rate
  17.  
  18. II. FIFO Queues
  19. FIFO - First In First Out
  20. Ex: Jack in the Box drive-thru
  21. 1)Queue
  22. a)Applications: Binary Tree???
  23. b)data members
  24. c)methods
  25. d)growth rate
  26.  
  27. III. Binary Tree
  28. 1) Definition
  29. A tree is a recursive collection of nodes where a special node is called the root (r) and can have maximum of two children subtrees T0, T1 each of whose roots are connected by a directed edge to r
  30. 2) Terms
  31. Node - a point in a tree structure where data is stored
  32. Directed edges - a connection between two nodes in a tree; also called a branch
  33. Root - a special node with at least one child; a node with no parent
  34. Leaf - a node without children
  35. Parent - r is said to be the parent of each subtree
  36. Child - a root of each subtree is said to be a child of r; left child and right child
  37. Ancestor - if a path exist from n1 to n2 then n1 is an ancestor of n2
  38. Descendant - if a path exst from n1 to n2 then n2 is a descendant of n1
  39. Level - is the distance of a node from the root; root is at level zero
  40. Height of a tree - is equal to the height of the maximum level
  41. 3)Traversals
  42. a)inorder
  43. Left Root Right
  44. b)preorder
  45. Root Left Right
  46. c)postorder
  47. Left Right Root
  48.  
  49. IV. Binary Search Tree (BST)
  50. 1) Definition
  51. A binary search tree is a binary tree
  52. Where the value of the left child is less than the value of the parent
  53. And the value of the right child is greater than the value of the parent
  54. 2)Growth Rate for BST
  55. Worst case growth rate: O(n)
  56. Average case growth rate: O(logn)
  57. 3)Growth Rate for operations
  58. O(logn)
  59. 4)Operations for BST
  60. a)Insert
  61. function: insert(newItem)
  62. Pseudocode:
  63. create new node, insert data, and set both child pointers to NULL
  64. search the tree to find the location to inser the new item; insert at the end
  65. insert new node as leaf
  66. insert new node into the proper location
  67. growth rate: O(logn)
  68. b)Delete
  69. function: delete(deleteItem)
  70. Pseudocode:
  71. Find the node
  72. Delete the node
  73. 1. Delete into an empty tree (only a root)
  74. 2. Delete a leaf node
  75. 3. Delete node with one child
  76. 4. Delete node with 2 children
  77. 1. Delete into empty tree
  78. Pseudocode:
  79. Delete into an empty tree;
  80. assign temp to root;
  81. delete temp;
  82. NULL root;
  83. growth rate: O(1)
  84. 2. Delete a leaf node
  85. Pseudocode:
  86. Determine if a leaf node;
  87. Delete current;
  88. Set previous child pointer to NULL;
  89. 3. Delete node with one child
  90. Pseudocode:
  91. Determine if there is only one child
  92. Set previous child pointer to point to grandchild
  93. Delete current node
  94. 4. Delete node with 2 children
  95. Pseudocode:
  96. Determine if there are 2 children
  97. Find replacement value(delete item value)
  98. Delete original, replacement node; this will be a lear or a one child node
  99. growth rate: O(logn)
  100. c)Search
  101. Non-recursive function: search()
  102. PseudoCode:
  103. if IsEmpty
  104. return error
  105. else
  106. search tree
  107. code:
  108. set found=false;
  109. set current=root;
  110. while(current!=NULL && current==found)
  111. if(current -> info==search info)
  112. set found=true;
  113. else
  114. if(search info<current -> info)
  115. set current= currenr->left;
  116. else
  117. set current=current->right;
  118. Growth rate: O(logn)
  119.  
  120. V. Recursion
  121. 1)What is Recursion?
  122. Recursion is a unique problem-solving approach where you solve a problem by repeatedly breaking the problem into smaller versions of the same problem, until you reduce the subproblem to a tribial size that can be easily solved
  123. Once the trivial solution is found, keep repeatedly combinin the solution until the original solution is found to the original problem
  124. 2)A recursive function must solve
  125. A base case (simplest solution)
  126. General case (diminished part of the proglem making progress toward the base case)
  127. 3)Growth rate: O(n)
  128. 4)
  129. 5)
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement