1 / 23

Chapter 10- B Trees

Modified. Chapter 10- B Trees. Chapter Scope. LinkedBinaryTree implementation Decision Tree Example. Binary Trees. A Binary Tree ADT. Decision Trees. A decision tree is a tree whose nodes represent decision points, and whose children represent the options available

amal-boyer
Download Presentation

Chapter 10- B Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modified Chapter 10- B Trees

  2. Chapter Scope • LinkedBinaryTree implementation • Decision Tree Example Java Software Structures, 4th Edition, Lewis/Chase

  3. Binary Trees Java Software Structures, 4th Edition, Lewis/Chase

  4. A Binary Tree ADT Java Software Structures, 4th Edition, Lewis/Chase

  5. Decision Trees • A decision tree is a tree whose nodes represent decision points, and whose children represent the options available • The leaves of a decision tree represent the possible conclusions that might be drawn • A simple decision tree, with yes/no questions, can be modeled by a binary tree • Decision trees are useful in diagnostic situations (medical, car repair, etc.) Java Software Structures, 4th Edition, Lewis/Chase

  6. Decision Trees • A simplified decision tree for diagnosing back pain: Java Software Structures, 4th Edition, Lewis/Chase

  7. /** * BackPainAnaylyzer demonstrates the use of a binary decision * tree to diagnose back pain. */ public class BackPainAnalyzer { /** * Asks questions of user to diagnose a medical problem. */ public static void main (String[] args) throws FileNotFoundException { System.out.println ("So, you're having back pain."); DecisionTree expert = new DecisionTree("input.txt"); expert.evaluate(); } } Java Software Structures, 4th Edition, Lewis/Chase

  8. /** * The DecisionTree class uses the LinkedBinaryTree class to * implement a binary decision tree. Tree elements are read from a * given file and then the decision tree can be evaluated based on * user input using the evaluate method. */ public class DecisionTree { private LinkedBinaryTree<String> tree; /** * Builds the decision tree based on contents of the given file * @param filename the name of the input file * @throws FileNotFoundException if the input file is not found */ public DecisionTree(String filename) throws FileNotFoundException { File inputFile = new File(filename); Scanner scan = new Scanner(inputFile); Java Software Structures, 4th Edition, Lewis/Chase

  9. intnumberNodes = scan.nextInt(); scan.nextLine(); int root = 0, left, right; // Read node strings into binary tree (nodes) and // store the trees in an ArrayList List<LinkedBinaryTree<String>> nodes = new java.util.ArrayList<LinkedBinaryTree<String>>(); for (inti = 0; i < numberNodes; i++) nodes.add(i,newLinkedBinaryTree<String>(scan.nextLine())); // Read tree structure pointers (indexes) in and assemble // subtrees into one decision tree. while (scan.hasNext()) { root = scan.nextInt(); left = scan.nextInt(); right = scan.nextInt(); scan.nextLine(); nodes.set(root, new LinkedBinaryTree<String>( (nodes.get(root)).getRootElement(), nodes.get(left), nodes.get(right))); } tree = nodes.get(root); } Java Software Structures, 4th Edition, Lewis/Chase

  10. /** * Follows the decision tree based on user responses. */ public void evaluate() { LinkedBinaryTree<String> current = tree; Scanner scan = new Scanner(System.in); // Follow a path from root to leaf based on input while (current.size() > 1) // Not a leaf { System.out.println (current.getRootElement()); if (scan.nextLine().equalsIgnoreCase("N")) current = current.getLeft(); else current = current.getRight(); } System.out.println (current.getRootElement()); } } Java Software Structures, 4th Edition, Lewis/Chase

  11. Implementing Binary Trees with Links • Now let's explore an implementation of a binary tree using links • The LinkedBinaryTreeclass holds a reference to the root node • The BinaryTreeNodeclass represents each node, with links to a possible left and/or right child Java Software Structures, 4th Edition, Lewis/Chase

  12. /** BinaryTreeNode represents a node in a binary tree with a * left and right child. */ public class BinaryTreeNode<T> { protected T element; protected BinaryTreeNode<T> left, right; //Child references /** * Creates a new tree node with the specified data. * @paramobj = element {data} part of the new node */ public BinaryTreeNode(Tobj) { element = obj; left = null; right = null; } element right left Java Software Structures, 4th Edition, Lewis/Chase

  13. /** Creates a new tree node with the specified data and subtrees * @paramobj = element (data) of the new tree node * @param left = tree that will be left subtree of this node * @param right = tree that will be right subtree of this node */ public BinaryTreeNode(T obj, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right ) { element = obj; if (left == null) this.left = null; else this.left = left.getRootNode(); if (right == null) this.right = null; else this.right = right.getRootNode(); } Java Software Structures, 4th Edition, Lewis/Chase

  14. /** Returns the number of non-null children of this node. * @return = the number of non-null children of this node */ public intnumChildren() { int children = 0; if (left != null) children = 1 + left.numChildren(); if (right != null) children = children + 1 + right.numChildren(); return children; } /** Return the element at this node. * @return the element stored at this node */ public T getElement() { return element; } Java Software Structures, 4th Edition, Lewis/Chase

  15. /** * Return the right child of this node. * @return the right child of this node */ public BinaryTreeNode<T> getRight() { return right; } /** * Sets the right child of this node. * @param node the right child of this node */ public void setRight(BinaryTreeNode<T> node) { right = node; } Java Software Structures, 4th Edition, Lewis/Chase

  16. /** * Return the left child of this node. * * @return the left child of the node */ public BinaryTreeNode<T> getLeft() { return left; } /** * Sets the left child of this node. * * @param node the left child of this node */ public void setLeft(BinaryTreeNode<T> node) { left = node; } } Java Software Structures, 4th Edition, Lewis/Chase

  17. /** LinkedBinaryTree implements the BinaryTreeADT interface */ public class LinkedBinaryTree<T> implements BinaryTreeADT<T>, Iterable<T> { protected BinaryTreeNode<T> root; protected intmodCount; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } Java Software Structures, 4th Edition, Lewis/Chase

  18. /** Creates a binary tree with the specified element as its root. * @param element = (data) element for the root node of the tree */ public LinkedBinaryTree(T element) { root = new BinaryTreeNode<T>(element); } /** Creates a binary tree with the specified element as its root * and the given trees as its left child and right child * * @param element = (data) element that for the root of the tree * @param left the left subtree of this tree * @param right the right subtree of this tree */ public LinkedBinaryTree(T element, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right ) { root = new BinaryTreeNode<T>(element); root.setLeft(left.root); root.setRight(right.root); } Java Software Structures, 4th Edition, Lewis/Chase

  19. /** Returns a reference to the specified target element * if it is found in this binary tree. Throws a * ElementNotFoundException if the specified target * element is not found in the binary tree. * * @paramtargetElement the element being sought in this tree * @return a reference to the specified target * @throws ElementNotFoundException if element not in tree */ public T find(T targetElement) throws ElementNotFoundException { BinaryTreeNode<T> current = findNode(targetElement, root); if (current == null) throw new lementNotFoundException("LinkedBinaryTree"); return (current.getElement()); } Java Software Structures, 4th Edition, Lewis/Chase

  20. /** * Returns a reference to the specified target element if it is * found in this binary tree. * * @paramtargetElement the element being sought in this tree * @param next the element to begin searching from */ private BinaryTreeNode<T> findNode(TtargetElement, BinaryTreeNode<T> next) { if (next == null) return null; if (next.getElement().equals(targetElement)) return next; BinaryTreeNode<T> temp = findNode(targetElement, next.getLeft()); if (temp == null) temp = findNode(targetElement, next.getRight()); return temp; } Java Software Structures, 4th Edition, Lewis/Chase

  21. /** Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with the root. * @return an in order iterator over this binary tree */ public Iterator<T> iteratorInOrder() { ArrayUnorderedList<T>tempList = new ArrayUnorderedList<T>(); inOrder(root, tempList); return new TreeIterator(tempList.iterator()); } /** Performs a recursive inorder traversal. * @param node the node to be used as the root for this traversal * @paramtempList the temporary list for use in this traversal */ protected void inOrder(BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) { if (node != null) { inOrder(node.getLeft(), tempList); tempList.addToRear(node.getElement()); inOrder(node.getRight(), tempList); } } Java Software Structures, 4th Edition, Lewis/Chase

  22. /** * Performs a levelorder traversal on this binary tree, using a * templist. * * @return a levelorder iterator over thisbinarytree */ public Iterator<T> iteratorLevelOrder() { ArrayUnorderedList<BinaryTreeNode<T>> nodes = new ArrayUnorderedList<BinaryTreeNode<T>>(); ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>(); BinaryTreeNode<T> current; nodes.addToRear(root); Java Software Structures, 4th Edition, Lewis/Chase

  23. while (!nodes.isEmpty()) { current = nodes.removeFirst(); if (current != null) { // add current element to tempList tempList.addToRear(current.getElement()); // enqueue children if (current.getLeft() != null) nodes.addToRear(current.getLeft()); if (current.getRight() != null) nodes.addToRear(current.getRight()); } else tempList.addToRear(null); } return new TreeIterator(tempList.iterator()); } Java Software Structures, 4th Edition, Lewis/Chase

More Related