CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Binary Trees Binary Search Trees.  Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the.
Department of Computer Science University of Maryland, College Park
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Binary Search Trees Chapter 7 Objectives
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture1 introductions and Tree Data Structures 11/12/20151.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Data Structures Using Java1 Chapter 10 Binary Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
CSE 373 Data Structures Lecture 7
Recursive Objects (Part 4)
UNIT III TREES.
Binary Search Tree (BST)
Chapter 10.1 Binary Search Trees
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees 7/14/2009.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun

Announcements Project 2 spec is out. – You can work individually or a team of two. – Due 7/28. Midter1 Regrades: If you believe we misgraded questions on a midterm, return the paper to me (or your TA) with a written note of the problem on a separate piece of paper. Upon receiving a regrade request, the entire exam will be regraded, so be sure to check the solution to confirm that you will not loose more points, which has happened in the past.

Inorder Traversal and Infix Expression static String toInfix (Tree T) { if (T == null) return ""; if (T.degree () == 0) return T.label (); else { return String.format ("(%s%s%s)", toInfix (T.left ()), T.label (), toInfix (T.right ()) }

Preorder Traversal and Prefix Expression static String toLisp (Tree T) { if (T == null) return ""; else if (T.degree () == 0) return T.label (); else { String R; R = ""; for (int i = 0; i < T.numChildren (); i += 1) R += " " + toLisp (T.child (i)); return String.format ("(%s%s)", T.label (), R); }

Time Tree traversal is linear: O(N), where N is the # of nodes. – there is one visit at the root, and – one visit for every edge in the tree – since every node but the root has exactly one parent, and the root has none, must be N − 1 edges in any non-empty tree. For k -ary tree (max # children is k ),, where h is height. So Many tree algorithms look at one child only. For them, time is proportional to the height of the tree, and this is assuming that tree is bushy—each level has about as many nodes as possible.

Binary Tree A binary tree is a tree in which no node has more than two children, and every child is either a left child or a right child, even if it's the only child its parent has.

Representing Binary Trees Each tree node has three references to neighboring tree nodes: a "parent" reference, and "left" and "right" references for the two children. Each node also has an "item" reference. class BinaryTreeNode{ Object item; SibTreeNode parent; SibTreeNode left; SibTreeNode right; } class BinaryTree{ BinaryTreeNode root; int size; } public void inorder() { if (left != null) { left.inorder(); } this.visit(); if (right != null) { right.inorder(); }

Divide and Conquer Much computation is devoted to finding things in response to various forms of query. Linear search for response can be expensive, especially when data set is too large for primary memory. Preferable to have criteria for dividing data to be searched into pieces recursively Tree is a natural framework for the representation:

Binary Search Tree Binary Search Tree is a binary tree. Generally, each node of the Binary Search Tree contains an pair called an Entry. – The key can be the same as the value. Binary Search Tree satisfies the following property, called the binary search tree invariant: For any node X, – every key in the left subtree of X is less than or equal to X's key, and – every key in the right subtree of X is greater than or equal to X's key. – A key equal to the parent's key can go into either subtree. Example.

Finding public Entry find (Comparable aKey) { BinaryTreeNode T = findHelper(root, aKey); if (T == null) return null; else return T.entry; } private static BinaryTreeNode findHelper(BinaryTreeNode T, Comparable aKey){ if (T == null) return T; //compare the Key with the current node int comp = aKey.compareTo(T.entry.key()); //aKey is smaller, look in the right subtree if (comp < 0) return find(T.left, aKey); //aKey is larger, look in the right subtree else if (comp < 0) return find(T.right, aKey); else //return when find a match return T; } Dashed boxes show which node labels we look at. Number looked at proportional to height of tree. Search for key = 50:

Iterative Version public Entry find(Comparable aKey) { //start at the root BinaryTreeNode T = root; while (node != null) { //compare the Key with the current node int comp = aKey.compareTo(T.entry.key); //aKey is smaller, look in the left subtree if (comp < 0) node = node.left; //aKey is larger, look in the right subtree else if (comp > 0) node = node.right; //Stop and return when find a match else return node.entry; } //Return null on failure return null; }

Finding What if we want to find the smallest key greater than or equal to k, or the largest key less than or equal to k? When searching downward through the tree for a key k that is not in the tree, we are certain to encounter both – the node containing the smallest key greater than k (if any key is greater) – the node containing the largest key less than k (if any key is less). – Implement a method smallestKeyNotSmaller(k): Use a variable to track the smallest key not smaller than k found so far. Pretend you are looking for key k, if you find it return immediately When you encounter a null pointer, return the value of the variable

First and Last Entry first() – find the smallest key in the binary tree – If the tree is empty, return null. Otherwise, start at the root. Repeatedly go to the left child until you reach a node with no left child. Entry last() – find the largest key in the binary tree – If the tree is empty, return null. Repeatedly go to the right child until you reach a node with no right child.

Inserting insert() starts by following the same path through the tree as find(). When it reaches a null reference, replace the null with a reference to a new node. Duplicate keys are allowed. – If insert() finds a node that already has the key, it puts it the new entry in the left subtree of the older one. – We could just as easily choose the right subtree; it doesn't matter.

Inserting public insert(Entry e) { insertHelper(root, E); } BinaryTreeNode static insertHelper(BinaryTreeNode T, Entry E) { if (T == null) return new BinaryTreeNode(E); //compare the Key with the current node int comp = aKey.compareTo(E.key); //aKey is smaller or equal, insert in the left subtree if (comp <= 0) T.left = insert(T.left, E); //aKey is larger, insert in the right subtree else //if (comp > 0) T.left = insert(T.right, E); return T; } Starred edges are set (to themselves, unless initially null). Again, time proportional to height. Try writing an iterative version. Insert with Key = 27

Reading Objects, Abstraction, Data Structures and Design using Java 5.0 – Chapter 8 pp