1 Graphs and Search Trees Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
1 Linear and Binary Search Instructor: Mainak Chaudhuri
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Discrete Mathematics Chapter 5 Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
1 Input/Output. 2 In Java input and output take place through I/O streams – An I/O stream represents an input source or output destination – Streams support.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Chapter 12 – Data Structures
BST Trees
Week 6 - Wednesday CS221.
Announcements Project checkpoint next week after lab sessions
Tree.
Trees.
Week 11 - Friday CS221.
Topic 18 Binary Search Trees
Examples of class: Using System.in.read ()
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Topic 19 Binary Search Trees
Trees.
Representation of a Threaded Tree
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Lecture 12 CS203 1.
CSC 143 Binary Search Trees.
Building Java Programs
Trees.
Tree traversals BST properties Search Insertion
Trees Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

1 Graphs and Search Trees Instructor: Mainak Chaudhuri

2 Building a graph class VertexList { private int vertexId; private VertexList next; public VertexList (int x) { vertexId = x; next = null; } // next slide

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 Building a graph public void Print () { if (next==null) { System.out.println (vertexId); } else { System.out.print (vertexId + “, ”); next.Print(); } } // end class

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 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 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 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 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 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 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 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 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 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 Binary search trees else { if (right == null) { right = new BSTVertex (x); } else { right.Insert (x); } // next slide

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 Binary search trees else { if (right == null) { return false; } else { return right.Search (x); } // next slide

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 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 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 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