• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Issues with correctly linking the child to the parent node and linking it back to its ancestor?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

I am having issues with correctly linking the child to the parent node and linking it back to its ancestor. Here is a copy of the code that I submitted for reference:




Also, here are the error messages and feedback that I received:

1. Invalid tree with right child linking to ancestor with this feedback: checkBSTValidity() returned a node that is not the BST rule-violating node. The node was either the left or right child pointing to an ancestor must be returned.

2. Invalid tree with left child linking to parent with this feedback: checkBSTValidity() returned a node that is not the BST rule-violating node.

3. Invalid tree with left child linking to ancestor with this feedback: checkBSTValidity() returned a node that is not the BST rule-violating node.



Please let me know if anything else is needed. Thank you-

 
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Peta,

can you give us the code for the Node-class as well?
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Peta,

can you give us the code for the Node-class as well?

Yeah. Node should have a property named "parent" or something like that.
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 here are the error messages and feedback that I received:


Where do those error messages come from?
 
Peta Brady
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Here is a copy of the read-only code for the Node.java file:




Also, here is a copy of the ready-only LabProgram,java code; in case anyone requests it for reference.



 
Peta Brady
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies, the error messages come from the IDE and here is a copy of the Node.java and LabProgram.java in its proper format. Both files are read-only. Thank you-





 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on that, Node is intended for a doubly-linked list. To form a tree, you'd need a parent-node pointer and no such property exists for that class. So you're out of luck.

You could subclass your Node class and put a parent property in it, but Node by itself isn't designed for that.
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear to me why you need a list of ancestors to validate a tree. Simply walking the tree recursively and comparing the current key with the left and right keys should be sufficient. Am I missing something?
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the input string for running the program?

What program generated the messages?  
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

having issues with correctly linking the child to the parent node and linking it back to its ancestor.


Where in your code are you doing any linking of nodes?  The only places I see that being done is in the Read-Only code which I assume was provided by your instructor.

Do you have a description of what your code is supposed to do?
 
Peta Brady
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Yes, I am working on a lab assignment. I made some adjustments and now have two errors. Here is a copy of the assignment, the code that I modified, and errors listed below:

Step 1: Inspect the Node.java file

Inspect the class declaration for a BST node in Node.java. Access Node.java by clicking on the orange arrow next to LabProgram.java at the top of the coding window. Each node has a key, a left child reference, and a right child reference.
Step 2: Implement the BSTChecker.checkBSTValidity() method

Implement the checkBSTValidity() method in the BSTChecker class in the BSTChecker.java file. The method takes the tree's root node as a parameter and returns the node that violates BST requirements, or null if the tree is a valid BST.

A violating node X will meet one or more of the following conditions:

   X is in the left subtree of ancestor Y, but X's key is > Y's key
   X is in the right subtree of ancestor Y, but X's key is < Y's key
   X's left or right child references an ancestor

Note: Other types of BST violations can occur, but are not covered in this lab.

The given code in LabProgram.java reads and parses input, and builds the tree for you. Nodes are presented in the form (key, leftChild, rightChild), where leftChild and rightChild can be nested nodes or "None". A leaf node is of the form (key). After parsing tree input, the BSTChecker.checkBSTValidity() method is called and the returned node's key, or "No violation", is printed.

Ex:
Tree 1 with 60 node violation

If the input is:

(50, (25, None, (60)), (75))

which corresponds to the tree above, then the output is:

60

because 60 violates BST requirements by being in the left subtree of 50.

Ex:
Tree 1 with 60 node violation

If the input is:

(20, (10), (30, (29), (31)))

which corresponds to the tree above, then the output is:

No violation

because all BST requirements are met.

The input format doesn't allow creating a tree with a node's child referencing an ancestor, so unit tests are used to test such cases.




BSTChecker.java:85: error: 'else' without 'if'
      else if ("Right". equals(direction) && child.key <= parent.key) {
      ^
BSTChecker.java:91: error: reached end of file while parsing
}
^
2 errors
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those compiler errors have to do with the placement of {...}.
 
Piet Souris
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Peta,

did you manage to get a correct parsing of the inputstring, and a correct check of the validity of the tree?

The validity checking does not need much knowledge of ancestors, though. For instance, say we have this tree
                       5
                 /                \
           3                       8
      /     \                   /        \
  1          4               6         10
/  \                           \
0    2                          7


if you look at the left node 6, then you see its key must be larger than the ancestor of its ancestor, but smaller than its ancestor. Likewise, for the rightnode 4, it must be larger than its ancestor, but smaller that the ancestors ancestor.

So you will soon start to think of the methods

and you can check the validity with something like:

and remember that a null node is always valid.
 
Peta Brady
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone for sharing their insight and efforts to assist me as I learn Java. I did manage to fix the compiler errors. Based on the great recommendations, I made some adjustments and submitted the following code:




I ran the code through IntelliJ IDEA, but the IDE in the lab is different so I am learning how to inspect the file and fix the mistakes.  It has been cumbersome, but I feel like it is helping me avoid "the loop"
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic