Download presentation
Presentation is loading. Please wait.
Published byJared Gilbert Modified over 9 years ago
1
COSC2007 Data Structures II Chapter 11 Trees IV
2
2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees
3
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 60 7020 1040 5030
4
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 60 7020 1040 5030
5
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 60 7020 1040 5030
6
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 60 7020 1040 5030
7
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
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
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
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
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
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
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
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
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
16
16 6 2 4 3 1 8 Search for 4 Efficiency of BST Search
17
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
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)
19
19 6 2 4 3 1 8 Insert 5 Efficiency of BST Insertion
20
20 6 2 4 3 1 8 5 What’s the time bound for insertion? h = height of the tree Efficiency of BST Insertion
21
21 6 2 4 3 1 8 What’s the time bound for insertion? 5 T insert (N) = ? h = height of the tree Efficiency of BST Insertion
22
22 6 2 4 3 1 8 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
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
24 Review The traversal of a binary tree is ______. O(n) O(1) O(n 2 ) O(log 2 n)
25
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
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
27 Review A data element within a record is called a ______. field tree Collection key
28
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
29 Review The maximum height of a binary tree of n nodes is ______. n n / 2 (n / 2) – 2 log 2 (n + 1)
30
30 Review The minimum height of a binary tree of n nodes is ______. n n / 2 (n / 2) – 2 log 2 (n + 1)
31
31 Review A full binary tree with height 4 has ______ nodes. 7 8 15 31
32
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
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
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
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
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.