删除功能赢得';t工作(java)
我们正在写一个项目,在这个项目中,我们使用链表在BST中添加和减去数字。然而,我们可以添加数字,但不能删除它们。有人能看看我们的删除功能看看有什么问题吗删除功能赢得';t工作(java),java,Java,我们正在写一个项目,在这个项目中,我们使用链表在BST中添加和减去数字。然而,我们可以添加数字,但不能删除它们。有人能看看我们的删除功能看看有什么问题吗 // 2016 Spring Data Structure BST Sample Code at MIU (For the project #2) // You may add/modify the classes but should maintain the basic structure of the classes. import ja
// 2016 Spring Data Structure BST Sample Code at MIU (For the project #2)
// You may add/modify the classes but should maintain the basic structure of the classes.
import java.util.*;
import java.util.*;
class Node {
int data; // data
Node lc; //left child pointer
Node rc; //right child pointer
Node(int data){ // constructor
this.data=data;
lc=null;
rc=null;
}
}
class BST {
Node root;
BST(){
root=null;
}
void insert(int newData){ // This is not the full version
Node newNode=new Node(newData);
if(root==null) root=newNode; // root was null
else { // root was not null, so find the place to attach the new node
Node ptr=root; // Node pointer
while(true){
if(newNode.data==ptr.data) {
System.out.println("The node already exist.");
break;
}
else if(newNode.data<ptr.data) {
if(ptr.lc!=null) ptr=ptr.lc;
else {
ptr.lc=newNode;
System.out.println(newNode.data+" was successfully inserted.");
break;
}
}
else if(newNode.data>ptr.data) {
if(ptr.rc!=null) ptr=ptr.rc;
else {
ptr.rc=newNode;
System.out.println(newNode.data+" was successfully inserted.");
break;
}
}
}
}
}
Node delete(Node root,int key){ // delete the nodes
if (root == null) return null;
if (root.data > key) {
root.lc = delete(root.lc, key);
}
else if (root.data < key) {
root.rc = delete(root.rc, key);
}
return null;
}
boolean search(Node root,int key){ // if the search is successful return the Node or return null
if(root==null){
return false;
}
if(root.data== key) {
return true;
}
if(root.data!= key){
return false;
}
return true;
}
int number_nodes(Node n){ // the counting left child
if(n == null)
return 0;
if(n.lc ==null && n.rc==null)
return 1;
else
return 1+number_nodes(n.lc);
}
int rnumber_nodes(Node n){ // the counting right child
if(n == null)
return 0;
if(n.lc ==null && n.rc==null)
return 1;
else
return 1+number_nodes(n.rc);
}
void inorder(Node n){ // recursive inorder travelsal
if(n==null) return;
System.out.print("[");
inorder(n.lc);
System.out.print(n.data);
inorder(n.rc);
System.out.print("]");
}
void preorder(Node n){ // recursive preorder travelsal
if(n==null) return;
System.out.print("[");
System.out.print(n.data);
preorder(n.lc);
preorder(n.rc);
System.out.print("]");
}
void postorder(Node n){ // recursive postorder travelsal
if(n==null) return;
System.out.print("[");
postorder(n.lc);
postorder(n.rc);
System.out.print(n.data);
System.out.print("]");
}
}
public class BST_2015134 { // change the name into your IDs
public static void main(String[] args) {
BST bst=new BST();
Scanner sScan=new Scanner(System.in); // menu
Scanner iScan=new Scanner(System.in); // data
while(true){
System.out.print("\n(q)uit,(i)nsert,(d)elete,(s)earch,i(n)order,(p)reorder,p(o)storder,(h)ow many:");
String uChoice=sScan.next();
if(uChoice.equalsIgnoreCase("i")){
System.out.print("Enter a number to insert:");
int uData=iScan.nextInt();
bst.insert(uData);
}
else if(uChoice.equalsIgnoreCase("d")){ // deletion
System.out.print("enter the delete number");
Scanner s=new Scanner(System.in); // to use new scanner
int delete_num=s.nextInt(); //
bst.delete(bst.root,delete_num);
}
else if(uChoice.equalsIgnoreCase("s")){ // search
System.out.print("enter the search number");
Scanner s=new Scanner(System.in);
int search_num=s.nextInt();
if(bst.search(bst.root,search_num)){
while(true){
System.out.println(" your number is found"); // to tell you your # is found or not found
break;
}
}
else{
System.out.println(" your number is not found");
}
}
else if(uChoice.equalsIgnoreCase("n")){ // in order
bst.inorder(bst.root);
}
else if(uChoice.equalsIgnoreCase("p")){ // pre order
bst.preorder(bst.root);
}
else if(uChoice.equalsIgnoreCase("o")){ // post order
bst.postorder(bst.root);
}
else if(uChoice.equalsIgnoreCase("h")){ // how many
int x,y;
x=bst.number_nodes(bst.root);
y=bst.rnumber_nodes(bst.root);
int total=x+y;
System.out.print(total);
}
if(uChoice.equalsIgnoreCase("q")) break; // quit
}
}
}
//MIU的2016 Spring数据结构BST示例代码(用于项目#2)
//您可以添加/修改类,但应保持类的基本结构。
导入java.util.*;
导入java.util.*;
类节点{
int data;//数据
节点lc;//左子指针
节点rc;//右子指针
节点(int数据){//constructor
这个。数据=数据;
lc=null;
rc=null;
}
}
BST级{
节根;
BST(){
root=null;
}
void insert(int newData){//这不是完整版本
Node newNode=新节点(newData);
如果(root==null)root=newNode;//root为null
else{//root不是null,因此请找到附加新节点的位置
Node ptr=root;//节点指针
while(true){
如果(newNode.data==ptr.data){
System.out.println(“节点已经存在。”);
打破
}
else if(newNode.dataptr.data){
如果(ptr.rc!=null)ptr=ptr.rc;
否则{
ptr.rc=newNode;
System.out.println(newNode.data+“已成功插入”);
打破
}
}
}
}
}
节点删除(节点根,int键){//删除节点
if(root==null)返回null;
如果(root.data>key){
root.lc=删除(root.lc,键);
}
else if(root.data
仅简要介绍一下您使用此功能所做的工作:
如果键预期包含在左子树中,请使用递归从左子树中删除If。如果该键预期包含在右子树中,则使用递归从左子树中删除If到现在为止,一直都还不错。然而,y
Node delete(Node root,int key){ // delete the nodes
if (root == null) return null;
if (root.data > key) {
root.lc = delete(root.lc, key);
}
else if (root.data < key) {
root.rc = delete(root.rc, key);
}else{
if(root.rc == null)
return root.lc;
if(root.lc == null)
return root.rc;
//if both the left and right child are effective, you need to restructure the tree
int minData = findMinimum(root.rc);
Node newNode = new Node(minData);
newNode.lc = root.lc;
newNode.rc = delete(root.rc, minData);
return newNode;
}
return root;
}
private int findMinimum(Node root){
if (root.lc == null)
return root.data;
else
return findMinimum(root.lc);
}