Balanced Binary Search Trees Balanced Binary Search Trees • height is O(log n), where n is the • Indexed AVL trees number of elements in the tree • Indexed red-black trees • AVL (Adelson-Velsky and Landis) • Indexed operations also take trees O(log n) time • red-black trees • get, put, and remove take O(log n) time AVL Tree Balanced Search Trees • weight balanced binary search trees • binary tree • 2-3 & 2-3-4 trees • for every node x, define its balance factor • AA trees balance factor of x = height of left subtree of x • B-trees - height of right subtree of x • balance factor of every node x is -1, 0, or 1 • BBST • etc. Height Balance Factors -1 The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). 1 1 -1 0 1 0 The height of every n node binary tree is at 0 -1 0 0 0 least log 2 (n+1). 0 This is an AVL tree.
AVL Search Tree put(9) -1 -1 10 10 1 1 0 1 1 7 40 7 40 -1 -1 0 1 0 1 -1 0 0 45 45 3 8 30 3 8 30 0 0 -1 0 -1 0 0 0 0 0 9 0 60 60 35 35 1 20 1 20 5 5 0 0 25 25 put(29) put(29) -1 -1 10 10 1 1 1 1 7 40 7 40 -1 -1 0 1 0 1 0 0 45 45 3 8 30 3 8 30 0 0 -1 0 0 0 0 0 -2 0 0 60 60 35 35 1 20 1 25 5 5 0 0 -1 0 RR imbalance => new node is in 25 20 29 0 right subtree of right subtree of RR rotation. 29 blue node (node with bf = -2) AVL Rotations Red Black Trees Colored Nodes Definition • RR • Binary search tree. • LL • Each node is colored red or black. • RL • Root and all external nodes are black. • LR • No root-to-external-node path has two consecutive red nodes. • All root-to-external-node paths have the same number of black nodes
Red Black Trees Example Red Black Tree 10 Colored Edges Definition • Binary search tree. 7 40 • Child pointers are colored red or black. 45 • Pointer to an external node is black. 3 8 30 • No root to external node path has two 60 35 consecutive red pointers. 1 20 5 • Every root to external node path has the 25 same number of black pointers. Red Black Tree Example Red Black Tree 10 • The height of a red black tree that has n (internal) nodes is between log 2 (n+1) and 2log 2 (n+1). 7 40 • java.util.TreeMap => red black tree 45 3 8 30 60 35 1 20 5 25 Graphs Graphs • G = (V,E) • Undirected edge has no orientation (u,v). • V is the vertex set. u v • Vertices are also called nodes and points. • E is the edge set. • Undirected graph => no oriented edge. • Each edge connects two different vertices. • Directed graph => every edge has an • Edges are also called arcs and lines. orientation. • Directed edge has an orientation (u,v). u v
Undirected Graph Directed Graph (Digraph) 2 2 3 3 8 8 1 1 10 10 4 4 5 5 9 9 11 11 6 6 7 7 Driving Distance/Time Map Applications—Communication Network 2 2 3 4 3 8 8 8 1 1 10 6 10 2 4 5 4 4 4 3 5 5 9 9 11 11 5 6 7 6 6 7 7 • Vertex = city, edge = communication link. • Vertex = city, edge weight = driving distance/time. Street Map Complete Undirected Graph 2 3 Has all possible edges. 8 1 10 4 5 9 11 n = 4 n = 1 n = 2 n = 3 6 7 • Some streets are one way.
Number Of Edges—Undirected Graph Number Of Edges—Directed Graph • Each edge is of the form (u,v), u != v. • Each edge is of the form (u,v), u != v. • Number of such pairs in an n vertex graph is • Number of such pairs in an n vertex graph is n(n-1). n(n-1). • Since edge (u,v) is the same as edge (v,u), • Since edge (u,v) is not the same as edge the number of edges in a complete (v,u), the number of edges in a complete undirected graph is n(n-1)/2. directed graph is n(n-1). • Number of edges in an undirected graph is • Number of edges in a directed graph is <= <= n(n-1)/2. n(n-1). Vertex Degree Sum Of Vertex Degrees 2 3 8 8 1 10 10 4 5 9 9 11 11 6 7 Number of edges incident to vertex. Sum of degrees = 2e (e is number of edges) degree(2) = 2, degree(5) = 3, degree(3) = 1 In-Degree Of A Vertex Out-Degree Of A Vertex 2 2 3 3 8 8 1 1 10 10 4 4 5 5 9 9 11 11 6 6 7 7 in-degree is number of incoming edges out-degree is number of outbound edges indegree(2) = 1, indegree(8) = 0 outdegree(2) = 1, outdegree(8) = 2
Sum Of In- And Out-Degrees each edge contributes 1 to the in-degree of some vertex and 1 to the out-degree of some other vertex sum of in-degrees = sum of out-degrees = e, where e is the number of edges in the digraph
Recommend
More recommend