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 12 of 12

Thread: 234 tree search and insert

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 234 tree search and insert

    hello everyone i got this assignment.We have this piece of code and we must make a search for a key.if the key exist it returns true if not false.Plus we must insert a key in the class.if it is already in there we say hey its already in and we don t put it again
    ok first of all this is what our professor gave us
    package askisi2;
    import java.util.*;
    public class mtree {
       protected class tnode {
            public int k1;
            public int k2;
            public int k3;
            public int np;
            public tnode c0;
            public tnode c1;
            public tnode c2;
            public tnode c3;
            public tnode goneas;
            tnode () {
                np = 0;
                c0 = null; c1 = null; c2 = null;
                c3 = null; goneas=null;
            }
        }
       protected tnode root;
       protected int size;
       public mtree() {
           root=null; size=0;
       }
     
       public int treeSize() {
        return size;
    }
    and this is what i made
     public   boolean key_search(int key) {
           tnode node = root;
    		while (node != null) {
    			if (key < node.c1) {
    				node = node.c1;
    			} else if (key == node.c1) {
    				return true;
    			} else if ((node.keys == 1) || (key < node.key2)) {
    				node = node.c2;
    			} else if (key == node.c2) {
    				return true;
    			} else if ((node.keys == 2) || (key < node.key3)) {
    				node = node.c3;
    			} else if (key == node.key3) {
    				return true;
    			} else {
    				node = node.c4;
    			}
    		}
    		return false;
           }
     public  void inTree(int key)  {
     
       tnode node = root;
    		if (root == null) {
    			root = new mtree(key);
    			size++;
    			return;
    		}
    		if (node.keys == 3) { 
    			tnode par = new tnode(node.key2);
    			tnode left = node; 
    			tnode right = new tnode(par, node.key3);
    			par.c1 = left;
    			par.c2 = right;
    			if (node.c1 != null) {
    				par.c2.c1 = node.c3;
    				par.c2.c2 = node.child4;
    				par.c2.c1.goneas = right;
    				par.c2.c2.goneas = right;
    			}
    			par.c1.keys = 1;
    			par.c1.key2 = 0;
    			par.c1.key3 = 0;
    			par.c1.c3 = null;
    			par.c1.c4 = null;
    			par.c1.goneas = par;
    			root = par;
    			node = root;
    		}
    		while (node.c1 != null) { 
    			if (key < node.key1) {
    				node = node.c1;
    			} else if (key == node.key1) {
    				// found the same key as trying to insert - do nothing
                                    System.Out.println("iparxei to key pou dwsate")  
    				return;
    			} else if ((node.keys == 1) || (key < node.key2)) {
    				node = node.c2;
    			} else if (key == node.key2) {
    				System.Out.println("iparxei to key pou dwsate")
    				return;
    			} else if ((node.keys == 2) || (key < node.key3)) {
    				node = node.c3;
    			} else if (key == node.key3) {
    				System.Out.println("iparxei to key pou dwsate")
    				return;
    			} else {
    				node = node.c4;
    			}
    			if (node.keys == 3) {
    				tnode left = node; 
    				tnode par = node.parent;
    				tnode right = new tnode(node.key3);
    				right.c1 = node.c3;
    				right.c2 = node.c4;
    				if (par.keys == 1) { 
    					if (node.key2 < par.key1) {
    						par.key2 = par.key1;
    						par.key1 = node.key2;
    						par.c3 = par.c2;
    						par.c2 = right;
    						par.c2.goneas = par;
    						par.c1 = left;
    						par.c1.goneas = par;
    					} else {
    						par.key2 = node.key2;
    						par.c2 = left;
    						par.c2.goneas = par;
    						par.c3 = right;
    						par.c3.goneas = par;
    					}
    				} else {
    					if (node.key2 < par.key1) {
    						par.key3 = par.key2;
    						par.key2 = par.key1;
    						par.key1 = node.key2;
    						par.c4 = par.c3;
    						par.c3 = par.c2;
    						par.c2 = right;
    						par.c2.goneas = par;
    						par.c1 = left;
    						par.c1.goneas = par;
    					} else if (node.key2 > par.key2) {
    						par.key3 = node.key2;
    						par.c3 = left;
    						par.c3.goneas = par;
    						par.c4 = right;
    						par.c4.goneas = par;
    					} else { 
    						par.key3 = par.key2;
    						par.key2 = node.key2;
    						par.c4 = par.c3;
    						par.c2 = left;
    						par.c2.goneas = par;
    						par.c3 = right;
    						par.c3.goneas = par;
    					}
    				}
    				par.keys++
    				node.keys = 1;
    				node.key2 = 0;
    				node.key3 = 0;
    				node.c3 = null;
    				node.c4 = null;
     
    				node = par;
    			}
    		}
    		if (node.keys == 1) {
    			if (key < node.key1) {
    				node.key2 = node.key1;
    				node.key1 = key;
    			} else {
    				node.key2 = key;
    			}
    		} else {
    			if (key < node.key1) {
    				node.key3 = node.key2;
    				node.key2 = node.key1;
    				node.key1 = key;
    			} else if (key > node.key2) {
    				node.key3 = key;
    			} else {
    				node.key3 = node.key2;
    				node.key2 = key;	
    			}
    		}
    		node.keys++;
    		size++;
    	}
     
     
         }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 234 tree search and insert

    Do you have any questions about or problems with the program?

    What happens when you compile and execute it?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 234 tree search and insert

    i m not supposed to compile it and execute.i had a paper where i was asked to find on the web or think on my own a code for search and insert a key in the 234 tree(i study math so he doesn t really care about programming.he cares more about the theory behind the 234 which i fully understand)
    the professor gave us the first names of the classes/nodes etc and we must change them as such in the search and insert class
    he gave us the first java code i wrote.but i don t know if i wrote the names right and used the classes/variables etc in the second class i provided
    soz for my english but its not my main language

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 234 tree search and insert

    If you have no questions or problems, what are you looking for here?

    Is this a math problem or a java programming problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 234 tree search and insert

    the problem is that i don t know if i replaced the names in the second code correctly according to the ones he gave us in the first code
    let me explain
    he gave us all the names in the first code i provided(tnode c1,c0 ,np etc) and i was supposed to use them in the classes about insert and search (the second part of the codes i provided)did i put them correctly?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 234 tree search and insert

    did i put them correctly
    The normal way to tell if a program is coded correctly is to compile it and execute it and see if it does what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 234 tree search and insert

    Quote Originally Posted by Norm View Post
    The normal way to tell if a program is coded correctly is to compile it and execute it and see if it does what you want.
    yes i agree but this is not a complete program.its parts of a program that we don t have that s why i posted it here cause its harder to fix it and i needed all the help i could get!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 234 tree search and insert

    What do you expect to happen here?

    Complete the code so it will compile and execute.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 234 tree search and insert

    but we haven t learn how to make programs in class!all we had to do is switch the classes etc to match the starting one(the first i provided).i did it but i don t know if i put them correctly in the second one

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 234 tree search and insert

    we haven t learn how to make programs in class!
    Then you need to start learning how to make programs.

    Can you ask your instructor if the changes you have made are correct? That would be the best place to find out if the changes you made are what is expected.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 234 tree search and insert

    but that s our homework.i made the changes in the second program but i want to make sure i got em right

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 234 tree search and insert

    Can you ask your instructor if the changes you have made are correct?

    If we do your homework for you, would that be fair to the other students?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java B-Tree Insert (no libraries)
    By a1mz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2014, 06:01 AM
  2. Binary Tree insert(int value) method
    By geeksutopia in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 28th, 2014, 09:37 AM
  3. 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
  4. Replies: 1
    Last Post: August 4th, 2012, 06:55 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