Presentation is loading. Please wait.

Presentation is loading. Please wait.

Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.

Similar presentations


Presentation on theme: "Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order."— Presentation transcript:

1 Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order walk  Search Tree  Insert a element to the Tree  Delete a element form the Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 1

2 Graph  Directed graph (or digraph) G = (V, E) V: Set of vertex (node) E: Set of edges (ordered vertex pair)  Undirected graph G = (V, E) V: Set of vertex E: Set of edges (unordered vertex pair) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 2

3 Graph COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 3 A C B D Digraph G = (V, E) V = {A, B, C, D, E, F} E = {(A, B), (C, A), (B, D), (B, C), (C, D), (D, C), (B, B) (F, E)} E F

4 Graph COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 4 A C B D E F Undirected Graph G = (V, E) V = {A, B, C, D, E, F} E = {(A, B), (A, C), (B, D), (B, C), (E, F)}

5 Tree  A undirected graph is connected if every pair of vertices is connected by a path.  Free Tree – is a connected acyclic, undirected graph.  Forest – a acyclic but possibly disconnected undirected graph  Rooted tree – a free tree where one of the vertices is distinguished from the other. The distinguished vertex is called root. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 5

6 Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 6 Free Tree Forest

7 Tree  If the last edge on the path from from the root R of a tree T to a node is (y,x) then y is parent of x and x is child of y.  If two nodes have same parent, they are siblings  A node has no children is leaf.  A non-leaf node is an internal node  The length of the path from the root R to a node x is the depth of x in the tree.  The largest depth of any node in tree T is the height of tree T. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 7 R ACDB G F H E

8 Binary Tree  A binary tree is a tree data structure in which each node has at most two children.  Typically the child nodes are called left child and right child.  Binary trees are commonly used to implement binary search trees and heaps. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 8

9 Binary Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 9 Balanced Binary Tree

10 Binary Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 10 Unbalanced Binary Tree

11 Binary Search Tree  Binary search tree – a binary tree with binary-search-tree property.  Binary-Search-Tree Property Let x be a node in a binary search tree. If y is a node in the left subtree of x then key(y)  key(x). If y is a node in the right subtree of x, then key(x)  key(y). COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 11

12 Binary Search Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 12 5 7 28 14 9 41 163

13 Binary Search Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 13 2 13 7 9 8 Unbalanced Binary Tree

14 Operations on Binary Search Tree  Binary Search Tree Operations Inorder walk Preorder walk Postorder walk Search Tree Insert a element to the Tree Delete a element form the Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 14

15 Operations on Binary Search Tree (Inorder, Preorder, Postorder)  The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. Running Time T(n) = 2T(n/2) + 1 = O(n) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 15 Inorder_Tree_Walk(x) { 1If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3print x  key 4Inorder_Tree_Walk(x  rightchild); }

16 Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 16 Preorder_Tree_Walk(x) { 1If x  NIL { 2Print x  key; 3Inorder_Tree_Walk(x  leftchild); 4Inorder_Tree_Walk(x  rightchild); } Postorder_Tree_Walk(x) { 1If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3Inorder_Tree_Walk(x  rightchild); 4Print x  key; }

17 Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 17 5 7 28 14 9 41 163 1234578914 16 Inorder_Tree_Walk(x) { 1 If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3print x  key 4Inorder_Tree_Walk(x  rightchild); }

18 Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 18 5 7 28 14 9 41 163 7 21 4 3 58 14916 Preorder_Tree_Walk(x) { 1 If x  NIL { 2print x  key; 3Inorder_Tree_Walk(x  leftchild); 4Inorder_Tree_Walk(x  rightchild); }

19 Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 19 5 7 28 14 9 41 163 7 2 1 4 3 5 814 9 16 Postorder_Tree_Walk(x) { 1 If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3Inorder_Tree_Walk(x  rightchild); 4Print x  key; }

20 Operations on Binary Search Tree (Search an Element)  Searching a binary search tree for a specific value can be a recursive or iterative process.  We begin by examining the root node. If the tree is null, the value we are searching for does not exist in the tree. Otherwise, if the value equals the root, the search is successful. If the value is less than the root, search the left sub-tree.  If it is greater than the root, search the right subtree. This process is repeated until the value is found or the indicated sub-tree is null. If the searched value is not found before a null sub-tree is reached, then the item must not be present in the tree. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 20

21 Operations on Binary Search Tree (Search an Element) Searching Input: pointer to the root of the tree and a key k, Output: returns a pointer to a node with key k if one exists; otherwise return NIL. T(n) = T(n/2) + 1 = O(log n) if a binary tree is balanced T(n)= T(n-1) + 1 = O(n) if a binary tree is not balanced COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 21 Tree_Search (x, k) { 1if x == NIL or k == x  key return x; 2if k < x  key 3return Tree_Search (x  leftchild, k); 4else 5return Tree_Search(x  rightchild, k); }

22 Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 22 5 13 1014 11 12 3 Search (x, 14) x 14 = 5 or 14> 5 or 14 < 5 ?

23 Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 23 13 1014 11 12 Search (x, 14) x 14= 13 or 14> 13 or 14 < 13 ? 14= 14 or 14> 14 or 14 < 14 ?

24 Operations on Binary Search Tree (Search an Element)  Searching a binary search tree for a specific value can be a recursive or iterative process. Following shows the iterative version of tree search COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 24 Tree_Search_II(x, k) { 1y = x; //better not modify pointer x 2while y  NIL and k  y  key { 3if k < y  key 4y = y  leftchild; 5else 6y = y  rightchild; } 7return y; }

25 Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 25 15 5 16 13 1014 20 1823 11 12 3 Search (x, 14) x 14 = 5 or 14> 5 or 14 < 5 ?

26 Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 26 15 5 16 13 1014 20 1823 11 12 3 Search (x, 14) x 14 = 12 or 14> 12 or 14 < 12 ? 14 = 13 or 14> 13 or 14 < 13 ?

27 Operations on Binary Search Tree (Insert a New Node)  Insertion begins as a search would begin; if the root is not NIL, we search the left or right sub-trees as before.  Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 27

28 Operations on Binary Search Tree (Insert a New Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 28 // T is point to root of binary tree z is point to new node Tree_Insert (T, z) { 1y = NIL; 2x = T; //while loop find out the location for new node 3while x  NIL { 4y = x; 5if z  key < x  key 6x = x  leftchild; 7else 8x = x  rightchild; } 9z  parent = y; 10if y = NIL; // means there is any node in the BST 11T = z; //new node become root 12else if z  key < y  key 13y  leftchild = z; // insert new node as a left child o y 14else 15y  rightchild = z; //insert new node as a right child of y }

29 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 29 161458793241

30 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 30 16145893241 7

31 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 31 16145893241 7 2 7 ?

32 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 32 1614589341 7 2

33 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 33 1614589341 7 2 8 7 ?

34 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 34 161459341 7 28

35 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 35 161459341 7 28 14 7 ?

36 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 36 161459341 7 28 14 8 ?

37 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 37 1659341 7 28 14

38 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 38 1659341 7 28 14 9 7 ?

39 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 39 1659341 7 28 14 9 8 ?

40 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 40 1659341 7 28 14 9 14 ?

41 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 41 165341 7 28 14 9

42 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 42 165341 7 28 14 9 4 7 ?

43 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 43 165341 7 28 14 9 4 2 ?

44 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 44 16531 7 28 14 9 4

45 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 45 16531 7 28 14 9 4 1 7 ?

46 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 46 16531 7 28 14 9 4 1 2 ?

47 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 47 1653 7 28 14 9 41

48 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 48 1653 7 28 14 9 41 16 7 ?

49 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 49 1653 7 28 14 9 41 16 8 ?

50 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 50 1653 7 28 14 9 41 16 14 ?

51 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 51 53 7 28 14 9 41 16

52 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 52 53 7 28 14 9 41 16 3 7 ?

53 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 53 53 7 28 14 9 41 16 3 2 ?

54 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 54 53 7 28 14 9 41 16 3 4 ?

55 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 55 5 7 28 14 9 41 163

56 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 56 5 7 28 14 9 41 163 5 7 ?

57 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 57 5 7 28 14 9 41 163 5 2 ?

58 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 58 5 7 28 14 9 41 163 5 4 ?

59 Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park 59 5 7 28 14 9 41 163

60 Operations on Binary Search Tree (Minimum Element) Minimum  An element in a binary search tree whose key is a minimum can always be found by leftmost child. T(n) = O(log 2 n) or possible worst case O(n) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 60 Tree_Minimum(x) { 0y = x; 1while y  leftchild  NIL 2y = y  leftchild; 3return y; }

61 Operations on Binary Search Tree (Maximum Element) Maximum  An element in a binary search tree whose key is a maximum can always be founded by rightmost child. T(n) = O( log 2 n) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 61 Tree_Maximum(x) { 0y = x; 1while y  rightchild  NIL 2y = y  rightchild; 3return y; }

62 Operations on Binary Search Tree (Successor of a Node)  The successor of a node x is the node with the smallest key greater than x->key  We need to concern two cases 1. If the right subtree of x is not empty, then the successor of x is the minimum of right subtree. 2. If the right subtree of x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 62

63 Operations on Binary Search Tree (Successor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 63 15 6 7 18 20173 4213 9 Ex) successor of node 15 is 17 (min of right subtree) Ex) successor of node 13 is 15.

64 Operations on Binary Search Tree (Successor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 64 Tree_Successor (x) { 1if x  rightchild  NIL return Tree_Minimum(x  rightchild); 2else { 3y = x  parent; // if x is y’s leftchild then y is successor of x 4while y  NIL and x ==y  rightchild { 5x = y; 6y = y  parent; } 7return y; }

65 Operations on Binary Search Tree (Predecessor of a Node)  The predecessor of a node x is the node with the largest key smaller than key(x)  We need to concern two cases 1. If the left subtree of x is not empty, then the predecessor of x is the maximum of right subtree. 2. If the left subtree of x is empty and x has a predecessor y, then y is the lowest ancestor of x whose right child is also an ancestor of x. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 65

66 Operations on Binary Search Tree (Predecessor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 66 15 6 7 18 20173 4213 9 Ex) Predecessor of node 15 is 13 (Maximum of left subtree) Ex) Predecessor of node 9 is 7.

67 Operations on Binary Search Tree (Predecessor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 67 Tree_Predecessor (x) { 1if x  leftchild  NIL return Tree_Maximum(x  leftchild) 2else { 3y = x  parent // if x is y’s rightchild then y is predecessor of x 4while y  NIL and x ==y  leftchild { 5x = y 6y = y  parent } 7return y }

68 Operations on Binary Search Tree (Delete a Node)  There are three cases needed to be concern in binary tree deletion. 1. A deleting node has no children ( deleting node is a leaf) 2. A deleting node has one children. 3. A deleting node has two children. a) If successor of deleting node is a leaf b) If successor of deleting node is not a leaf COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 68

69 Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 69 Delete Leaf Node

70 Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 70 Delete a Node with one child

71 Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 71

72 Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 72

73 Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 73

74 Binary Search Tree (Delete a node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 74 Tree_Delete (T, z) { if z  leftchild == NIL or z  rightchild == NIL y = z else y = Tree_Successor(z) if y  leftchild  NIL x = y  leftchild else x = y  rightchild if x  NIL x  parent = y  parent if y  parent ==NIL T  root = x else if y == y  parent  left y  parent  leftchild = x else y  parent  rightchild = x if y  z z  key =y  key delete y }


Download ppt "Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order."

Similar presentations


Ads by Google