Data Structures Final Exam Review

Only available on StudyMode
  • Topic: Hash table, Binary search tree, Sorting algorithm
  • Pages : 5 (1611 words )
  • Download(s) : 144
  • Published : February 21, 2013
Open Document
Text Preview
Ch. 4 Trees

Depth: length of the unique path from root to node
Height: length of the longest path from the node to a leaf •Keep children in a linked list
Preorder traversal: work at the node is done before its children are processed •Postorder traversal: work at a node is performed after its children are evaluated •Binary tree: no node can have more than two children

oAverage depth is O(rootN), O(logN) for binary search tree
oCan maintain references to children cuz there’s only 2
Example of a binary tree: expression tree
oLeaves are operands, other nodes contain operators
oInorder traversal: recursively print left child, then parent, then right •O(N)
oPostorder traversal: recursively print left subtree, right subtree, then operator → O(N) oPreorder traversal: print operator, then recursively print the left and right subtrees oConstructing an expression tree from a postfix expression: read one symbol at a time; if operand, create a one-node tree and push it onto a stack. If operator, pop two trees T1, T2 from stack, and form a new tree whose root is the operator, and whose left and right children are T2 and T1; push new tree onto stack •Binary search tree: binary tree with the property that for every node X, the value of all items in its left subtree are < X and the value of all items in the right subtree are > X oContains: Uses O(logN) stack space

ofindMin, findMax: traverse all the way left or right from the root oinsert: traverse down tree as would with contains, stick it at the end oremove: easy if leaf or has one child; if two children; replace data in node with smallest data of right subtree, and recursively delete that node oLazy deletion: if expected number of deletions is small, just mark the node as deleted but don’t actually do anything; small time penalty as depth doesn’t really increase oRunning time of all operations on a node is O(depth), and the average depth is O(logN) oIf input is presorted, inserts takes O(N^2) since there are no left children •AVL Trees

oBinary search tree with a balance condition: ensure depth is O(logN) by requiring that for every node in the tree, the height of the left and right subtrees can differ by at most 1 (height of empty tree is -1) oMinimum number of nodes S(h) of an AVL tree of height h is S(h) = S(h-1) + S(h-2) + 1 where S(1) = 2

oAll operations O(logN) except possibly insertion
Violation after inserting into left subtree of left child, or right subtree of right child → single rotation •Violation after inserting into right subtree of left child or left subtree of right child → double rotation •Splay Trees

oAmortized O(logN) cost per operation
oMove accessed nodes to root
oZig-zag: node is a left child and its parent is a right child or vice versa oZig-zig: node and its parent are both left or right children •Level-order traversal: all nodes at depth d processed before any node at d+1; not done recursively, it uses a queue instead of stack recursion •Set interface: unique operations are insert, remove, and search oTreeset maintains order, basic operations take O(logN) worst case •Map interface: collection of entries consisting of keys and their values oKeys are unique, but several keys can map to the same values oSortedMap: keys maintained in sorted order

oOperations include isEmpty, clear, size, containsKey, get, put oNo iterator, but:
Set keySet()
Collection values()
Set entrySet()
oFor an object of type Map.Entry, available methods include •KeyType getKey()
ValueType getValue()
ValueType setValue(ValueType newValue)
TreeSet and TreeMap implemented with a balanced binary search tree

Ch. 5 Hashing

Hashing is a technique for inserting, deleting and searching in O(N) average, so findMin, findMax and printing the table in order aren’t supported •Hash function maps a key into some number from 0 to TableSize – 1 and places it in the appropriate cell •If the input...
tracking img