




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





new Array(n)
new ListNode()
new DoublyLinkedListNode()
new BinaryTreeNode()
key
other data
pointer = (array, index)
key
other data
pointer
next
key
other data
pointer
prev next
key
other data
pointer
left right
parent
Inorder-Traverse(node, function) if node ≠ nil then Inorder-Traverse(left[node], function) function(node) Inorder-Traverse(right[node], function)
BST-Insert(tree, node) previous <- nil current <- root[tree] while current ≠ nil do previous <- current if key[node] < key[current] then current <- left[current] else current <- right[current] {current is now nil} parent[node] <- previous if previous = nil then root(tree) <- node else if key[node] < key[previous] then left[previous] <- node else right[previous] <- node
{Different from CLR version. Assumes there is a dummy header node at root.} BST-Delete(tree, node) if (left[node] = nil) or (right[node] = nil) {Case 1: at least one child of node is nil} then if node = left[parent[node]] then left[parent[node]] <- Single-Child(node) else right[parent[node]] <- Single-Child(node) {Case 2: both children of node are non-nil.} else succ <- BST-Successor(tree, node) {Delete succ from current position. Guaranteed to use Case 1.} BST-delete(tree, succ) {Splice succ into position of node} parent[left[node]] <- succ left[succ] <- left[node] parent[right[node]] <- succ right[succ] <- right[node] parent[succ] <- parent[node] if node = left[parent[node]] then left[parent[node]] <- succ else right[parent[node]] <- succ
{If one child is nil, returns the other one. Returns nil if both children are nil.} Single-Child(node) if left[node] = nil then return right[node] else return left[node]