CSC 205 – Java Programming II Lecture 35 April 17, 2002.

Slides:



Advertisements
Similar presentations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Advertisements

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 1 The ADT Binary Search Tree Definition The ADT Binary Search Tree is a.
BST Search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is.
CS 171: Introduction to Computer Science II
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Marc Smith and Jim Ten Eyck
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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 Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CSC 205 Java Programming II Lecture 28 BST Implementation.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
COSC2007 Data Structures II Chapter 11 Trees II. 2 Topics ADT Binary Tree (BT) Operations Tree traversal BT Implementation Array-based LL-based Expression.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
CS 302 Data Structures Trees.
Binary Search Tree Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Introduction to Trees IT12112 Lecture 05.
Trees Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Trees.
CSC 205 – Java Programming II
CSC 205 – Java Programming II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CSC 205 – Java Programming II Lecture 35 April 17, 2002

Binary Trees – Recap Full, complete, 2-tree Level and height Jane BobTom AlanEllenNancy 0Jane 1Bob 2Tom 3Alan 4Ellen 5Nancy

Array-Based Representation Jane BobTom AlanEllenNancy 0Jane 12 1Bob 34 2Tom5 3Alan 4Ellen 5Nancy 6 06 rootfree Frank Insert Frank

Balanced Tree A binary tree is (height) balanced if –the height of any of its node’s left subtree differes from the height of the node’s right subtree no more than 1. Jane BobTom AlanEllenNancy Jane BobTom EllenNancy Frank

Traversals of a Binary Tree Traverse a tree means –Visit every node of tree –Visit each node only once –Visit nodes in a given order Three different orders –Preorder –Inorder –Postorder

Examples Preorder 60, 20, 10, 40, 30, 50, 70 Postorder 10, 30, 50, 40, 20, 70, 60 Inorder 10, 20, 30, 40, 50, 60, 70

Inorder Traversal Algorithm The result of inorder traversal of a BST –The nodes will be visited in order according to their values inorder(binTree) //Traverses the binary tree binTree in inorder. //Visits a node in between the visits to its left //and right subtrees. if (binTree is not empty) { inorder(left subtree of the binTree’s root) Process (e.g. display) the data in the root inorder(left subtree of the binTree’s root) }

Preorder Traversal Algorithm Postorder generates prefix representations preorder(binTree) //Traverses the binary tree binTree in preorder. //Visits a node before visiting either of its // left and right subtrees. if (binTree is not empty) { Process (e.g. display) the data in the root preorder(left subtree of the binTree’s root) }

Reference-Based Representation Each node contains –An item to be stored –A left and a right references item leftChildrightChild root TreeNode

The TreeNode Class Can be used for any binary trees –not necessarily BSTs public class TreeNode { private Object item; private TreeNode leftChild, rightChild; public TreeNode(Object newItem, TreeNode left, TreeNode right) { item = newItem; leftChild = left; rightChild = right; } …… )

The BinaryTreeBasis Class An abstract base class to be extended –By trees with special properties, such as BST public abstract class BinaryTreeBasis { protected TreeNode root; public BinaryTreeBasis() {root = null;} public BinaryTreeBasis(Object rootItem) { root = new TreeNode(rootItem, null, null); } public Object getRootItem() throws TreeException { …… } …… )

BST Properties satisfied by each node n in a BST –n’s value is greater than all values in its left subtree T L –n’s value is smaller than all values in its right subtree T R –Both T L and T R are BSTs Assume not duplicate values are allowed Items can be stored in a BST should be comparable to each other

BST Operations Values stored in a BST are sorted –Can be displayed in order by inorder traversal Operations of BSTs –Insert a new item into a BST –Delete the item with a given search key from a BST –Retrieve the item with a given search key from a BST –Traverse the items in a BST in the 3 orders

The Search Strategy search(bst, searchKey) //Searches the binary search tree bst for the item //whose search key is searchKey. if (bst is empty) The desired record is not found else if (searchKey == search key of the root’s item) The desired record is found else if (searchKey < search key of the root’s item) search(Left subtree of bst, searchKey) else search(Right subtree of bst, searchKey) The basis of insertion, deletion, and retrieval.

Insertion Use search to determine where into the tree a new item to insert. –search will always terminate at an empty tree. insertItem(treeNode, newItem) //Inserts newItem into the binary search tree of //which treeNode is the root. Let parentNode be the parent of the empty subtree at which search terminates when it seeks newItem’s search key if (search parentNode’s left subtree) Set leftChild of parentNode to reference newItem else Set rightChild of parentNode to reference newItem

Deletion Search will be used A separate deleteNode method will also be called deletItem(rootNode, searchKey) //Deletes from the bst with root rootNode the item //whose search key equals searchKey. If no such item //exists, the operation fails & throws TreeException. Locate (by using the search algorithm) the item whose search key equals searchKey ; it occurs in node i if (item is found in node i) deleteNode(i) //defined next else throws a tree exception

Deletion – 3 Scenarios Delete is much more complicated There are three different cases to consider Assume that item i is in node N –N is a leaf – simply delete it –N has only one child – replace N w/ its child –N has two children – needs more discussion

With Two Children After deleting the node N from a BST, the resultant binary tree should still be a BST The following steps are needed in this case –Locate another node M that is easier to remove from the BST than the node N. That is, M has at most one child. –Copy the item that is in M to N –Remove the node M from the BST

Example – deleting the root Jane BobTom AlanEllenNancyWendy N M Nancy Alan, Bob, Ellen, Jane, Nancy, Tom, Wendy

Result – still a BST Nancy BobTom AlanEllenWendy

The deleteNode Algorithm Recall that the inorder traversal displays the nodes in order according to their values –Either the inorder successor or predecessor of N can be used to replace N –In our example, either Nancy or Ellen would work They are the left-most descendant in the right subtree (Nancy, inorder successor), and the right- most descendant in the left subtree (Ellen, inorder predecessor ), respectively