Download presentation
Presentation is loading. Please wait.
Published byKristina Moody Modified over 9 years ago
1
1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
2
2 Building a graph class VertexList { private int vertexId; private VertexList next; public VertexList (int x) { vertexId = x; next = null; } // next slide
3
3 Building a graph public VertexList Add (int x) { // Add at the front VertexList newHead = new VertexList (x); newHead.SetNext(this); return newHead; } public void SetNext (VertexList vl) { next = vl; } // next slide
4
4 Building a graph public void Print () { if (next==null) { System.out.println (vertexId); } else { System.out.print (vertexId + “, ”); next.Print(); } } // end class
5
5 Building a graph class GraphBuilder { public static void main (String a[]) throws java.io.IOException { // Array of vertex lists is a graph VertexList graph[]; char c; boolean directed = false; int vertex1, vertex2, n, i; MyInput inp = new MyInput(); // next slide
6
6 Building a graph System.out.print (“Enter number of vertices:”); n = inp.ReadInt(); graph = new VertexList[n]; for (i=0; i<n; i++) { graph[i] = null; } // next slide
7
7 Building a graph System.out.print (“Directed or undirected? (d/u)”); c = inp.ReadChar(); if (c==‘d’) { directed = true; } c = inp.ReadChar();// eat the line feed while (true) { System.out.print (“Enter an edge as two vertex ids: ”); // next slide
8
8 Building a graph vertex1 = inp.ReadInt(); vertex2 = inp.ReadInt(); if (graph[vertex1] != null) { graph[vertex1] = graph[vertex1].Add (vertex2); } else { graph[vertex1] = new VertexList (vertex2); } // next slide
9
9 Building a graph if (directed==false) { // Need to add vertex2->vertex1 if (graph[vertex2] != null) { graph[vertex2] = graph[vertex2].Add (vertex1); } else { graph[vertex2] = new VertexList (vertex1); } // next slide
10
10 Building a graph System.out.print (“More edges? (y/n)”); c = inp.ReadChar(); if (c==‘n’) { break; } else { c = inp.ReadChar(); // line feed } } // end of while // next slide
11
11 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
12
12 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 the values in sorted order
13
13 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
14
14 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
15
15 Binary search trees else { if (right == null) { right = new BSTVertex (x); } else { right.Insert (x); } // next slide
16
16 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
17
17 Binary search trees else { if (right == null) { return false; } else { return right.Search (x); } // next slide
18
18 Binary search trees public void PrintSorted () { // Known as in-order traversal // Prints in ascending order of values if (left != null) { left.PrintSorted (); } System.out.print (value + “ ”); if (right != null) { right.PrintSorted (); } } // end class
19
19 Binary search trees class BSTBuilder { public static void main (String a[]) throws java.io.IOException { BSTVertex root = null; int x; char c; MyInput inp = new MyInput (); while (true) { System.out.print (“Enter the next value: ”); x = inp.ReadInt(); // next slide
20
20 Binary search trees if (root != null) { root.Insert (x); } else { root = new BSTVertex (x); } System.out.print (“More values? (y/n)”); c = inp.ReadChar (); // next slide
21
21 Binary search trees if (c==‘n’) { break; } else { c = inp.ReadChar (); // line feed } } // end while // Print out the tree root.PrintSorted (); System.out.print (“\n”); } // end main } // end class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.