Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class programm_02 implements Runnable {
- public static void main(String[] args) {
- new Thread(null, new programm_02(), "", 64 * 1024 * 1024).start();
- }
- public class Knot {
- private long key;
- private Knot left;
- private Knot right;
- public Knot(long key) {
- this.key = key;
- }
- }
- public class Tree {
- private Knot root;
- public Tree() {
- }
- public void insert(long x) {
- Knot parent = null;
- Knot knot = root;
- while (knot != null) {
- if (x < knot.key) {
- parent = knot;
- knot = knot.left;
- } else if (x > knot.key) {
- parent = knot;
- knot = knot.right;
- } else {
- return;
- }
- }
- Knot newKnot = new Knot(x);
- if (x < parent.key) {
- parent.left = newKnot;
- } else if (x > parent.key) {
- parent.right = newKnot;
- }
- }
- public void create(Scanner sc) {
- if (root == null) {
- root = new Knot(sc.nextLong());
- }
- while (sc.hasNext()) {
- insert(sc.nextLong());
- }
- }
- public void bypass(Knot knot, FileWriter fw) throws IOException {
- if (knot != null) {
- fw.write(knot.key + "\n");
- bypass(knot.left, fw);
- bypass(knot.right, fw);
- }
- }
- public void delete (long key) {
- Knot knot = root;
- Knot parent = null;
- if(key == root.key && knot.right == null && knot.left != null) {
- root = knot.left;
- return;
- } else if (key == root.key && knot.right != null && knot.left == null) {
- root = knot.right;
- return;
- }
- while (true) {
- if (key < knot.key) {
- parent = knot;
- knot = knot.left;
- if (knot.key == key && (knot.left == null && knot.right == null)) {
- parent.left = null;
- return;
- } else if (knot.key == key && (knot.left == null && knot.right != null)) {
- parent.left = knot.right;
- return;
- }
- else if (knot.key == key && (knot.left != null && knot.right == null)) {
- parent.left = knot.left;
- return;
- }
- } else if (key > knot.key) {
- parent = knot;
- knot = knot.right;
- if (knot.key == key && (knot.left == null && knot.right == null)) {
- parent.right = null;
- return;
- } else if (knot.key == key && (knot.left == null && knot.right != null)) {
- parent.right = knot.right;
- return;
- }
- else if (knot.key == key && (knot.left != null && knot.right == null)) {
- parent.right = knot.left;
- return;
- }
- } else if (key == knot.key && knot.left != null && knot.right != null) {
- Knot newRoot = knot;
- knot = knot.right;
- if(knot.left != null) {
- while (knot.left != null) {
- knot = knot.left;
- }
- long temp = knot.key;
- delete(knot.key);
- newRoot.key = temp;
- return;
- }
- else {
- newRoot.key = knot.key;
- newRoot.right = knot.right;
- return;
- }
- }
- }
- }
- public boolean check (long key) {
- Knot knot = root;
- while(knot != null) {
- if(key < knot.key) {
- knot = knot.left;
- } else if (key > knot.key) {
- knot = knot.right;
- } else if (key == knot.key) {
- return true;
- }
- }
- return false;
- }
- }
- public void run() {
- FileInputStream fin = null;
- FileWriter fw = null;
- try {
- fin = new FileInputStream("input.txt");
- Scanner sc = new Scanner(fin);
- long deletedKey = sc.nextLong();
- fw = new FileWriter("output.txt");
- Tree tree = new Tree();
- tree.create(sc);
- if (tree.check(deletedKey) == true) {
- tree.delete(deletedKey);
- tree.bypass(tree.root, fw);
- fw.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement