1 / 20

Binary Tree search animation

Binary Tree search animation. Act 1: Looking for something in the tree. root. a 0. a 0. left data right. 6. a 2. a 1. a 1. a 2. left data right. left data right. 7. 3. a 4. a 3. a 3. a 4. left data right. left data right. 4. 2.

Download Presentation

Binary Tree search animation

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. Binary Tree search animation (c) Eraj Basnayake

  2. Act 1: Looking for something in the tree (c) Eraj Basnayake

  3. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  4. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 searchHelper target node a0 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  5. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 searchHelper target node a0 4 searchHelper target node a1 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  6. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 searchHelper target node a0 4 searchHelper target node a1 4 searchHelper target node a4 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  7. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 searchHelper target node a0 4 searchHelper target node a1 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class true (c) Eraj Basnayake

  8. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 searchHelper target node a0 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class true (c) Eraj Basnayake

  9. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class true (c) Eraj Basnayake

  10. the end Act 2: Looking for something not in the tree (c) Eraj Basnayake

  11. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 5 in the tree. search item 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  12. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 5 in the tree. search item 5 searchHelper target node a0 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  13. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 5 searchHelper target node a0 5 searchHelper target node a1 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  14. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 5 searchHelper target node a0 5 searchHelper target node a1 5 searchHelper target node a4 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  15. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 5 searchHelper target node a0 5 searchHelper target node a1 5 searchHelper target node a4 5 searchHelper target node 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class (c) Eraj Basnayake

  16. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 5 searchHelper target node a0 5 searchHelper target node a1 5 searchHelper target node a4 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class false (c) Eraj Basnayake

  17. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 5 searchHelper target node a0 5 searchHelper target node a1 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class false (c) Eraj Basnayake

  18. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 5 searchHelper target node a0 5 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class false (c) Eraj Basnayake

  19. root a0 a0 left data right 6 a2 a1 a1 a2 left data right left data right 7 3 a4 a3 a3 a4 left data right left data right 4 2 Searching for the value 4 in the tree. search item 4 public class BST implements BinarySearchTree{ … public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); } }//class false (c) Eraj Basnayake

  20. the end (c) Eraj Basnayake

More Related