200 likes | 322 Views
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.
E N D
Binary Tree search animation (c) Eraj Basnayake
Act 1: Looking for something in the tree (c) Eraj Basnayake
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
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
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
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
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
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
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
the end Act 2: Looking for something not in the tree (c) Eraj Basnayake
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
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
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
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
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
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
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
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
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
the end (c) Eraj Basnayake