From 3d83e8aa2ba5fb7e8072b12506ac1f94faa9ad50 Mon Sep 17 00:00:00 2001 From: Showndarya Date: Sun, 8 Apr 2018 12:28:28 +0530 Subject: [PATCH 1/3] Add solution for BinarySearchTree --- .../solutions/BinarySearchTree.java | 123 ++++++++++++++++++ .../binarytree/solutions/BinaryTree.java | 79 +++++++++++ Session08/binarytree/solutions/TreeNode.java | 43 ++++++ 3 files changed, 245 insertions(+) create mode 100644 Session08/binarytree/solutions/BinarySearchTree.java create mode 100644 Session08/binarytree/solutions/BinaryTree.java create mode 100644 Session08/binarytree/solutions/TreeNode.java diff --git a/Session08/binarytree/solutions/BinarySearchTree.java b/Session08/binarytree/solutions/BinarySearchTree.java new file mode 100644 index 0000000..a8f3d17 --- /dev/null +++ b/Session08/binarytree/solutions/BinarySearchTree.java @@ -0,0 +1,123 @@ +// This class fails to compile, can you figure out why, and fix it? +// This is beacuse BinaryTree has a constructor which is not default. Hence, when we extend this class and try to call a no-argument constructor, it'll give a compile-time error. so, we call it by passing the parameters (to instantiate the root node) in super() called inside the constructor of BinarySearchTree + +class BinarySearchTree extends BinaryTree { + + private int flag = 0; + + public BinarySearchTree() + { + super(0); + } + + // insert node + @Override + public boolean insert(int value) { + super.root = insert(super.root, value); + return true; + } + + private TreeNode insert(TreeNode root, int data) + { + // if current node is null, insert node with data + if (root == null) root = new TreeNode(data); + else + { + // traverse to left sub-tree if value to be inserted is lesser than value in current node + if (data <= node.getData()) root.left = insert(root.left, data); + // traverse to right sub-tree if value to be inserted is greater than value in current node + else root.right = insert(root.right, data); + } + return root; + } + + // delete node + @Override + public boolean delete(int value) { + super.root = deleteRec(super.root, value); + if(flag==1) {flag=0;return false;} + else return true; + } + + TreeNode deleteRec(TreeNode root, int key) + { + // if tree is empty, set flag to 1 and return root + if (root == null) {flag=1;return root;} + + // traverse to left sub-tree if value to be inserted is lesser than value in current node + if (key < root.data) root.left = deleteRec(root.left, key); + // traverse to right sub-tree if value to be inserted is greater than value in current node + else if (key > root.data) root.right = deleteRec(root.right, key); + + // if value is same as current value, delte it + else + { + // node with only one child or no child + if (root.left == null) return root.right; + else if (root.right == null) return root.left; + + // if node has two children, get the lowest inorder successor from the right subtree + root.data = minValue(root.right); + // Delete the inorder successor + root.right = deleteRec(root.right, root.data); + } + return root; + } + + // find the lowest value in the tree by reaching the leftmost leaf node + int minValue(TreeNode root) + { + int minv = root.data; + while (root.left != null) + { + minv = root.left.data; + root = root.left; + } + return minv; + } + + // check if node exists + @Override + public boolean exists(int find) { + TreeNode tn = search(super.root,find); + if(flag==1) {flag=0;return false;} + else return true; + } + + public TreeNode search(TreeNode root, int key) + { + // not found + if(root==null) + { + flag=1; + return root; + } + // if found, return root + if (root.data==key) return root; + + // traverse to left sub-tree if value to be inserted is lesser than value in current node + if (root.data > key) return search(root.left, key); + // traverse to right sub-tree if value to be inserted is greater than value in current node + return search(root.right, key); + } + + // lowest common ancestor + @Override + public TreeNode lowestCommonAncestor(int value1, int value2) { + + TreeNode tn = lca(super.root, value1,value2); + if(flag==1) return null; + else return tn; + } + + // the node value present in between a and b is the lca. Assuming, ab && root.data>a) lca(root.left,a,b); + else if(root.data Date: Tue, 17 Apr 2018 19:15:22 +0530 Subject: [PATCH 2/3] Updated solution for Binary Search Tree --- .../solutions/BinarySearchTree.java | 108 ++++++++++-------- .../binarytree/solutions/BinaryTree.java | 26 ++--- Session08/binarytree/solutions/TreeNode.java | 25 ---- 3 files changed, 75 insertions(+), 84 deletions(-) diff --git a/Session08/binarytree/solutions/BinarySearchTree.java b/Session08/binarytree/solutions/BinarySearchTree.java index a8f3d17..0544b56 100644 --- a/Session08/binarytree/solutions/BinarySearchTree.java +++ b/Session08/binarytree/solutions/BinarySearchTree.java @@ -1,57 +1,69 @@ // This class fails to compile, can you figure out why, and fix it? -// This is beacuse BinaryTree has a constructor which is not default. Hence, when we extend this class and try to call a no-argument constructor, it'll give a compile-time error. so, we call it by passing the parameters (to instantiate the root node) in super() called inside the constructor of BinarySearchTree -class BinarySearchTree extends BinaryTree { +/* This is beacuse BinaryTree has a constructor which is not default. +Hence, when we extend this class and try to call a no-argument constructor, it'll give a compile-time error. +So, we call it by passing the parameters (to instantiate the root node) in super() called inside the constructor of BinarySearchTree. +*/ - private int flag = 0; +class BinarySearchTree extends BinaryTree { - public BinarySearchTree() - { - super(0); + public BinarySearchTree() { + // initialize root to null + super(-1); } - // insert node @Override public boolean insert(int value) { super.root = insert(super.root, value); - return true; + if(exists(value) == true) return true; + else return false; } - private TreeNode insert(TreeNode root, int data) - { + public TreeNode insert(TreeNode root, int data) { // if current node is null, insert node with data - if (root == null) root = new TreeNode(data); - else - { - // traverse to left sub-tree if value to be inserted is lesser than value in current node - if (data <= node.getData()) root.left = insert(root.left, data); + if (root == null) { + root = new TreeNode(data); + } + + else { + // traverse to left sub-tree if value to be inserted is lesser than or equal to the value in the current node + if (data <= root.getData()) root.left = insert(root.left, data); // traverse to right sub-tree if value to be inserted is greater than value in current node else root.right = insert(root.right, data); } return root; } - // delete node @Override public boolean delete(int value) { - super.root = deleteRec(super.root, value); - if(flag==1) {flag=0;return false;} - else return true; + TreeNode node = deleteRecursive(super.root, value); + if(node == null) { + return false; + } + else { + super.root = node; + return true; + } } - TreeNode deleteRec(TreeNode root, int key) - { - // if tree is empty, set flag to 1 and return root - if (root == null) {flag=1;return root;} + public TreeNode deleteRecursive(TreeNode root, int key){ + // if tree is empty, return null + if (root == null) { + return null; + } // traverse to left sub-tree if value to be inserted is lesser than value in current node - if (key < root.data) root.left = deleteRec(root.left, key); + if (key < root.data) { + root.left = deleteRecursive(root.left, key); + } + // traverse to right sub-tree if value to be inserted is greater than value in current node - else if (key > root.data) root.right = deleteRec(root.right, key); + else if (key > root.data) { + root.right = deleteRecursive(root.right, key); + } - // if value is same as current value, delte it - else - { + // if value is same as current value, delete it + else { // node with only one child or no child if (root.left == null) return root.right; else if (root.right == null) return root.left; @@ -59,61 +71,65 @@ TreeNode deleteRec(TreeNode root, int key) // if node has two children, get the lowest inorder successor from the right subtree root.data = minValue(root.right); // Delete the inorder successor - root.right = deleteRec(root.right, root.data); + root.right = deleteRecursive(root.right, root.data); } return root; } // find the lowest value in the tree by reaching the leftmost leaf node - int minValue(TreeNode root) - { + int minValue(TreeNode root) { int minv = root.data; - while (root.left != null) - { + while (root.left != null) { minv = root.left.data; root = root.left; } return minv; } - // check if node exists @Override public boolean exists(int find) { TreeNode tn = search(super.root,find); - if(flag==1) {flag=0;return false;} - else return true; + if(tn == null) { + return false; + } + else { + return true; + } } public TreeNode search(TreeNode root, int key) { // not found - if(root==null) - { - flag=1; - return root; + if(root==null) { + return null; } // if found, return root - if (root.data==key) return root; + if (root.data==key) { + return root; + } // traverse to left sub-tree if value to be inserted is lesser than value in current node - if (root.data > key) return search(root.left, key); + if (root.data > key) { + return search(root.left, key); + } // traverse to right sub-tree if value to be inserted is greater than value in current node return search(root.right, key); } - // lowest common ancestor @Override public TreeNode lowestCommonAncestor(int value1, int value2) { TreeNode tn = lca(super.root, value1,value2); - if(flag==1) return null; + if(tn == null) return null; else return tn; } - // the node value present in between a and b is the lca. Assuming, ab && root.data>a) lca(root.left,a,b); else if(root.data Date: Tue, 17 Apr 2018 22:28:57 +0530 Subject: [PATCH 3/3] Tweaking in code style --- Session08/binarytree/solutions/BinarySearchTree.java | 11 +++++------ Session08/binarytree/solutions/TreeNode.java | 3 +-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Session08/binarytree/solutions/BinarySearchTree.java b/Session08/binarytree/solutions/BinarySearchTree.java index 0544b56..479048b 100644 --- a/Session08/binarytree/solutions/BinarySearchTree.java +++ b/Session08/binarytree/solutions/BinarySearchTree.java @@ -46,7 +46,7 @@ public boolean delete(int value) { } } - public TreeNode deleteRecursive(TreeNode root, int key){ + public TreeNode deleteRecursive(TreeNode root, int key) { // if tree is empty, return null if (root == null) { return null; @@ -97,8 +97,7 @@ public boolean exists(int find) { } } - public TreeNode search(TreeNode root, int key) - { + public TreeNode search(TreeNode root, int key) { // not found if(root==null) { return null; @@ -124,9 +123,9 @@ public TreeNode lowestCommonAncestor(int value1, int value2) { else return tn; } - // The node value present in between a and b is the lca. Assuming, a