PTA 二叉搜索树的结构 Java

PTA 二叉搜索树的结构 Java

在这里插入图片描述
在这里插入图片描述

直接建立一个二叉搜索树
在这里插入图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NavigableSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int n = Reader.nextInt();
		HashMap<Integer, Node> map = new HashMap<Integer, Node>();
		Node root = new Node();
		root.data = Reader.nextInt();
		root.fa = null;
		map.put(root.data, root);
		for (int i = 1; i < n; i++) {
			Node temp = new Node();
			temp.data = Reader.nextInt();
			map.put(temp.data, temp);
			insert(root, temp);
		}
		int m = Reader.nextInt();
		int a,b;
		for (int i = 0; i < m; i++) {
			String str = Reader.nextLine();
			String[] s = str.split(" ");
			try {
				switch (s.length) {
				case 4: // root
					a = Integer.valueOf(s[0]);
					if(map.get(a).fa == null) {
						System.out.println("Yes");
					}else {
						System.out.println("No");
					}
					break;
				case 5:// siblings
					a = Integer.valueOf(s[0]);
					b = Integer.valueOf(s[2]);
					if(map.get(a).fa == map.get(b).fa) {
						System.out.println("Yes");
					}else {
						System.out.println("No");
					}
					break;
				case 6:
					a = Integer.valueOf(s[0]);
					b = Integer.valueOf(s[5]);
					if(map.get(b).fa.data == a) {
						System.out.println("Yes");
					}else {
						System.out.println("No");
					}
					break;
				case 7:
					a = Integer.valueOf(s[0]);
					b = Integer.valueOf(s[6]);
					if(s[3].equals("left")) {
						if(map.get(b).left.data == a) {
							System.out.println("Yes");
						}else {
							System.out.println("No");
						}
					}else {
						if(map.get(b).right.data == a) {
							System.out.println("Yes");
						}else {
							System.out.println("No");
						}
					}
					break;
				case 8:
					a = Integer.valueOf(s[0]);
					b = Integer.valueOf(s[2]);
					int count = 0;
					Node current = map.get(a);
					while(current.fa != null) {
						count++;
						current = current.fa;
					}
					current = map.get(b);
					while(current.fa != null) {
						count--;
						current = current.fa;
					}
					if(count == 0) {
						System.out.println("Yes");
					}else {
						System.out.println("No");
					}
					break;
				default:
					break;
				}
			} catch (Exception e) {
				System.out.println("No");
			}

		}
	}

	static class Node {
		int data;
		Node fa = null;
		Node left = null;
		Node right = null;
	}

	static void insert(Node root, Node val) {
		if (val.data < root.data) {
			if (root.left == null) {
				root.left = val;
				val.fa = root;
			} else {
				insert(root.left, val);
			}
		} else {
			if (root.right == null) {
				root.right = val;
				val.fa = root;
			} else {
				insert(root.right, val);
			}
		}
	}

}

// Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	static void init(File file) {
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static char nextChar() throws IOException {
		return next().toCharArray()[0];
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}

}

发布了27 篇原创文章 · 获赞 2 · 访问量 1474

猜你喜欢

转载自blog.csdn.net/Samil_Hy/article/details/104197650