260 likes | 385 Views
Lecture 12. Binary Search Trees. Topics. Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees. Trees. Edge. interior node. path. Node. subtree. leaf. child. root. Degree?. parent. Depth/Level?. Height?. Binary Tree Representation. Parent. Data.
E N D
Lecture 12 • Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees Data Structure and Algorithm
Trees Edge interior node path Node subtree leaf child root Degree? parent Depth/Level? Height? Data Structure and Algorithm
Binary Tree Representation Parent Data Left Right root • Tree • root • Node • data • left child • right child • parent (optional) Data Structure and Algorithm
Full Binary Tree 3 7 2 4 5 1 6 • Full Binary Tree • Replace each missing child in the binary tree with a node having no children (black nodes) which are called leaf nodes. • Each node is either a leaf or has degree exactly 2 • All leaves are at level h (height) • All interior nodes are full 3 7 2 4 5 1 6 Binary Tree Full Binary Tree Data Structure and Algorithm
Complete Binary Tree • Complete Binary Tree • All leaves have the same depth • All interior nodes have degree 2 depth 0 depth 1 height=3 depth 2 depth 3 Data Structure and Algorithm
Complete Binary Tree • The number of internal nodes of a complete binary tree of height h is: 1 + 21 + 22 +……2h-1 = 2h -1/2-1 = 2h -1 • What is the number of total nodes in a complete binary tree of height h? • What is the number of leaf nodes in a complete binary tree of height h? Data Structure and Algorithm
Applications - Expression Trees + * - 5 3 8 4 To represent infix expressions (5*3)+(8-4) Data Structure and Algorithm
Applications - Parse Trees Used in compilers to check syntax statement statement else statement if cond then statement if cond then Data Structure and Algorithm
Binary Search Trees • Binary Search Trees (BSTs) are an important data structure for dynamic sets • In addition to data, elements have: • key: an identifying field inducing a total ordering • left: pointer to a left child (may be NULL) • right: pointer to a right child (may be NULL) • p: pointer to a parent node (NULL for root) Data Structure and Algorithm
Binary Search Trees F B H A D K • BST property: key[leftSubtree(x)] key[x] key[rightSubtree(x)] • Example: Data Structure and Algorithm
Pre order visit the node go left go right In order go left visit the node go right Post order go left go right visit the node Traversals for a Binary Tree Data Structure and Algorithm
Traversal Examples A B C D E F G H I Pre order A B D G H C E F I In order G D H B A E C F I Post order G H D B E I F C A Data Structure and Algorithm
Traversal Implementation • recursive implementation of preorder • pre-order ( node ) • visit node • pre-order ( node.left ) • pre-order ( node.right ) • What changes need to be made for in-order, post-order? Data Structure and Algorithm
Inorder Tree Walk • in-order(x) in-order (x.left); print(x); in-order(x.right); • Prints elements in sorted (increasing) order Data Structure and Algorithm
Postorder Tree Walk • post-order(x) print(x) post-order (x.left); post-order(x.right); Data Structure and Algorithm
Evaluating an expression tree + * - 5 3 8 4 • Walk the tree in postorder • When visiting a node, use the results of its children to evaluate it. Data Structure and Algorithm
BST Operations • Search • Key • Minimum • Maximum • Successor • Predecessor • Insert node • Delete node Data Structure and Algorithm
BST Search: Key F B H A D K • Search for D and C: Data Structure and Algorithm
BST Search: Key • Given a key and a pointer to a node, returns an element with that key or NULL: TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k); Cost: O(lg2n) Where height, h=lg2n Data Structure and Algorithm
BST Search: Minimum • TreeMinimum(x) • While left[x] <> NIL do • x = left[x] • return x Data Structure and Algorithm
BST Search: Maximum • TreeMaximum(x) • While right[x] <> NIL do • x = right[x] • return x Data Structure and Algorithm
BST Search: Successor 15 18 6 3 17 20 7 2 4 13 9 • Inorder traversal: 2 3 4 6 7 9 13 15 17 18 20 Case1:x has a right subtree successor is minimum node in right subtree Case2: x has no right subtree successor is first ancestor of x whose left child is also ancestor of x Data Structure and Algorithm
BST Search: Successor • if right[x] <> NIL then • return TreeMinimum (right[x]) • y = p[x] • while y <> NIL and x = right[y] do • x = y • y = p[y] • return y Predecessor: similar algorithm Data Structure and Algorithm
BST: Insert Node F B H A D K • Example: Insert C C Data Structure and Algorithm
BST: Insert Node • Adds an element x to the tree so that the binary search tree property continues to hold • TreeInsert (T, z) • y = NIL • x = root[T] • While x <> NILL 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 Cost: O(lg2n) Where height, h=lg2n Data Structure and Algorithm
BST: Delete Node F Example: delete Kor H or B B H C A D K • Deletion is a bit tricky • 3 cases: • x has no children: • Remove x • x has one child: • Splice out x • x has two children: • Swap x with successor • Perform case 1 or 2 to delete it Data Structure and Algorithm