Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: ValorMaisProximoBST #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
| 65 | [Rotações **BST**](https://github.com/viniciusbds/tst-eda/blob/master/RotacoesBST/README.md) | [Java](https://github.com/viniciusbds/tst-eda/tree/master/RotacoesBST/RotacoesBST.java) |
| 66 | [Elementos Maiores **BST**](https://github.com/viniciusbds/tst-eda/blob/master/ElementosMaioresBST/README.md) | [Java](https://github.com/viniciuswps/tst-eda/tree/master/ElementosMaioresBST/ElementosMaioresBST.java) |
| 67 | [Remoção **BST**](https://github.com/viniciusbds/tst-eda/blob/master/RemocaoBST/README.md) | [Java](https://github.com/viniciuswps/tst-eda/tree/master/RemocaoBST/RemocaoBST.java) |
| 68 | [Valor Mais Próximo **BST**](https://github.com/viniciusbds/tst-eda/blob/master/ValorMaisProximoBST/README.md) | Java |
| 68 | [Valor Mais Próximo **BST**](https://github.com/viniciusbds/tst-eda/blob/master/ValorMaisProximoBST/README.md) | [Java](https://github.com/viniciusbds/tst-eda/blob/master/ValorMaisProximoBST/ValorMaisProximoBST.java) |
| 69 | Altura de uma Árvore | Not implemented |
| 71 | Árvores similares | Not implemented |
| 72 | Lista de Adjacencia | Not implemented |
Expand Down
130 changes: 127 additions & 3 deletions ValorMaisProximoBST/ValorMaisProximoBST.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,127 @@
class ValorMaisProximoBST {
// implementar
}
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class ValorMaisProximoBST {

static int min_diff, min_diff_key;

/* A binary tree node has key, pointer to left child
and a pointer to right child */
static class Node
{
int key;

Node left, right;
};

/* Utility that allocates a new node with the
given key and null left and right pointers. */

static Node newnode(int key)
{

Node node = new Node();
node.key = key;
node.left = node.right = null;
return (node);
}

// Function to find node with minimum absolute
// difference with given K
// min_diff -. minimum difference till now
// min_diff_key -. node having minimum absolute
// difference with K
static void maxDiffUtil(Node ptr, int k)
{
if (ptr == null)
return ;

// If k itself is present
if (ptr.key == k)
{
min_diff_key = k;
return;
}

// update min_diff and min_diff_key by checking
// current node value
if (min_diff > Math.abs(ptr.key - k))
{
min_diff = Math.abs(ptr.key - k);
min_diff_key = ptr.key;
}

// if k is less than ptr.key then move in
// left subtree else in right subtree
if (k < ptr.key)
maxDiffUtil(ptr.left, k);
else
maxDiffUtil(ptr.right, k);
}

// Wrapper over maxDiffUtil()
static int maxDiff(Node root, int k)
{
// Initialize minimum difference
min_diff = 999999999; min_diff_key = -1;

// Find value of min_diff_key (Closest key
// in tree with k)
maxDiffUtil(root, k);

return min_diff_key;
}

static Node createTree(Node root, int number)
{
if (root == null) {
return newnode(number);
}

if (number < root.key) {
root.left = createTree(root.left, number);
} else if (number > root.key) {
root.right = createTree(root.right, number);
} else {
return root;
}

return root;
}

static ArrayList<Integer> printPreorder(Node node)
{
ArrayList<Integer> list = new ArrayList<>();
if (node == null)
return list;

/* first print data of node */
list.add(node.key);

/* then recur on left sutree */
list.addAll(printPreorder(node.left));

/* now recur on right subtree */
list.addAll(printPreorder(node.right));

return list;
}

// Driver program to run the case
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String[] numbers = scan.nextLine().split(" ");
int k = Integer.parseInt(scan.nextLine());

Node root = newnode(Integer.parseInt(numbers[0]));
for (int i = 1; i < numbers.length; i++) {
root = createTree(root, Integer.parseInt(numbers[i]));
}

System.out.println(Arrays.toString(printPreorder(root).toArray()));
System.out.println(maxDiff(root, k));

}
}