TORONTO KIDS COMPUTER CLUB | PMCA Sunday 12:00 Java Homework 20.04.26.
17424
post-template-default,single,single-post,postid-17424,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.10.0,vc_responsive

PMCA Sunday 12:00 Java Homework 20.04.26.

29 Apr PMCA Sunday 12:00 Java Homework 20.04.26.

Continue with the node in the previous class and write the method

In the class Tree.java  to delete a node from the tree.

Please find the  code below:

Driver.java
import java.util.Arrays;

public class Driver {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Tree t=new Tree();
		Node one=new Node(5);
		t.insertToBinarySearchTree(one);
		
		Node two=new Node(8);
		t.insertToBinarySearchTree(two);

		Node three=new Node(2);
		t.insertToBinarySearchTree(three);
		
		t.printTree(1);
		
		// test code to delete a node
		t.deleteNode(5);
		
		t.printTree(1);
	}

}

Tree.java

public class Tree {
	Node parentNode=null;
	
	/*
	 * Method to print  the tree
	 * if the type is 1 than it will do preordertraversal
	 * if type is 2 than it will do postordertraveral
	 * if type is 3 than it will do inorderTraversal
	 * */
	public void printTree(int type) {
		if(type==1) {
			preOrderTraversal(parentNode);
		}
		else if(type==2) {
			postOrderTraversal(parentNode);
		}
		else {
			inorderTraversal(parentNode);
		}
	}
	public void preOrderTraversal(Node currentNode) {
		if(currentNode==null) {
			return;
		}
		System.out.println(currentNode.number);
		
		preOrderTraversal(currentNode.LeftChild);
		preOrderTraversal(currentNode.RightChild);
		
	}
	// copy it 
	public void postOrderTraversal(Node currentNode) {
		if(currentNode==null) {
			return;
		}
		postOrderTraversal(currentNode.LeftChild);
		postOrderTraversal(currentNode.RightChild);
		System.out.println(currentNode.number);

	}
	
	public void inorderTraversal(Node currentNode) {
		if(currentNode==null) {
			return;
		}
		inorderTraversal(currentNode.LeftChild);
		System.out.println(currentNode.number);
		inorderTraversal(currentNode.RightChild);

	}

	// driver function to insertToBinarySearchTree
	public void insertToBinarySearchTree(Node node) {
		if(parentNode==null) {
			parentNode=node;
		}
		else {
			insertNode(node,parentNode);
		}
	}
	
	// code to insertNode to the tree
	private void insertNode(Node node,Node currentNode) {

		if(currentNode.number<node.number ) {
			if(currentNode.RightChild==null) {
				currentNode.RightChild=node;
				return;
			}
			else {
				insertNode(node, currentNode.RightChild);
			}
		}
		else {

			if(currentNode.LeftChild==null) {
				currentNode.LeftChild=node;
				return ;
			}
			else {
				insertNode(node,currentNode.LeftChild);
			}
		}
	}
	
	// write the code to delete a node from Binary search tree
	
	public static void deleteNode(int number) {
		//find the node with the number and delete the node
		//write the code here
	}
}


Node.java
public class Node {

	int number;
	Node LeftChild;
	Node RightChild;
	Node Parent;
	public Node(int number) {
		this.number=number;
	}
}
No Comments

Sorry, the comment form is closed at this time.