For this project, you will do a practice of Binary Search Tree (BST) based on a given framework and implement the action of insert/delete a node. After you download the provided framework and unzip the file, three files are extracted: (1) MainGUI.java (2) Node.java (3) Tree.java.
(1) Use Eclipse to create a JavaFX project by clicking menu File -> New -> Project. This is the same as you did on previous JavaFX projects.
(2) Copy the three files above to the source folder of your newly created projects src/application/ folder. By default, after you create the project, you have Main.java and application.css files in the folder. You can just delete the Main.java (leave the application.css as it is) before copying the three files into it.
(3) Then refresh the application package in the Eclipse. You should be able to run the code as shown in Figure-1:
Figure-1: Binary Search Tree User Interface see image.
The GUI allows you to add a node into the tree by simply typing a number (with one or two digits only) and hitting ENTER key or clicking the add button. At the moment, you cannot see a node showing up since you havent implemented the node insertion function. After a successful implementation, you should be able to see nodes appear on the canvas as Figure-2 shows:
Figure-2: Result of successful implementation of insertNode() function see image.
Also, you can change the node position freely by simply dragging the node on canvas to move it to any preferred position as Figure-3 shows. But it is recommended not to move both the left and right children to one side of the parent or above the parent, which makes the tree structure less meaningful to the concept of BST. Every time when a node is inserted or selected, it is highlighted by a thin yellow ring around the node. You can remove such highlight at any time by Right Clicking or Left Clicking your mouse on any blank area of the canvas. Also, you can use the mouse to choose or highlight any node by left clicking on the node directly.
Figure-3: Demo of dragging or moving a node to any position see image.
(1) Implement the insertNode(Node node) function to add a node into the tree. Make sure no repeating value nodes are allowed to be inserted, e.g. for the tree in the Figure-3, if you insert 39 again, no any changes should occur on the tree, since 39 already exists.
(2) Implement the deleteNode(Node node) function.
With the existing framework, you need to focus on modifying the code in the Tree.java file. You do NOT need to do any change on the MainGUI.java. However, you may write some code (optional) for the Node.java depending on your specific way of implementation. However, I did not change anything on Node.java in my implementation. For the Node.java and Tree.java files, each file has a pair of comment lines:
/******************** Implementation Here: ***************************/
/******************** End of Implementation ***************************/
(1) Please follow the comment in the code, which describes the framework and the part for your implementation in details.
(2) For the Node.java, the data members you may use for the assignment are separated from the ones for the GUI part. So the data members you need to know are at the top of the class, such as left, right, etc. In fact, for this class, you just need to use these data members and do not need any additional coding.
(3) For the given Node data memebers, you may notice other than assigning left or right child, you also need to assign other relationship such as parent and left_child_of_parent variables. These variables are important as they allow the nodes to be accessed easily across the tree. The GUI also needs this information to draw the tree on the canvas. So when you modify the code, make sure set these variables accordingly.