Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:

Similar presentations


Presentation on theme: "Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:"— Presentation transcript:

1 Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源: http://www.csie.ntu.edu.tw/~ds/

2 2 Binary Search Tree Binary search tree – Every element has a unique key –The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree –The left and right subtrees are also binary search trees A recursive definition! 20 12 25 10 15 30 5 40 2 60 70 65 80 22

3 3 Searching a Binary Search Tree tree_pointer search(tree_pointer root, int key) { /* return a pointer to the node that contains key. If there is no such node, return NULL */ if (!root) return NULL; if (key == root->data) return root; if (key data) return search(root->left_child, key); return search(root->right_child,key); } A Recursive Version Each tree node: - A key value (data) - Two pointers to it left and right children Complexity: O(h)

4 4 Searching a Binary Search Tree tree_pointer search2(tree_pointer tree, int key) { while (tree) { if (key == tree->data) return tree; if (key data) tree = tree->left_child; else tree = tree->right_child; } return NULL; } A Iterative Version

5 5 Searching a Binary Search Tree Search by rank permitted if –Each node have an additional field LeftSize which is 1 plus the number of elements in the left subtree of the node 30 5 40 2 1 2 3 1 tree_pointer search3(tree_pointer tree, int rank) { while (tree) { if (rank == tree->LeftSize) return tree; if (rank LeftSize) tree = tree->left_child; else { rank-= tree->LeftSize; tree = tree->right_child; } return NULL; }

6 6 Insert Node in Binary Search Tree 30 5 40 2 30 5 40 2 35 80 30 5 40 2 80 Insert 80 Insert 35

7 7 Insertion into A Binary Search Tree void insert_node(tree_pointer *node, int num) {tree_pointer ptr, temp = modified_search(*node, num); if (temp || !(*node)) { ptr = (tree_pointer) malloc(sizeof(node)); if (IS_FULL(ptr)) { fprintf(stderr, “The memory is full\n”); exit(1); } ptr->data = num; ptr->left_child = ptr->right_child = NULL; if (*node) if (num data) temp->left_child=ptr; else temp->right_child = ptr; else *node = ptr; } // Not NULL // Return last visited tree node

8 8 Deletion for A Binary Search Tree Delete a leaf node –The left (or right) child field of its parent is set to 0 and the node is simply disposed 30 5 40 2 35 80 30 5 40 2 80 Delete 35 Delete 80 30 5 40 2

9 9 Deletion for A Binary Search Tree Delete a nonleaf node with only one child –The node is deleted and the single-child takes the place of the disposed node 30 5 40 2 35 80 Delete 5 30 40 2 35 80

10 10 Deletion for A Binary Search Tree Delete a nonleaf node with only one child 2 T1 T2 1 X 1 T1

11 11 Deletion for A Binary Search Tree Delete a nonleaf node that has two children –The largest element in its left subtree or the smallest one in the right tree will replace the disposed mode 30 5 40 2 35 80 Delete 30 5 40 2 35 80 or 40 2 35 80

12 12 Deletion for A Binary Search Tree Delete a nonleaf node that has two children –Recursive adjustments might happen 40 20 60 10 3050 70 45 55 52 40 20 55 10 3050 70 45 52 Before deleting 60 After deleting 60

13 13 Exercise Implement a binary search which is equipped with –Insertion operation –Search operation Search by key Search by Rank –Deletion operation

14 14 Trie A D C B E F G H I JK Tree A B C G H D K J E F I Trie struct DEF_TREE { int Key; struct Tree *Child; struct Tree *Brother; struct Tree *Father; };

15 15 Graphs Berlin Chen 陳柏琳 台灣師範大學資工所 助理教授 參考資料來源: http://www.csie.ntu.edu.tw/~ds/

16 16 Definition A graph G consists of two sets –A finite, nonempty set of vertices V(G) –A finite, possible empty set of edges E(G) –G(V,E) represents a graph An undirected graph (graph) is one in which the pair of vertices in a edge is unordered, (v 0, v 1 ) = (v 1,v 0 ) A directed graph (digraph) is one in which each edge is a directed pair of vertices, != tail head

17 17 Examples for Graph 0 12 3 0 1 2 0 12 3 4 5 6 G1G1 G2G2 G3G3 V(G 1 )={0,1,2,3} E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G 2 )={0,1,2,3,4,5,6} E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G 3 )={0,1,2} E(G 3 )={,, } complete undirected graph: n(n-1)/2 edges complete directed graph: n(n-1) edges complete graph incomplete graph

18 18 Complete Graph A complete graph is a graph that has the maximum number of edges –For undirected graph with n vertices, the maximum number of edges is n(n-1)/2 –For directed graph with n vertices, the maximum number of edges is n(n-1) –Example: G1 is a complete graph 0 12 3 G1G1 complete graph

19 19 Adjacent and Incident If (v 0, v 1 ) is an edge in an undirected graph –v 0 and v 1 are adjacent –The edge (v 0, v 1 ) is incident on vertices v 0 and v 1 If is an edge in a directed graph –v 0 is adjacent to v 1, and v 1 is adjacent from v 0 –The edge is incident on v 0 and v 1 0 1 2 G3G3 0 12 3 4 5 6 G2G2

20 20 Loop and Multigraph Example of a graph with feedback loops and a multigraph (Figure 6.3) –Edges of the form (v, v) or : self edge or self loop 02 1 (a) 2 1 0 3 (b) self edge multigraph: multiple occurrences of the same edge

21 21 Subgraph and Path A subgraph of G is a graph G’ such that V(G’) is a subset of V(G) and E(G’) is a subset of E(G) A path from vertex v p to vertex v q in a graph G, is a sequence of vertices, v p, v i 1, v i 2,..., v i n, v q, such that (v p, v i 1 ), (v i 1, v i 2 ),..., (v i n, v q ) are edges in an undirected graph The length of a path is the number of edges on it

22 22 00 123 120 12 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G 1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G 3 分開 單一 0 12 3 G1G1 0 1 2 G3G3 Figure 6.4: subgraphs of G 1 and G 3

23 23 Simple Path and Cycle A simple path is a path in which all vertices except possibly the first and the last are distinct A cycle is a simple path in which the first and the last vertices are the same In an undirected graph G, two vertices, v 0 and v 1, are connected if there is a path in G from v 0 to v 1 An undirected graph is connected if, for every pair of distinct vertices v i, v j, there is a path from v i to v j

24 24 0 12 3 0 12 3 4 5 6 G1G1 G2G2 connected tree (acyclic graph)

25 25 Connected Component A connected component of an undirected graph is a maximal connected subgraph A tree is a graph that is connected and acyclic A directed graph is strongly connected if there is a directed path from v i to v j and also from v j to v i A strongly connected component is a maximal subgraph that is strongly connected

26 26 Figure 6.5: A graph with two connected components 1 0 2 3 4 5 6 7 H1H1 H2H2 G 4 (not connected) connected component (maximal connected subgraph)

27 27 Figure 6.6: Strongly connected components of G 3 0 1 2 0 1 2 G3G3 not strongly connected strongly connected component (maximal strongly connected subgraph)

28 28 Degree The degree of a vertex is the number of edges incident to that vertex For directed graph, –the in-degree of a vertex v is the number of edges that have v as the head –the out-degree of a vertex v is the number of edges that have v as the tail –if d i is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is

29 29 undirected graph degree 0 12 3 4 5 6 G1G1 G2G2 3 2 33 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 12 3 3 3 3

30 30 ADT for Undirected Graph structure Graph is objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices functions: for all graph  Graph, v, v 1 and v 2  Vertices Graph Create()::=return an empty graph Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no incident edge Graph InsertEdge(graph, v 1,v 2 )::= return a graph with new edge between v 1 and v 2 Graph DeleteVertex(graph, v)::= return a graph in which v and all edges incident to it are removed Graph DeleteEdge(graph, v 1, v 2 )::=return a graph in which the edge (v 1, v 2 ) is removed Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE else return FALSE List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v

31 31 Graph Representations Adjacency Matrix Adjacency Lists Adjacency Multilists

32 32 Adjacency Matrix Let G=(V,E) be a graph with n vertices. The adjacency matrix of G is a two-dimensional n by n array, say adj_mat If the edge (v i, v j ) is in E(G), adj_mat[i][j]=1 If there is no such edge in E(G), adj_mat[i][j]=0 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric

33 33 Examples for Adjacency Matrix G1G1 G2G2 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 symmetric undirected: n 2 /2 directed: n 2

34 34 Merits of Adjacency Matrix From the adjacency matrix, to determine the connection of vertices is easy The degree of a vertex is For a digraph, the row sum is the out_degree, while the column sum is the in_degree sum up a column sum up a row

35 35 Data Structures for Adjacency Lists #define MAX_VERTICES 50 typedef struct node *node_pointer; typedef struct node { int vertex; struct node *link; }; node_pointer graph[MAX_VERTICES]; int n=0; /* vertices currently in use */ Each row in adjacency matrix is represented as an adjacency list

36 36 01230123 012012 0123456701234567 1 23 023 013 0 12 G1G1 1 0 2 G3G3 1 2 0 3 0 3 12 5 4 6 57 6 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

37 37 Interesting Operations Degree of a vertex in an undirected graph –Number of nodes in adjacency list Number of edges in a graph –Determined in O(n+e) Out-degree of a vertex in a directed graph (digraph) –Number of nodes in its adjacency list In-degree of a vertex in a directed graph (digraph) –Traverse the whole data structure –Or maintained in inversed adjacency lists

38 38 Compact Representation 1 0 2 3 4 5 6 7 0 1 2 3 4 5 6 7 node[0] … node[n-1]: starting point for vertices node[n]: n+2e+1 (upper bound of the array) The vertices adjacent from vertex i are stored in array indexed from node[i] to node[i+1]-1

39 39 Inverse Adjacency Lists  012012 1 NULL 0 NULL 1 NULL 0 1 2 Determine in-degree of a vertex in a fast way. Figure 6.10: Inverse adjacency lists for G 3

40 40 A simplified version of the list structure for sparse matrix representation tail head column link for head row link for tail Figure 6.11: Alternate node structure for adjacency lists Information for an edge for in-degree information for out-degree information

41 41 0 1 2 2 NULL 1 0 1 0 NULL 0 1 NULL NULL 1 2 NULL NULL 0 1 2 Figure 6.12: Orthogonal representation for graph G 3 head nodes (shown twice)

42 42  3  2 NULL 1  0  2  3 NULL 0  1  3  1 NULL 0  2  2  0 NULL 1  3 headnodes vertax link Order is of no significance 0 12 3 Figure 6.13:Alternate order adjacency list for G 1

43 43 Exercise P. 344 Exercises 2, 10

44 44 Adjacency Multilists An edge in an undirected graph is represented by two nodes in adjacency list representation. Adjacency Multilists –lists in which nodes may be shared among several lists (an edge is shared by two different paths) m vertex1 vertex2 list1 list2 A Boolean mark indicating whether or not the edge has been examined

45 45 Example for Adjacency Multlists Lists: vertex 0: N0->N1->N2 vertex 1: N0->N3->N4 vertex 2: N1->N3->N5 vertex 3: N2->N4->N5 0 1 N1 N3 0 2 N2 N3 0 3 N4 1 2 N4 N5 1 3 N5 2 3 N0 N1 N2 N3 N4 N5 01230123 edge (0,1) edge (0,2) edge (0,3) edge (1,2) edge (1,3) edge (2,3) (1,0) (2,0) (3,0) (2,1) (3,1) (3,2) 0 12 3 six edges

46 46 Some Graph Operations Traversal Given G=(V,E) and vertex v, find all w  V, such that w connects v –Depth First Search (DFS) preorder tree traversal –Breadth First Search (BFS) level order tree traversal Connected Components Spanning Trees

47 47 Example: Graph G and its adjacency lists depth first search: v0, v1, v3, v7, v4, v5, v2, v6 breadth first search: v0, v1, v2, v3, v4, v5, v6, v7 Figure 6.16: visit all vertices in G that are reachable form v0

48 48 Depth First Search (DFS, dfs) void dfs(int v) { node_pointer w; visited[v]= TRUE; printf(“%5d”, v); for (w=graph[v]; w; w=w->link) if (!visited[w->vertex]) dfs(w->vertex); } #define FALSE 0 #define TRUE 1 short int visited[MAX_VERTICES]; Data structure adjacency list: O(e) adjacency matrix: O(n 2 ) A Recursive Function

49 49 Breadth First Search (BFS, bfs) typedef struct queue *queue_pointer; typedef struct queue { int vertex; queue_pointer link; }; void addq(queue_pointer *, queue_pointer *, int); int deleteq(queue_pointer *);

50 50 Breadth First Search void bfs(int v) { node_pointer w; queue_pointer front, rear; front = rear = NULL; printf(“%5d”, v); visited[v] = TRUE; addq(&front, &rear, v); adjacency list: O(e) adjacency matrix: O(n 2 ) while (front) { v= deleteq(&front); for (w=graph[v]; w; w=w->link) if (!visited[w->vertex]) { printf(“%5d”, w->vertex); addq(&front, &rear, w->vertex); visited[w->vertex] = TRUE; }

51 51 Connected Components void connected(void) { for (i=0; i<n; i++) visited[i]=0; for (i=0; i<n; i++) { if (!visited[i]) { dfs(i); printf(“\n”); } adjacency list: O(n+e) adjacency matrix: O(n 2 )

52 52 Exercise Implement DFS and BFS Implement a function to determine the connected components for a graph 0 12 3 4 5 6 7 1 0 2 3 4 5 6 7

53 53 Spanning Trees When graph G is connected, a depth first or breadth first search starting at any vertex will visit all vertices in G –Form a tree that includes all the vertices of G A spanning tree is any tree that consists solely of edges in G and that includes all the vertices E(G): T (tree edges) + N (nontree edges) where T: set of edges used during search N: set of remaining edges

54 54 Examples of Spanning Tree 0 12 3 0 12 3 0 12 3 0 12 3 G1G1 Possible spanning trees

55 55 Spanning Trees Either DFS or BFS can be used to create a spanning tree –When DFS is used, the resulting spanning tree is known as a depth first spanning tree –When BFS is used, the resulting spanning tree is known as a breadth first spanning tree While adding a nontree edge into any spanning tree, this will create a cycle

56 56 DFS VS. BFS Spanning Tree 0 12 3 4 5 6 7 0 12 3 4 5 6 7 DFS Spanning BFS Spanning 0 12 3 4 5 6 7 nontree edge cycle

57 57 Biconnected Graph A spanning tree is a minimal subgraph, G’, of G such that V(G’)=V(G) and G’ is connected Any connected graph with n vertices must have at least n-1 edges A biconnected graph is a connected graph that has no articulation points 0 12 3 4 5 6 7 biconnected graph

58 58 Articulation Point A vertex v is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph has at least two connected components 0 12 3 4 5 6

59 59 biconnected component: a maximal connected subgraph H (no subgraph that is both biconnected and properly contains H) 1 3 5 6 0 8 9 7 2 4 1 0 1 3 2 4 3 5 8 7 9 7 5 6 7 biconnected components Two biconnected components can at most have one vertex at common

60 60 Find biconnected component of a connected undirected graph by depth first spanning tree 8 9 1 4 0 3 0 5 3 5 4 6 1 6 9 8 7 7 3 4 5 7 6 98 2 0 1 0 6767 1 5 2424 3 (a) depth first spanning tree (b) 2 2 dfn depth first number nontree edge (back edge) nontree edge (back edge) If u is an ancestor of v then dfn(u) < dfn(v)

61 61 Minimum Cost Spanning Tree The cost of a spanning tree of a weighted undirected graph is the sum of the costs of the edges in the spanning tree A minimum cost spanning tree is a spanning tree of least cost Three different algorithms can be used –Kruskal –Prim –Sollin Select n-1 edges from a weighted graph of n vertices with minimum cost.

62 62 Greedy Strategy An optimal solution is constructed in stages At each stage, the best decision is made at this time Since this decision cannot be changed later, we make sure that the decision will result in a feasible solution Typically, the selection of an item at each stage is based on a least cost or a highest profit criterion

63 63 Kruskal’s Idea Build a minimum cost spanning tree T by adding edges to T one at a time Select the edges for inclusion in T in nondecreasing order of the cost An edge is added to T if it does not form a cycle Since G is connected and has n > 0 vertices, exactly n-1 edges will be selected

64 64 Examples for Kruskal’s Algorithm 0 1 2 3 4 5 6 0 1 2 3 4 5 6 28 16 12 18 24 22 25 10 14 0 1 2 3 4 5 6 10 0 5 2 3 1 6 1 2 3 6 3 4 4 6 4 5 0 1 10 12 14 16 18 22 24 25 28 6/9

65 65 Examples for Kruskal’s Algorithm 0 1 2 3 4 5 6 10 12 0 1 2 3 4 5 6 10 12 14 0 1 2 3 4 5 6 10 12 1416 0 5 2 3 1 6 1 2 3 6 3 4 4 6 4 5 0 1 10 12 14 16 18 22 24 25 28 + 3 6 cycle

66 66 Examples for Kruskal’s Algorithm 0 5 2 3 1 6 1 2 3 6 3 4 4 6 4 5 0 1 10 12 14 16 18 22 24 25 28 0 1 2 3 4 5 6 10 12 1416 22 0 1 2 3 4 5 6 10 12 1416 22 25 4 6 cycle + cost = 10 +25+22+12+16+14

67 67 Kruskal’s Algorithm T= {}; while (T contains less than n-1 edges && E is not empty) { choose a least cost edge (v,w) from E; delete (v,w) from E; if ((v,w) does not create a cycle in T) add (v,w) to T else discard (v,w); } if (T contains fewer than n-1 edges) printf(“No spanning tree\n”);

68 68 Exercise The missionaries and cannibals problem is usually stated as follows. Three missionaries and three cannibals are on one side of a river, along with a boat that can hold one or two people. Find a way to get every one to the other side, without leaving a group of missionaries in one place outnumbered by the cannibals in that place.


Download ppt "Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:"

Similar presentations


Ads by Google