From 47bac8fe22cca2f426edbd1e95a88ce8cc1fb3e3 Mon Sep 17 00:00:00 2001 From: Sandeepdhungana <70063857+Sandeepdhungana@users.noreply.github.com> Date: Thu, 1 Oct 2020 09:48:45 +0530 Subject: [PATCH 1/2] Update BST.java --- src/com/tree/BST.java | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/com/tree/BST.java b/src/com/tree/BST.java index 1dec1c1..950932e 100644 --- a/src/com/tree/BST.java +++ b/src/com/tree/BST.java @@ -3,44 +3,45 @@ public class BST { Node root; public BST() { - root=null; + root = null; } - - public Node NodeCreate(int value){ +// this function returns the new node by settingvalue to value and child to be null. + public Node NodeCreate(int value) { return new Node(value, null, null); } - public void add(Node start, Node newNode){ - if(root==null){ - root=newNode; +// function to add the elements in BST. + public void add(Node start, Node newNode) { + if(root == null){ + root = newNode; return; } - if(newNode.value> start.value){ - if( start.right==null) - start.right=newNode; - add(start.right,newNode); + if(newNode.value > start.value) { + if( start.right == null) + start.right = newNode; + add(start.right, newNode); } - if(newNode.value< start.value){ - if( start.left==null) - start.left=newNode; - add(start.left,newNode); + if(newNode.value < start.value) { + if( start.left == null) + start.left = newNode; + add(start.left, newNode); } } - -public void Search(int value, Node start){ +// function to search the given value using recursion. +public void Search(int value, Node start) { - if(start==null){ - System.out.println("node isnot found"); + if(start == null) { + System.out.println("node is not found"); return; } - if(start.value==value) + if(start.value == value) { System.out.println("node is found"); return; } - if( value>start.value){ + if( value > start.value) { Search(value, start.right); } - if( value Date: Thu, 1 Oct 2020 09:53:43 +0530 Subject: [PATCH 2/2] Update LinkedListU.java --- src/com/ds/LinkedListU.java | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/com/ds/LinkedListU.java b/src/com/ds/LinkedListU.java index b990203..5a4bbbc 100644 --- a/src/com/ds/LinkedListU.java +++ b/src/com/ds/LinkedListU.java @@ -3,26 +3,29 @@ public class LinkedListU { Node head; public LinkedListU() { - head= null; + head = null; } -public void add(Object value){ - Node newNode=new Node(value, null); - if(head==null) - head=newNode; - else{ +// function to add value of type object and its children in linked list. +public void add(Object value) { + Node newNode = new Node(value, null); + if(head == null) + head = newNode; + else { newNode.next=head; - head= newNode; + head = newNode; } } +// function to delete node of linked list. public void delete(){ - head=head.next; + head = head.next; } +// function to display elements of the linked list. public void display(){ - Node n=head; - while(n!=null){ + Node n = head; + while(n != null){ System.out.println((T)n.value); - n=n.next; + n = n.next; } }