COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSC 205 – Java Programming II Lecture 35 April 17, 2002.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
A balanced life is a prefect life.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
CS202 - Fundamentals of Computer Science II
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees part 2.. Full Binary Trees A BC GDE F Full binary tree A binary tree is full if all the internal nodes (nodes other than leaves) has two children.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
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.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
COSC2007 Data Structures II
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
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.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
2-3 Trees, Trees Red-Black Trees
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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 12 Tables & Priority Queues III.
Trees 12/6/2015CS202 - Fundamentals of Computer Science II1.
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.
CSC 205 Java Programming II Lecture 28 BST Implementation.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS 302 Data Structures Trees.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees.
Binary Search Tree Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 16 Tree Implementations
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures Balanced Trees CSCI
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CS202 - Fundamental Structures of Computer Science II
Trees Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CSC 205 – Java Programming II
CSC 205 – Java Programming II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Presentation transcript:

COSC2007 Data Structures II Chapter 11 Trees IV

2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

3 BST Traversal public class TreeNode // node in the tree { private Object item; // data portion private TreeNode leftChildPtr; // pointer to left child private TreeNode rightChildPtr; // pointer to right child TreeNode() {}; TreeNode(Object nodeItem, TreeNode left, TreeNode right ) { } ……. } // end TreeNode class public abstract class BinaryTreeBasis { protected TreeNode root; ……. // constructor and methods } // end BinaryTreeBasis class

4 BST Traversal public class TreeNode // node in the tree { private Object item; // data portion private TreeNode leftChildPtr; // pointer to left child private TreeNode rightChildPtr; // pointer to right child TreeNode() {}; TreeNode(Object nodeItem, TreeNode left, TreeNode right ) { } ……. } // end TreeNode class public abstract class BinaryTreeBasis { protected TreeNode root; ……. // constructor and methods } // end BinaryTreeBasis class

5 ADT BST Implementation Implementation of BST Operations // Assumption: A tree contains at most one item with a given // search key at any time. // public class BinarySearchTree extends BinaryTreeBasis //inherits isEmpty(), makeEmpty(), getRootItem() { public:BinarySearchTree(){} //default constructor public BinarySearchTree(KeyedItem rootItem) { super (rootItem); } void insert(Keyeditem newItem) { root =insertItem(root, newItem); } // end insert

6 ADT BST Implementation Implementation of BST Operations // Assumption: A tree contains at most one item with a given // search key at any time. // public class BinarySearchTree extends BinaryTreeBasis //inherits isEmpty(), makeEmpty(), getRootItem() { public:BinarySearchTree(){} //default constructor public BinarySearchTree(KeyedItem rootItem) { super (rootItem); } void insert(Keyeditem newItem) { root =insertItem(root, newItem); } // end insert

7 ADT BST Implementation public void delete(Comparable searchKey) throws TreeException { root =deleteItem(root, searchKey); } // end delete public void delete(KeyedItem item) throws TreeException { root =deleteItem(root, item.getKey()); } // end delete public KeyedItem retrieve(Comparable searchKey) { return retrieveItem(root, searchKey); } // end retrieve

8 ADT BST Implementation Protected TreeNode insertItem(TreeNode tNode, KeyedItem newItem) { TreeNode newSubtree; if (tNode == NULL) { // position of insertion found; insert after leaf // create a new node tNode = new TreeNode(newItem, null, null); return tNode; } KeyedItem nodeItem = (KeyedItem)tNode.getItem(); //search for the insertion position if (newItem.getKey().compareTo(nodeItem.getKey())<0) { // search the left subtree newSubtree=insertItem(tNode.getLeft(), newItem); tNode.setleft (newSubtree); return tNode; } else { // search the right subtree newSubtree=insertItem(tNode.getRight(), newItem); tNode.setRight (newSubtree); return tNode; } } // end insertItem

9 ADT BST Implementation protected TreeNode deleteItem(TreeNode tNode, Comparable searchKey) // Calls: deleteNode. { TreeNode newSubtree; if (tNode == NULL) throw TreeException("TreeException: delete failed"); // empty tree else { KeyedItem nodeItem = (KeyedItem)tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey())==0) { // item is in the root of some subtree tNode = deleteNode(tNode); // delete the item // else search for the item else if (searchKey.compareTo(nodeItem.getKey())<0) { // search the left subtree newSubtree = deleteItem(tNode.getLeft(), searchKey); tNode.setLeft (newSubtree); } else { // search the right subtree newSubtree = deleteItem(tNode.getRight(), searchKey); tNode.setLeft (newSubtree); } end if } //end if return tNode; } // end deleteItem

10 ADT BST Implementation protected TreeNode deleteNode(TreeNode tNode) { // Algorithm note: There are four cases to consider: // 1. The root is a leaf. 2. The root has no left child. // 3. The root has no right child.4. The root has two children. // Calls: findleftmost and deleteleftMost. KeyedItem replacementItem; // test for a leaf if ((tNode.getLeft() == null) && (tNode.getRight() == null) ){ return null; } // end if leaf // test for no left child else if (tNode.getLeft() == null) { return tNode.getRight(); } // end if no left child

11 ADT BST Implementation // test for no right child else if (tNode.getRight() == null) { return tNode.getLeft(); } // end if no right child // there are two children: retrieve and delete the inorder successor else { replacementItem =findLeftmost(tNode.getRight()); tNode.setItem (replacementItem); tNode.setRight(deleteLeftmost(tNode.getRight())); return tNode; } // end if two children } // end deleteNode

12 ADT BST Implementation protected KeyedItem findLeftmost(TreeNode tNode) { if (tNode.getLeft()== null) return (KeyedItem)tNode.getItem (); else return findLeftmost(tNode.getLeft()); } // end findLeftmost protected TreeNode deleteLeftmost(TreeNode tNode) { if (tNode.getLeft()== null) return tNode.getRight (); else { tNode.setLeft (deleteLeftmost (tNode.getLeft())); return tNode; } } // end deleteLeftmost

13 ADT BST Implementation protected KeyedItem retrieveItem(TreeNode tNode, Comparable searchKey ) { KeyedItem treeItem; if (tNode == null) treeItem = null; else { KeyedItem nodeItem = (KeyedItem)tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey())==0) // item is in the root of some subtree treeItem = (KeyedItem)tNode.getItem(); else if (searchKey.compareTo(nodeItem.getKey())<0) // search the left subtree retrieveItem(tNode.getLeft(), searchKey); else // search the right subtree retrieveItem(tNode.getRight(), searchKey); } //end if return treeItem; } // end retrieveItem

14 Efficiency of BST Operations For the retrieval, insertion, and deletion operations, compare the searchKey to the search keys in the nodes along a path through the tree The path terminates At the node that contains the search key Or, if the searchKey isn't present, at an empty subtree

15 Efficiency of BST Operations The maximum height of N-Node tree: When ? Complete trees & full trees have minimum height The height of an N-node BST ranges from  log 2 ( N + 1 )  to N Questions

Search for 4 Efficiency of BST Search

17 In general, the search is confined to nodes along a single path from the root to a leaf h = height of the tree Search time = O(h)

18 Suppose the tree is balanced, which means minimum path length is close to maximum path length h = height of the tree = O(log N) where N is the size (number of nodes) in the tree Search time = O(h) = O(log N)

Insert 5 Efficiency of BST Insertion

What’s the time bound for insertion? h = height of the tree Efficiency of BST Insertion

What’s the time bound for insertion? 5 T insert (N) = ? h = height of the tree Efficiency of BST Insertion

What’s the time bound for insertion? 5 T insert (N) = T search (N) + c = ? h = height of the tree Efficiency of BST Insertion

23 Comparison of Time Complexity Operation Average caseWorse Case Retrieval O(logn)O(n) Insertion O(logn)O(n) Deletion O(logn)O(n) Traversal O(n) O(n)

24 Review The traversal of a binary tree is ______. O(n) O(1) O(n 2 ) O(log 2 n)

25 Review In an array based representation of a complete binary tree, which of the following represents the right child of node tree[i]? tree[i+2] tree[i–2] tree[2*i+1] tree[2*i+2]

26 Review In an array based representation of a complete binary tree, which of the following represents the parent of node tree[i]? tree[i–2] tree[(i–1)/2] tree[2*i–1] tree[2*i–2]

27 Review A data element within a record is called a ______. field tree Collection key

28 Review The maximum number of comparisons for a retrieval operation in a binary search tree is the ______. length of the tree height of the tree number of nodes in the tree number of leaves in the tree

29 Review The maximum height of a binary tree of n nodes is ______. n n / 2 (n / 2) – 2 log 2 (n + 1)

30 Review The minimum height of a binary tree of n nodes is ______. n n / 2 (n / 2) – 2 log 2 (n + 1)

31 Review A full binary tree with height 4 has ______ nodes

32 Review A complete binary tree 1. is sometimes a full binary tree 2. is never a full binary tree 3. is sometimes a binary tree 4. is always a full binary tree

33 Review A proper binary tree 1. is sometimes a full binary tree 2. is never a full binary tree 3. is sometimes a binary tree 4. is always a full binary tree

34 Review Select the one true statement. A. Every binary tree is either complete or full. B. Every complete binary tree is also a full binary tree. C. Every full binary tree is also a complete binary tree. D. No binary tree is both complete and full.

35 Review (True/False) The ADT binary search tree is value- oriented. A binary tree cannot be empty. The root of a tree is at level 0. Inorder traversal visits a node before it traverses either of its subtrees.

36 Review (True/False) Insertion in a search-key order produces a maximum-height BST Insertion in random order produces a near-minimum-height BST