Graphs and Search Trees Instructor Mainak Chaudhuri mainakccse




![Building a graph class Graph. Builder { public static void main (String a[]) throws Building a graph class Graph. Builder { public static void main (String a[]) throws](https://slidetodoc.com/presentation_image_h/f86985df72533bcf2aa23841ce23d0be/image-5.jpg)













![Binary search trees class BSTBuilder { public static void main (String a[]) throws java. Binary search trees class BSTBuilder { public static void main (String a[]) throws java.](https://slidetodoc.com/presentation_image_h/f86985df72533bcf2aa23841ce23d0be/image-19.jpg)


- Slides: 21
Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse. iitk. ac. in 1
Building a graph class Vertex. List { private int vertex. Id; private Vertex. List next; public Vertex. List (int x) { vertex. Id = x; next = null; } // next slide 2
Building a graph public Vertex. List Add (int x) { // Add at the front Vertex. List new. Head = new Vertex. List (x); new. Head. Set. Next(this); return new. Head; } public void Set. Next (Vertex. List vl) { next = vl; } 3 // next slide
Building a graph public void Print () { if (next==null) { System. out. println (vertex. Id); } else { System. out. print (vertex. Id + “, ”); next. Print(); } } } // end class 4
Building a graph class Graph. Builder { public static void main (String a[]) throws java. io. IOException { // Array of vertex lists is a graph Vertex. List graph[]; char c; boolean directed = false; int vertex 1, vertex 2, n, i; My. Input inp = new My. Input(); 5 // next slide
Building a graph System. out. print (“Enter number of vertices: ”); n = inp. Read. Int(); graph = new Vertex. List[n]; for (i=0; i<n; i++) { graph[i] = null; } // next slide 6
Building a graph System. out. print (“Directed or undirected? (d/u)”); c = inp. Read. Char(); if (c==‘d’) { directed = true; } c = inp. Read. Char(); // eat the line feed while (true) { System. out. print (“Enter an edge as two vertex ids: ”); // next slide 7
Building a graph vertex 1 = inp. Read. Int(); vertex 2 = inp. Read. Int(); if (graph[vertex 1] != null) { graph[vertex 1] = graph[vertex 1]. Add (vertex 2); } else { graph[vertex 1] = new Vertex. List (vertex 2); } 8 // next slide
Building a graph if (directed==false) { // Need to add vertex 2 ->vertex 1 if (graph[vertex 2] != null) { graph[vertex 2] = graph[vertex 2]. Add (vertex 1); } else { graph[vertex 2] = new Vertex. List (vertex 1); } } 9 // next slide
Building a graph System. out. print (“More edges? (y/n)”); c = inp. Read. Char(); if (c==‘n’) { break; } else { c = inp. Read. Char(); // line feed } } // end of while 10 // next slide
Building a graph // Print out the input graph for (i=0; i<n; i++) { System. out. println (“Neighbours of vertex ” + i + “: ”); graph[i]. Print(); } } // end main } // end class 11
Binary search trees • Recall that on n vertices, the minimum depth is O(log n) – You can achieve this if you have a balanced binary search tree (this is possible to ensure in O(log n) time, but we will not discuss) – The maximum depth is still O(n) • Example: insertion in sorted order • Worst case search time (i. e. the number of comparisons) in a binary search tree is equal to the depth of the tree – This is O(log n) for nearly balanced trees • Next few slides builds a binary search tree, searches in a binary search tree, and prints 12 the values in sorted order
Binary search trees class BSTVertex { private int value; private BSTVertex left; private BSTVertex right; public BSTVertex (int x) { value = x; left = null; right = null; } // next slide 13
Binary search trees public void Insert (int x) { // this points to root of a subtree if (x <= value) { if (left == null) { left = new BSTVertex (x); } else { left. Insert (x); } } // next slide 14
Binary search trees else { if (right == null) { right = new BSTVertex (x); } else { right. Insert (x); } } } // next slide 15
Binary search trees public boolean Search (int x) { if (x == value) { return true; } else if (x < value) { if (left == null) { return false; } else { return left. Search (x); } } // next slide 16
Binary search trees else { if (right == null) { return false; } else { return right. Search (x); } } } // next slide 17
Binary search trees public void Print. Sorted () { // Known as in-order traversal // Prints in ascending order of values if (left != null) { left. Print. Sorted (); } System. out. print (value + “ ”); if (right != null) { right. Print. Sorted (); } } } // end class 18
Binary search trees class BSTBuilder { public static void main (String a[]) throws java. io. IOException { BSTVertex root = null; int x; char c; My. Input inp = new My. Input (); while (true) { System. out. print (“Enter the next value: ”); x = inp. Read. Int(); 19
Binary search trees if (root != null) { root. Insert (x); } else { root = new BSTVertex (x); } System. out. print (“More values? (y/n)”); c = inp. Read. Char (); // next slide 20
Binary search trees if (c==‘n’) { break; } else { c = inp. Read. Char (); // line feed } } // end while // Print out the tree root. Print. Sorted (); System. out. print (“n”); } // end main } // end class 21