Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Delete root node in Binary Search Tree

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Delete root node in Binary Search Tree

    Hi everyone,

    I'm learning data structure and this is the example code from the textbook.

    package lab7.part2;
     
    public class BinarySearchTree_Pham {
        TreeNode root;
     
        public BinarySearchTree_Pham() {
            root = null;
        }
     
        public boolean insert(Student_Pham newStudent) {
            TreeNodeWrapper p = new TreeNodeWrapper();
            TreeNodeWrapper c = new TreeNodeWrapper();
            TreeNode n = new TreeNode();
            if(n == null)
                return false;
            else {
                n.node = newStudent.deepCopy();
                n.lc = null;
                n.rc = null;
                if(root == null) {
                    root = n;
                }
                else {
                    findNode(newStudent.getId(), p, c);
                    if(newStudent.getId().compareTo(p.get().node.getId()) < 0)
                        p.get().lc = n;
                    else
                        p.get().rc = n;
                }
                return true;
            }
        } //end insert
     
        public Student_Pham fetch(String id) {
            boolean found;
            TreeNodeWrapper p = new TreeNodeWrapper();
            TreeNodeWrapper c = new TreeNodeWrapper();
            found = findNode(id, p, c);
            if (found == true)
                return c.get().node.deepCopy();
            else
                return null;
        } //end fetch
     
        public boolean delete(String id) {
            boolean found;
            TreeNodeWrapper p = new TreeNodeWrapper();
            TreeNodeWrapper c = new TreeNodeWrapper();
            TreeNode largest;
            TreeNode nextLargest;
            found = findNode(id, p, c);
            if(found == false)
                return false;
            else {
                if(c.get().lc == null && c.get().rc == null) {
                    if (p.get().lc == c.get())
                        p.get().lc = null;
                    else
                        p.get().rc = null;
                } //end case 1
                else if (c.get().lc == null || c.get().rc == null) {
                    if (p.get().lc == c.get()) {
                        if (c.get().lc != null)
                            p.get().lc = c.get().lc;
                        else
                            p.get().lc = c.get().rc;
                    }
                    else {
                        if (c.get().lc != null)
                            p.get().rc = c.get().lc;
                        else
                            p.get().rc = c.get().rc;
                    }
                } // end case 2
                else {
                    nextLargest = c.get().lc;
                    largest = nextLargest.rc;
                    if (largest != null) {
                        while (largest.rc != null) {
                            nextLargest = largest;
                            largest = largest.rc;
                        }
                        c.get().node = largest.node;
                        nextLargest.rc = largest.lc;
                    }
                    else {
                        nextLargest.rc = c.get().rc;
                        if (p.get().lc == c.get())
                            p.get().lc =nextLargest;
                        else
                            p.get().rc = nextLargest;
                    }
                } // end case 3
                return true;
            }
        } // end of delete
     
        public boolean update(String id, Student_Pham newStudent) {
            if(delete(id) == false)
                return false;
            else if (insert(newStudent) == false)
                return false;
            return true;
        } // end update
     
        public void showAll() {
            if (root == null)
                System.out.println("Structure is empty.");
            else
                LNRoutputTraversal(root);
        } //end showAll
     
        private void LNRoutputTraversal(TreeNode root) {
            if (root.lc != null)
                LNRoutputTraversal(root.lc);
            System.out.println(root.node);
            if (root.rc != null)
                LNRoutputTraversal(root.rc);
        }
     
        public class TreeNode {
            private Student_Pham node;
            private TreeNode lc;
            private TreeNode rc;
            public TreeNode() {}
        }
     
        private boolean findNode(String targetKey, TreeNodeWrapper parent, TreeNodeWrapper child) {
            parent.set(root);
            child.set(root);
            if (root == null)
                return true;
            while (child.get() != null) {
                if(child.get().node.getId().compareTo(targetKey) == 0)
                    return true;
                else {
                    parent.set(child.get());
                    if(targetKey.compareTo(child.get().node.getId()) < 0)
                        child.set(child.get().lc);
                    else
                        child.set(child.get().rc);
                }
            } //end while
            return false;
        }
     
        public class TreeNodeWrapper {
            TreeNode treeRef = null;
            public TreeNodeWrapper() {}
            public TreeNode get() {return treeRef;}
            public void set(TreeNode t) {treeRef = t;}
        }
    }

    It cannot handle deleting root node in the tree and my task is to modify it to make it delete root node. Can anyone help me?

    Thank you

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Delete root node in Binary Search Tree

    Do you have the algorithm or list of steps the program must take to solve the problem?
    Once you have that you can try to write the java code to implement it.

    If you have any problems writing the code, post the steps for the algorithm in English and ask the questions you have about writing the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with my Binary tree. (Node links are incorrect) Please!
    By Jacksla3 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 12:19 PM
  2. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  3. Height of a node in a binary tree
    By ueg1990 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 19th, 2012, 01:17 AM
  4. Binary Tree Search[HELP]
    By husain2213 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2012, 11:33 PM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM

Tags for this Thread