quiz حل الأسئلة الجامعية manage_search الأرشيف

تم الحل ✓
categoryبرمجة وتطوير البرمجيات schoolبكالوريوس event_available2026-07-15

السؤال

Transcribed Image Text:

2 3 4 5 6 7 8 // Fig. 21.15: Tree.java // TreeNode and Tree class declarations for a binary search tree. package com.deitel.datastructures; // class TreeNode definition class TreeNode<E extends Comparable<E>> { //package access members TreeNode<E> leftNode; E data; //node value TreeNode<E> rightNode; // constructor initializes data and makes this a leaf node public TreeNode(E nodeData) { 9 10 12 13 14 15 16 } 17 data nodeData; leftNode rightNode = null; // node has no children Fig. 21.15 TreeNode and Tree class declarations for a binary search tree. (Part 1 of 6.) Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved 18 19 20 21 22 23 // locate insertion point and insert new node; ignore duplicate values public void insert(E insertValue) { // insert in left subtree if (insertValue.compareTo(data) < 0) { // insert new TreeNode if (leftNode = null) { leftNode = new TreeNode<E>(insertValue); else { // continue traversing left subtree recursively leftNode.insert(insertValue); // insert in right subtree else if (insertvalue.compareTo(data) > 0) { 24 25 } 26 27 28 } 29 } 30 31 32 33 34 35 ) 36 37 38 ) 39 // insert new TreeNode if (rightNode = null) { rightNode new TreeNode<E>(insertValue); else { // continue traversing right subtree recursively rightNode.insert(insertValue); 40 41 } Fig. 21.15 TreeNode and Tree class declarations for a binary search tree. (Part 2 of 6.) Copyright 1992-2018 by Fearson Education, Inc All Rights Reserved 42 43 44 45 46 47 48 49 // class Tree definition public class Tree<E extends Comparable<E>> { private TreeNode<E> root; // constructor initializes an empty Tree of integers public Tree() {root = null;} // insert a new node in the binary search tree public void insert Node (E insertValue) { if (root null) { 50 51 52 53 54 } 55 56 57 } 58 } 59 root new TreeNode<E>(insertValue); // create root node else { root.insert(insertValue); // call the insert method Fig. 21.15 TreeNode and Tree class declarations for a binary search tree. (Part 3 of 6.) Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. 60 61 62 63 64 65 66 67 68 69 70 71 // begin preorder traversal public void preorderTraversal () (preorderHelper(root);} // recursive method to perform preorder traversal private void preorderHelper (TreeNode<E> node) { if (node == null) { return; }) System.out.printf("%s", node.data); // output node data preorderHelper(node.leftNode); // traverse left subtree preorderHelper (node. rightNode); // traverse right subtree 72 73 } Fig. 21.15 TreeNode and Tree class declarations for a binary search tree. (Part 4 of 6.) Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved. 74 75 76 77 78 79 80 // begin inorder traversal public void inorderTraversal () (inorderHelper(root); } // recursive method to perform inorder traversal private void inorderHelper (TreeNode<E> node) { if (node=null) { return; 81 } 82 83 84 85 86 } inorderHelper(node.leftNode); // traverse left subtree System.out.printf("%s", node.data); // output node data inorderHelper(node. rightNode); // traverse right subtree 87 Fig. 21.15 TreeNode and Tree class declarations for a binary search tree. (Part 5 of 6.) Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved 88 89 90 91 92 93 // begin postorder traversal public void postorderTraversal () (postorderHelper(root);} // recursive method to perform postorder traversal private void postorderHelper (TreeNode<E> node) { if (node == null) { return; 94 95 } 96 97 98 99 100 101 } } postorderHelper(node.leftNode); // traverse left subtree postorderHelper (node.rightNode); // traverse right subtree System.out.printf("%s", node.data); // output node data Fig. 21.15 TreeNode and Tree class declarations for a binary search tree. (Part 6 of 6.) Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Fig. 21.16: TreeTest.java // Binary tree test program. import java.security. SecureRandom; import com.deitel.datastructures. Tree; public class TreeTest { public static void main(String[] args) { Tree<Integer> tree = new Tree<Integer>(); SecureRandom randomNumber = new SecureRandom(); System.out.println("Inserting the following values: "); //insert 10 random integers from 0-99 in tree for (int i=1; i <= 10; i++) { } int value randomNumber.nextInt(100); System.out.printf("%d ", value); tree.insertNode(value); Fig. 21.16 Binary tree test program. (Part 1 of 3.) © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved 19 20 21 22 23 24 25 26 27 28 29 } 30 } System.out.printf("%n %n Preorder traversal %n"); tree.preorderTraversal(); System.out.printf("%n %nInorder traversal %n"); tree. inorderTraversal(); System.out.printf("%n % n Postorder traversal %n"); tree.postorderTraversal(); System.out.println(); Fig. 21.16 Binary tree test program. (Part 2 of 3.) Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserve Inserting the following values: 49 64 14 34 85 64 46 14 37 55 Preorder traversal 49 14 34 46 37 64 55 85 Inorder traversal 14 34 37 46 49 55 64 85 Postorder traversal 37 46 34 14 55 85 64 49 Fig. 21.16 | Binary tree test program. (Part 3 of 3.) Consistent with the operations and capabilities demonstrated in Fig. 21.15 "Tree.java" and Fig. 21.16 "TreeTest.java" presented in Deitel's "Java How to Program Book" Chapter 21 and its presentation, create an Eclipse Java project to implement this example with Comparable<Student> objects, for which you also need to write a "Student.java" class that implements Comparable<Student> interface. The Student.java class should define a first name, a last name, and an ID of appropriate data types and the necessary methods such as getters, constructor, equals(), compareTo() and toString(). Demonstrate in the driver class "TreeTest.java" by adding at least 5 sample mock up Student objects to a Binary Tree of Students. Similar to the example's output, display what Student objects are inserted to the binary tree, display the pre-order traversal, in-order traversal, and post-order traversal results.

check_circle الجواب — حل مفصل خطوة بخطوة

hourglass_top