Download presentation
Presentation is loading. Please wait.
1
2009 Pearson Education, Inc. All rights reserved. 1 26 Data Structures Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides added by L.Lilien are © 2006-2009 Leszek T. Lilien. Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly granted upon a written (e.g., emailed) request.
2
2009 Pearson Education, Inc. All rights reserved. 2 26.1 Introduction 26.2 Simple-Type struct s, Boxing and Unboxing 26.3 Self-Referential Classes 26.4 Linked Lists 26.5 Stacks 26.6 Queues 26.7 **SKIP** Trees 26.7.1 Binary Search Tree of Integer Values 26.7.2 Binary Search Tree of IComparable Objects
3
2009 Pearson Education, Inc. All rights reserved. 3 26.1 Introduction Dynamic data structures – Grow and shrink at execution time – Linked Lists: - “Lined up in a row” - Insertions and removals can occur anywhere in the list – Stacks: - Insertions and removals only at top Push and pop – Queues: - Insertions made at back, removals from front – *SKIPPED* Trees, including binary trees: - Facilitate high-speed searching and sorting of data - Efficient elimination of duplicate items
4
2009 Pearson Education, Inc. All rights reserved. 4 26.2Simple-Type structs, Boxing and Unboxing Data structures can store: – Simple-type values – Reference-type values There are mechanisms that enable manipulation of simple-type values as objects – Discussed below
5
2009 Pearson Education, Inc. All rights reserved. 5 DIGRESSION: Simple-Type struct s Recall: Each simple type (int, char, …) has a corresponding struct in namespace System that declares this simple type We have struct s: – Boolean, Byte & Sbyte, Char, Decimal, Double, Int16 & UInt16, Int32 & UInt32, Int64 & UInt64 Simple types are just aliases for these corresponding struct s – So a variable can be declared: EITHER using the keyword for that simple type OR the struct name - E.g., int (keyword) and Int32 ( struct name) are interchangeable – Methods related to a simple type are located in the corresponding struct - E.g. method Parse –converting string to int value– is located in struct Int32
6
2009 Pearson Education, Inc. All rights reserved. 6 Boxing Conversions All simple-type struct s inherit from class System.ValueType In turn, class ValueType inherits from class object => Any simple-type value can be assigned to an object variable Such an assignment is named a Boxing conversions – In Boxing c onversion simple-type value is copied into an object => after Boxing c onversion, a simple-type value can be manipulated as an object
7
2009 Pearson Education, Inc. All rights reserved. 7 Boxing Conversions (Cont.) Boxing conversions can be explicit or implicit int i = 5; // create an int value object obj1 = ( object) i; // explicit boxing conversion of // a simple int value into an object object obj2 = i; // implicit boxing conversion of a simple // int value into an object – Now obj1 and obj2 refer to 2 different objects! - Both objects include a copy of the integer value from the int variable i
8
2009 Pearson Education, Inc. All rights reserved. 8 Unboxing Conversions Unboxing conversions — used for explicit conversion of an object reference to a simple value int int1 = ( int ) obj1; // explicitly unboxes the int value // that was boxed within obj1 Attempts to unbox an object reference that does not refer to a correct simple value causes InvalidCastException We’ll see later (Ch. 27, ed.3) how so called “generics” eliminate the overhead of boxing/unboxing
9
2009 Pearson Education, Inc. All rights reserved. 9 26.3 Self-Referential Classes Self-referential class – Contains a reference member to an object of the same class type - E.g.: class Node { private int data; private Node next; // self-reference to Node … } - Reference like next above can be used to link objects of the same type together -So, next above is a link -Self-referential classes are conveniently implemented using dynamic data structures -“Dynamic” because they grow/shrink in computer memory Slide modified by L. Lilien
10
2009 Pearson Education, Inc. All rights reserved. 10 26.3 Self-Referential Classes (Cont.) Growth of dynamic data structures requires dynamic memory allocation – Obtain memory when needed - E.g., using new operator E.g.: Node nodeToAdd = new Node(10); Note: If no memory is available, new throws an OutOfMemoryException. Shrinking of dynamic data structures requires dynamic memory deallocation (release) – Release memory when no longer needed - E.g.: nodeToAdd = null; // now available for // garbage collection Slide modified by L. Lilien
11
2009 Pearson Education, Inc. All rights reserved. 11 Outline Fig26_01.cs
12
2009 Pearson Education, Inc. All rights reserved. 12 26.3 Self-Referential Classes (Cont.) Self-referential objects can be linked together to form useful and more complex data structures. – Figure illustrates two self-referential objects linked together to form a linked list. – A backslash “\” indicates a null link.
13
2009 Pearson Education, Inc. All rights reserved. 13 ** READ LATER ** 26.3 Self-Referential Classes (Cont.) Common Programming Error 26.1 Not setting the link in the last node of a list to null is a logic error. Good Programming Practice 26.1 When creating a large number of objects, test for an OutOfMemoryException. Perform appropriate error processing if the requested memory is not allocated.
14
2009 Pearson Education, Inc. All rights reserved. 14 26.4 Linked Lists Linked List: – Linear collection of self-referential nodes connected by links - Nodes: class objects of linked-lists - Programs access linked lists through a reference to first node Subsequent nodes accessed by link-reference members Last node’s link set to null to indicate end of list - Nodes can hold data of any type - Nodes created dynamically Slide modified by L. Lilien A graphical representation of a linked list (with an optional reference to the last node).
15
2009 Pearson Education, Inc. All rights reserved. 15 26.4 Linked Lists (Cont.) Linked lists - similar to arrays However: - Arrays are a fixed size - Linked lists have no limit to size - Can grow till memory available - Linked lists can grow at run time - More nodes can be added as program executes Slide modified by L. Lilien
16
2009 Pearson Education, Inc. All rights reserved. 16 ** READ LATER ** 26.4 Linked Lists (Cont.) Performance Tip 26.1 An array can be declared to contain more elements than the number of items expected, possibly wasting memory. Linked lists provide better memory utilization in these situations, because they can grow and shrink at execution time. Performance Tip 26.2 After locating the insertion point for a new item in a sorted linked list, inserting an element in the list is fast—only two references have to be modified. All existing nodes remain at their current locations in memory. Performance Tip 26.3. The elements of an array are stored contiguously in memory to allow immediate access to any array element. Linked list elements can be accessed only by traversing the list from the front.
17
2009 Pearson Education, Inc. All rights reserved. 17 ** READ LATER ** 26.4 Linked Lists (Cont.) A linked list is appropriate when the number of data elements is unpredictable Linked lists become full (can not grow anymore) only when the system has insufficient memory
18
2009 Pearson Education, Inc. All rights reserved. 18 26.4 Linked Lists (Cont.) Operations on linked lists: – Insert: - at front - at back – Remove - from front - from back All shown next
19
2009 Pearson Education, Inc. All rights reserved. 19 26.4 Linked Lists (Cont.) - InsertAtFront A graphical representation of the InsertAtFront operation. Changed links Unchanged links Slide modified by L. Lilien 117 12 new ListNode (a) firstNode 12 new ListNode 117 firstNode (b)
20
2009 Pearson Education, Inc. All rights reserved. 20 26.4 Linked Lists (Cont.) - InsertAtBack A graphical representation of the InsertAtBack operation. (a) New ListNode 571211 firstNodelastNode (b) 711 firstNodeNew ListNode 125 lastNode Changed links Unchanged links Slide modified by L. Lilien
21
2009 Pearson Education, Inc. All rights reserved. 21 26.4 Linked Lists (Cont.) - RemoveFromFront A graphical representation of the RemoveFromFront operation. (a) 712 lastNode 115 firstNode 711 firstNodelastNode (b) 125 removeItem Slide modified by L. Lilien Changed links Unchanged links
22
2009 Pearson Education, Inc. All rights reserved. 22 26.4 Linked Lists (Cont.) - RemoveFromBack A graphical representation of the RemoveFromBack operation. (a) 712 lastNode 115 firstNode Slide modified by L. Lilien Changed links Unchanged links 711 firstNodelastNode (b) 125 removeItem
23
2009 Pearson Education, Inc. All rights reserved. 23 ** READ LATER ** 26.4 Linked Lists (Cont.) Performance Tip 26.4 Using linked data structures and dynamic memory allocation can save memory. Keep in mind, however, that reference links occupy space, and dynamic memory allocation incurs the overhead of method calls. Performance Tip 26.5 Insertion and deletion in a sorted array can be time consuming— all the elements following the inserted or deleted element must be shifted appropriately
24
2009 Pearson Education, Inc. All rights reserved. 24 Outline LinkedListLibrary.cs (1 of 9 ) The program of Figs. 26.4 and 26.5 (below) uses an object of class List to manipulate a list of miscellaneous object types. The code of Fig. 26.4 is placed in its own class library: LinkedListLibrary. Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 1 of 9.) Data can refer to any object. Next stores a reference to the next ListNode object in the linked list.
25
2009 Pearson Education, Inc. All rights reserved. 25 Outline LinkedListLibrary.cs (2 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 2 of 9.) Overriding the default constructor to place a ListNode at the end of a List Constructor which places a ListNode before a specific ListNode.
26
2009 Pearson Education, Inc. All rights reserved. 26 Outline LinkedListLibrary.cs (3 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 3 of 9.) private instance variables firstNode and lastNode are endpoints of a linked list.
27
2009 Pearson Education, Inc. All rights reserved. 27 Outline LinkedListLibrary.cs (4 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 4 of 9.) InsertAtFront manipulates the List (this process is explained later) InsertAtBack manipulates the List (this process is explained later)
28
2009 Pearson Education, Inc. All rights reserved. 28 Outline LinkedListLibrary.cs (5 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 5 of 9.) RemoveFromFront manipulates the List (this process is explained later) RemoveFromBack manipulates the List (this process is explained later)
29
2009 Pearson Education, Inc. All rights reserved. 29 Outline LinkedListLibrary.cs (6 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 6 of 9.) RemoveFromBack manipulates the List (this process is explained later)
30
2009 Pearson Education, Inc. All rights reserved. 30 Outline LinkedListLibrary.cs (7 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 7 of 9.) IsEmpty determines whether the list is empty.
31
2009 Pearson Education, Inc. All rights reserved. 31 Outline LinkedListLibrary.cs (8 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 8 of 9.)
32
2009 Pearson Education, Inc. All rights reserved. 32 Outline LinkedListLibrary.cs (9 of 9 ) Fig. 26.4 | ListNode, List and EmptyListException class declarations. (Part 9 of 9.)
33
2009 Pearson Education, Inc. All rights reserved. 33 Outline ListTest.cs (1 of 4 ) Class ListTest (Fig. 26.5 - below) uses the above LinkedListLibrary to create and manipulate a linked list. To use the library, we need (see Line 4) : using LinkedListLibrary Fig. 26.5 | Testing class List. (Part 1 of 4.) Initializing a new List object.
34
2009 Pearson Education, Inc. All rights reserved. 34 Outline ListTest.cs (2 of 4 ) Fig. 26.5 | Testing class List. (Part 2 of 4.) Demonstrating List insertion methods. Removing objects via List deletion methods.
35
2009 Pearson Education, Inc. All rights reserved. 35 Outline ListTest.cs (3 of 4 ) Fig. 26.5 | Testing class List. (Part 3 of 4.) Removing objects via List deletion methods.
36
2009 Pearson Education, Inc. All rights reserved. 36 Outline ListTest.cs (4 of 4 ) Fig. 26.5 | Testing class List. (Part 4 of 4.)
37
2009 Pearson Education, Inc. All rights reserved. 37 Perform In–class Activity #1: Create & use LinkedListLibrary 26.5 Stacks – Cont. In-class Activity #1
38
2009 Pearson Education, Inc. All rights reserved. 38 ** READ LATER ** 26.4 Linked Lists (Cont.) Method InsertAtFront places a new node at the front of the list through the following process: 1.Determine whether the list is empty. 2.If the list is empty, set both firstNode and lastNode to refer to a new ListNode. 3.If the list is not empty, the new node is “linked” into the list by setting firstNode to refer to a new ListNode.
39
2009 Pearson Education, Inc. All rights reserved. 39 ** READ LATER ** (We have already seen this and the following 9 slides) 26.4 Linked Lists (Cont.) In Fig. 26.6, part (a) shows a list and a new node during the InsertAtFront operation and before the new node is linked into the list. Part (b) illustrates Step 3 of the InsertAtFront operation, which enables the node containing 12 to become the new list front.
40
2009 Pearson Education, Inc. All rights reserved. 40 ** READ LATER ** 26.4 Linked Lists (Cont.) Fig. 26.6 | InsertAtFront operation.
41
2009 Pearson Education, Inc. All rights reserved. 41 ** READ LATER ** 26.4 Linked Lists (Cont.) Method InsertAtBack places a new node at the back of the list through the following process: 1.Determine whether the list is empty. 2.If the list is empty, set both firstNode and lastNode to refer to a new ListNode. 3.If the list is not empty, link the new node into the list by setting lastNode and lastNode.Next to refer to a new ListNode object.
42
2009 Pearson Education, Inc. All rights reserved. 42 ** READ LATER ** 26.4 Linked Lists (Cont.) In Fig. 26.7, part (a) shows a list and a new node during the InsertAtBack operation; before the new node has been linked into the list. The dashed lines and arrows in part (b) illustrate Step 3 of method InsertAtBack, which enables a new node to be added to the end of a list that is not empty.
43
2009 Pearson Education, Inc. All rights reserved. 43 ** READ LATER ** 26.4 Linked Lists (Cont.) Fig. 26.7 | InsertAtBack operation.
44
2009 Pearson Education, Inc. All rights reserved. 44 ** READ LATER ** 26.4 Linked Lists (Cont.) Method RemoveFromFront removes the front node of the list and returns a reference to the removed data. 1.Assign firstNode.Data to variable removeItem. 2.If firstNode and lastNode are the same object, the method sets firstNode and lastNode to null. 3.If the list has more than one node, the method assigns firstNode.Next to firstNode. 4.Return the removeItem reference.
45
2009 Pearson Education, Inc. All rights reserved. 45 ** READ LATER ** 26.4 Linked Lists (Cont.) In Fig. 26.8, part (a) illustrates a list before a removal operation. Part (b) shows the reference manipulations. Fig. 26.8 | RemoveFromFront operation.
46
2009 Pearson Education, Inc. All rights reserved. 46 ** READ LATER ** 26.4 Linked Lists (Cont.) Method RemoveFromBack removes the last node of a list and returns a reference to the removed data. 1.Assign lastNode.Data to variable removeItem. 2.If firstNode and lastNode refer to the same object, set firstNode and lastNode to null. 3.If the list has more than one node, create ListNode variable current and assign it firstNode.
47
2009 Pearson Education, Inc. All rights reserved. 47 ** READ LATER ** 26.4 Linked Lists (Cont.) 4.Now “walk the list” with current until it references the node before the last node. 5.After locating the second-to-last node, assign current to lastNode. 6.Set current.Next to null to remove the last node from the list. 7.Return the removeItem reference.
48
2009 Pearson Education, Inc. All rights reserved. 48 ** READ LATER ** 26.4 Linked Lists (Cont.) In Fig. 26.9, part (a) illustrates a list before a removal operation. Part (b) shows the reference manipulations. Fig. 26.9 | RemoveFromBack operation.
49
2009 Pearson Education, Inc. All rights reserved. 49 26.4 Linked Lists (Cont.) – Circular (Singly) Linked Lists A linked list = a singly linked list A linked list may be traversed in only one direction. In a circular, singly linked list (Fig. 26.10), the last node points back to the first node. Fig. 26.10 | Circular, singly linked list.
50
2009 Pearson Education, Inc. All rights reserved. 50 26.4 Linked Lists (Cont.) – Doubly Linked Lists A doubly linked list (fig.) allows traversals both forward and backward. Each node has both: – a forward reference to the next node in the list and – a backward reference to the previous node in the list Fig. 26.11 | Doubly linked list.
51
2009 Pearson Education, Inc. All rights reserved. 51 26.4 Linked Lists (Cont.) – Circular Doubly Linked Lists Fig. 26.12 | Circular, doubly linked list. In a circular, doubly linked list (fig. below): – the forward reference of the last node refers to the first node, and – the backward reference of the first node refers to the last node.
52
2009 Pearson Education, Inc. All rights reserved. 52 26.5 Stacks Stack – Everyday example: Stack of plates in a cafeteria - Add a plate at the top (“push” a plate on the stack) - Remove a plate from the top (“pop” a plate from the stack) – In computer science, stack is a special version of a linked list: Last-in, first-out (LIFO) data structure - Takes and releases new nodes only at the “top “(=the front of the list) Operations: – Push: adds new entry (node) to top of stack – Pop: removes top entry (node) from top of stack Can be used for: – Storing return addresses – Storing local variables – …
53
2009 Pearson Education, Inc. All rights reserved. 53 26.5 Stacks – Cont. Stack Implemented via Inheritance from Class (Linked) List Program of Figs. 26.13 and 26.14 (below) create a stack class — named StackInheritance— by inheriting from class List from the library LinkedListLibrary (of Fig. 26.4). LL’s note: The name StackInheritance is not clear enough. A much clearer (though much longer) name would be: StackImplementedByListInheritance
54
2009 Pearson Education, Inc. All rights reserved. 54 Outline StackInheritance Library.cs
55
2009 Pearson Education, Inc. All rights reserved. 55 StackInheritanceTest (Fig. 26.14 below) uses class StackInheritance. Outline StackInheritance Test.cs (1 of 3 ) Fig. 26.14 | Testing class StackInheritance. (Part 1 of 4.) Creating a stack of object s.
56
2009 Pearson Education, Inc. All rights reserved. 56 Outline StackInheritance Test.cs (2 of 3) Adding various values to the stack. An infinite loop pops elements from the stack until an EmptyListException is thrown.
57
2009 Pearson Education, Inc. All rights reserved. 57 Outline The output of the program testing class StackInheritance. StackInheritance Test.cs (3 of 3 ) Produced by Line 129 in the method Display of the class List from the LinkedListLibrary (Fig. 26.4) Produced by Line 78 of class List from the LinkedListLibrary (Fig. 26.4)
58
2009 Pearson Education, Inc. All rights reserved. 58 Perform In–class Activity #2: Create & use LinkedListLibrary & StackInheritanceLibrary 26.5 Stacks – Cont. In-class Activity #2
59
2009 Pearson Education, Inc. All rights reserved. 59 Above: the class Stack implemented by inheritance from class (Linked) List Alternative way: the class Stack implemented by composition using class (Linked) List -reusing class List The only change to StackInheritanceTest : -Use StackCompositionLibrary (Fig. 26.15 - below) instead of StackInheritanceLibrary **READ ON YOUR OWN ** 26.5 Stacks – Cont. Stack Implemented via Composition Using Class (Linked) List
60
2009 Pearson Education, Inc. All rights reserved. 60 Outline StackComposition Library.cs (1 of 2 ) Fig. 26.15 | StackComposition class encapsulates functionality of class List. (Part 1 of 2.) Using a private object of class List to hide functionality. **READ ON YOUR OWN **
61
2009 Pearson Education, Inc. All rights reserved. 61 Outline StackComposition Library.cs (2 of 2 ) Fig. 26.15 | StackComposition class encapsulates functionality of class List. (Part 2 of 2.) **READ ON YOUR OWN **
62
2009 Pearson Education, Inc. All rights reserved. 62 26.6 Queues Queue: – Similar to a checkout line (“queue”) in a supermarket, etc. - Join the “queue” at its tail, you are served when you are at its head (after being serving, you leave the “queue”) – First-in, first-out (FIFO) data structure - Nodes added to the tail, removed from the head Operations: – Enqueue: insert node at the end – Dequeue: remove node from the front Many computer applications: – Printer spooling - Jobs in a queue until the printer handles them – Information packets on networks - Packets enqueued until the router can route them – …
63
2009 Pearson Education, Inc. All rights reserved. 63 Outline QueueInheritance Library.cs Class QueueInheritance derived from class (Linked) List
64
2009 Pearson Education, Inc. All rights reserved. 64 Outline QueueTest.cs (1 of 3 ) Fig. 26.17 | Testing class QueueInheritance. (Part 1 of 4.) Creating a QueueInheritance object.
65
2009 Pearson Education, Inc. All rights reserved. 65 Outline The program enqueues various values. An infinite while loop dequeues the elements until an EmptyListException is thrown. QueueTest.cs (2 of 3 )
66
2009 Pearson Education, Inc. All rights reserved. 66 Outline QueueTest.cs (3 of 3 )
67
2009 Pearson Education, Inc. All rights reserved. 67 The End of Slides Covered in CS 1120 (The following slides cover Trees —not covered in CS 1120)
68
2009 Pearson Education, Inc. All rights reserved. 68
69
2009 Pearson Education, Inc. All rights reserved. 69 ** SKIP ** 26.7 Trees Linked lists, stacks and queues are linear data structures Tree: non-linear, two-dimensional data structure Binary tree: – Each node contains data & two links (hence “binary in the name) : left link and right link – Root node: “top” node in a tree - Links refer to child nodes – Leaf node: node with no children B XA Y
70
2009 Pearson Education, Inc. All rights reserved. 70 ** SKIP ** 26.7 Trees (Cont.) Fig. 26.18 | Binary-tree graphical representation.
71
2009 Pearson Education, Inc. All rights reserved. 71 ** SKIP ** 26.7 Trees (Cont.) The root node is the first node in a binary tree. The left child is the first node in the left subtree, and the right child is the first node in the right subtree. The children of a specific node are called siblings. A node with no children is called a leaf node. Common Programming Error 26.2 Not setting to null the links in leaf nodes of a tree is a common logic error.
72
2009 Pearson Education, Inc. All rights reserved. 72 ** SKIP ** 26.7 Trees Binary search tree: – Values in left subtree are less than the value of the subtree’s parent – Values in right subtree are greater than the value of the subtree’s parent J AP M 47 2577 11659343
73
2009 Pearson Education, Inc. All rights reserved. 73 ** SKIP ** 26.7.1 Binary Search Tree of Integer Values Traversal: method of retrieving data from a tree – In these methods if there is a subtree, recursively the traversal is called recursively Kinds of traversals: – Inorder traversal: - Get data from left subtree/child of node - Get data from node - Get data from right subtree/child of node Example (figure) Inorder traversal: 11 25 43 47 65 77 93 47 2577 11659343
74
2009 Pearson Education, Inc. All rights reserved. 74 ** SKIP ** 26.7.1 Binary Search Tree of Integer Values – Preorder traversal: - Get data from node - Get data from left subtree/child of node - Get data from right subtree/child of node - Example: 47 25 11 43 77 65 93 – Postorder traversal - Get data from left subtree/child of node - Get data from right subtree/child of node - Get data from node - Example: 11 43 25 65 93 77 47 – Level-order traversal - Visit nodes of tree row by row, from left to right - Example: 47 25 77 11 43 65 93 47 2577 11659343
75
2009 Pearson Education, Inc. All rights reserved. 75 ** SKIP ** 26.7.1 Trees Fig. 23.15A graphical representation of a binary tree. B AD C Reference to the tree
76
2009 Pearson Education, Inc. All rights reserved. 76 ** SKIP ** 26.7.1 Trees Fig. 23.16 A binary search tree containing 12 values. 47 2577 11 717 6593 68 43 3144
77
2009 Pearson Education, Inc. All rights reserved. 77 Outline BinaryTree Library.cs (1 of 7 ) ** SKIP ** The application of Figs. 26.20 and 26.21 creates a binary search tree and traverses it using recursive inorder, preorder and postorder traversals. Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 1 of 7.)
78
2009 Pearson Education, Inc. All rights reserved. 78 Outline BinaryTree Library.cs (2 of 7 ) Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 2 of 7.) Initially, every TreeNode is a leaf node with null children. Insert recursively determines the location for the new node in the binary search tree. ** SKIP **
79
2009 Pearson Education, Inc. All rights reserved. 79 Outline BinaryTree Library.cs (3 of 7 ) Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 3 of 7.) Insert recursively determines the location for the new node in the binary search tree. root stores the root node of the tree. ** SKIP **
80
2009 Pearson Education, Inc. All rights reserved. 80 Outline BinaryTree Library.cs (4 of 7 ) Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 4 of 7.) Insert a new node in the tree. Traversing the array (the process is explained later). ** SKIP **
81
2009 Pearson Education, Inc. All rights reserved. 81 Outline BinaryTree Library.cs (5 of 7 ) Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 5 of 7.) Traversing the array (the process is explained later). ** SKIP **
82
2009 Pearson Education, Inc. All rights reserved. 82 Outline BinaryTree Library.cs (6 of 7 ) Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 6 of 7.) Traversing the array (the process is explained later). ** SKIP **
83
2009 Pearson Education, Inc. All rights reserved. 83 Outline BinaryTree Library.cs (7 of 7 ) Fig. 26.20 | Declaration of class TreeNode and class Tree. (Part 7 of 7.) ** SKIP **
84
2009 Pearson Education, Inc. All rights reserved. 84 Outline TreeTest.cs (1 of 3 ) Fig. 26.21 | Testing class Tree with a binary tree. (Part 1 of 3.) ** SKIP **
85
2009 Pearson Education, Inc. All rights reserved. 85 Outline TreeTest.cs (2 of 3 ) Fig. 26.21 | Testing class Tree with a binary tree. (Part 2 of 3.) ** SKIP **
86
2009 Pearson Education, Inc. All rights reserved. 86 Outline TreeTest.cs (3 of 3 ) Fig. 26.21 | Testing class Tree with a binary tree. (Part 3 of 3.) ** SKIP **
87
2009 Pearson Education, Inc. All rights reserved. 87 26.7.1 Trees (Cont.) Consider the binary search tree shown in Fig. 26.22. Fig. 26.22 | Binary search tree ** SKIP **
88
2009 Pearson Education, Inc. All rights reserved. 88 26.7.1 Trees (Cont.) The inorder traversal of a binary search tree displays the node values in ascending order. Method InorderHelper defines the steps for an inorder traversal: 1.If the argument is null, do not process the tree. 2.Traverse the left subtree with a call to InorderHelper. 3.Process the value in the node. 4.Traverse the right subtree with a call to InorderHelper. ** SKIP **
89
2009 Pearson Education, Inc. All rights reserved. 89 26.7.1 Trees (Cont.) The preorder traversal processes the value in each node, then the values in the left subtree, then the values in the right subtree. Method PreorderHelper defines the steps for a preorder traversal: 1.If the argument is null, do not process the tree. 2.Process the value in the node. 3.Traverse the left subtree with a call to PreorderHelper. 4.Traverse the right subtree with a call to PreorderHelper. ** SKIP **
90
2009 Pearson Education, Inc. All rights reserved. 90 26.7.1 Trees (Cont.) The postorder traversal processes the value in each node after the values of all that node’s children are processed. Method PostorderHelper defines the steps for a postorder traversal: 1.If the argument is null, do not process the tree. 2.Traverse the left subtree with a call to PostorderHelper. 3.Traverse the right subtree with a call to PostorderHelper. 4.Process the value in the node. ** SKIP **
91
2009 Pearson Education, Inc. All rights reserved. 91 26.7.1 Trees (Cont.) A binary search tree facilitates duplicate elimination. – The insertion operation recognizes a duplicate value, because a duplicate is placed the same as the original value Searching a binary tree for a value that matches a key value is fast, especially for tightly packed binary trees – In a tightly packed binary tree, each level contains about twice as many elements as the previous level. ** SKIP **
92
2009 Pearson Education, Inc. All rights reserved. 92 ** SKIP ** 26.7.2 Binary Search Tree of IComparable Objects Can use polymorphism to manipulate objects of different types in uniform ways – Binary search trees can be implemented to manipulate data of any object that implements the IComparable interface Implementation of IComparable defines: – CompareTo method - E.g.: public void Insert( IComparable insertValue ) { … if ( insertValue.CompareTo( data ) < 0 ) … } – insertValue.CompareTo( data ) returns value: - < 0 if insertValue < data - = 0 if they are equal - > 0 if insertValue > data
93
2009 Pearson Education, Inc. All rights reserved. 93 26.7 Trees (Cont.) 26.7.2 Binary Search Tree of IComparable Objects We would like to define the functionality of a binary tree once and reuse it for many data types. Classes that implement IComparable define method CompareTo, which compares the object that invokes the method with the object received as an argument. ** SKIP **
94
2009 Pearson Education, Inc. All rights reserved. 94 Outline BinaryTree Library2.cs (1 of 7 ) The program of Figs. 26.23 and 26.24 enhances the program to manipulate IComparable objects. Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 1 of 7.) A few lines are changed to switch to use IComparable objects. ** SKIP **
95
2009 Pearson Education, Inc. All rights reserved. 95 Outline BinaryTree Library2.cs (2 of 7 ) Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 2 of 7.) Method CompareTo is now used to place items in the tree. ** SKIP **
96
2009 Pearson Education, Inc. All rights reserved. 96 Outline BinaryTree Library2.cs (3 of 7 ) Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 3 of 7.) ** SKIP **
97
2009 Pearson Education, Inc. All rights reserved. 97 Outline BinaryTree Library2.cs (4 of 7 ) Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 4 of 7.) ** SKIP **
98
2009 Pearson Education, Inc. All rights reserved. 98 Outline BinaryTree Library2.cs (5 of 7 ) Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 5 of 7.) ** SKIP **
99
2009 Pearson Education, Inc. All rights reserved. 99 Outline BinaryTree Library2.cs (6 of 7 ) Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 6 of 7.) ** SKIP **
100
2009 Pearson Education, Inc. All rights reserved. 100 Outline BinaryTree Library2.cs (7 of 7 ) Fig. 26.23 | Declaration of class TreeNode and class Tree. (Part 7 of 7.) ** SKIP **
101
2009 Pearson Education, Inc. All rights reserved. 101 Outline TreeTest.cs (1 of 5 ) Class TreeTest (Fig. 26.24) creates Tree objects to store int, double and string values. The Tree of string s is stored in alphabetical order. Fig. 26.24 | Testing class Tree with IComparable objects. (Part 1 of 5.) ** SKIP **
102
2009 Pearson Education, Inc. All rights reserved. 102 Outline TreeTest.cs (2 of 5 ) Fig. 26.24 | Testing class Tree with IComparable objects. (Part 2 of 5.) PopulateTree adds an Array of values to a Tree. ** SKIP **
103
2009 Pearson Education, Inc. All rights reserved. 103 Outline TreeTest.cs (3 of 5 ) Fig. 26.24 | Testing class Tree with IComparable objects. (Part 3 of 5.) PopulateTree adds an Array of values to a Tree. TraverseTree outputs the preorder, inorder and postorder traversals of the Tree. ** SKIP **
104
2009 Pearson Education, Inc. All rights reserved. 104 Outline TreeTest.cs (4 of 5 ) Fig. 26.24 | Testing class Tree with IComparable objects. (Part 4 of 5.) ** SKIP **
105
2009 Pearson Education, Inc. All rights reserved. 105 Outline TreeTest.cs (5 of 5 ) Fig. 26.24 | Testing class Tree with IComparable objects. (Part 5 of 5.) ** SKIP **
106
2009 Pearson Education, Inc. All rights reserved. 106 The End of all slides for Ch. 26 (ed.3)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.