Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.

Similar presentations


Presentation on theme: "1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees."— Presentation transcript:

1 1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees

2 1.2 Data Structure and Algorithm subtree Trees Node Edge parent root leaf interior node child path Degree? Depth/Level? Height?

3 1.3 Data Structure and Algorithm Binary Tree Representation Node  data  left child  right child  parent (optional) Parent Data Left Right Tree root

4 1.4 Data Structure and Algorithm 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 2 6 1 7 4 5 3 2 6 1 7 4 5 Full Binary Tree Binary Tree

5 1.5 Data Structure and Algorithm Complete Binary Tree  All leaves have the same depth  All interior nodes have degree 2 depth 0 depth 1 depth 2 depth 3 height=3

6 1.6 Data Structure and Algorithm Complete Binary Tree The number of internal nodes of a complete binary tree of height h is: 1 + 2 1 + 2 2 +……2 h-1 = 2 h -1/2-1 = 2 h -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?

7 1.7 Data Structure and Algorithm Applications - Expression Trees 5384 - + * (5*3)+(8-4) To represent infix expressions

8 1.8 Data Structure and Algorithm Applications - Parse Trees Used in compilers to check syntax statement ifcondthen statementelsestatement ifcondthen statement

9 1.9 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)

10 1.10 Data Structure and Algorithm Binary Search Trees BST property: key[leftSubtree(x)]  key[x]  key[rightSubtree(x)] Example: F BH KDA

11 1.11 Data Structure and Algorithm Traversals for a Binary Tree 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

12 1.12 Data Structure and Algorithm Traversal Examples A B D G C E H F 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

13 1.13 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?

14 1.14 Data Structure and Algorithm Inorder Tree Walk n in-order(x) in-order (x.left); print(x); in-order(x.right); Prints elements in sorted (increasing) order

15 1.15 Data Structure and Algorithm Postorder Tree Walk n post-order(x) print(x) post-order (x.left); post-order(x.right);

16 1.16 Data Structure and Algorithm Evaluating an expression tree Walk the tree in postorder When visiting a node, use the results of its children to evaluate it. 5384 - + *

17 1.17 Data Structure and Algorithm BST Operations Search  Key  Minimum  Maximum  Successor  Predecessor Insert node Delete node

18 1.18 Data Structure and Algorithm BST Search: Key Search for D and C: F BH KDA

19 1.19 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(lg 2 n) Where height, h=lg 2 n

20 1.20 Data Structure and Algorithm BST Search: Minimum TreeMinimum(x)  While left[x] <> NIL do  x = left[x]  return x

21 1.21 Data Structure and Algorithm BST Search: Maximum TreeMaximum(x)  While right[x] <> NIL do  x = right[x]  return x

22 1.22 Data Structure and Algorithm BST Search: Successor Inorder traversal: 2 3 4 6 7 9 13 15 17 18 20 18 2017 3 42 6 7 13 9 15 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

23 1.23 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

24 1.24 Data Structure and Algorithm BST: Insert Node Example: Insert C F BH KDA C

25 1.25 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(lg 2 n) Where height, h=lg 2 n

26 1.26 Data Structure and Algorithm BST: Delete Node 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 F BH KDA C Example: delete K or H or B


Download ppt "1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees."

Similar presentations


Ads by Google