ジョブレコードを学びます
二分木を作成し、ノードが規則に従っている、整数データに格納されているバイナリツリーのルートとして、以下左の子ノードの親ノードの値よりも第1の値は、親ノードの右の子ノードの値よりも大きいです。上記で作成したバイナリツリーに基づいて、クエリを実行し、入力された値は、その親ノード、子ノードの兄弟を見つけるために照会されます。出力バイナリプリアンブルシーケンスにおいて、トラバース結果ました。
入力フォーマット:
値は、整数入力、複数行の入力です。最初の行:バイナリツリー内のノードの数。二行目:ノードの値は、カンマで区切られました。第三の行:クエリにノード値。
出力フォーマット:
クエリに出力ノードの最初の行の親ノード。クエリに出力ノードの2行目の子ノード。第三の出力ライン兄弟。バイナリツリーの結果の振る舞い四次トラバーサル。第五行動バイナリツリーの先行順結果。バイナリツリーの第六行動の結果を横断した後。
サンプル入力:
ここでは、入力のセットを与えられています。たとえば、次の
8
5,2,9,11,4,23,10,16
23
出力例:
出力に対応し、ここで考えます。例えば:
親23 11
23 16無しの子供が右の子を残している
23人の兄弟と10
の前にシーケンス結果5,2,4,9,11,10,23,16
INORDER結果2,4,5,9,10、 11,16,23
、その後の結果4,2,10,16,23,11,9,5
サンプル入力2:
ここでは、入力のセットを与えられています。例えば:
1
7
7
出力サンプル2:
出力に対応し、ここで考えます。例えば:
ノー両親7人の
7なしの子供
7同胞の
プリアンブル7つの結果
の順序の結果で7
以降の結果7
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String str = sc.next();
String[] s = str.split(",");
int[] a = new int[num];
for (int i = 0; i < num; i++) {
a[i]=Integer.parseInt(s[i]);
}
int x=sc.nextInt();
BinaryTree b=new BinaryTree(a,num,x);
}
}
class TreeNode{
int value;
TreeNode parent;
TreeNode left;
TreeNode right;
TreeNode bro;
public TreeNode(int value){
this.value=value;
this.parent=null;
this.left=null;
this.right=null;
}
}
class BinaryTree{
public TreeNode rootNode;
public BinaryTree(int[]data,int num,int x){
for (int i=0;i<num;i++){
addNode(data[i]);
}
searchNode(rootNode,x);
int count =0;
System.out.print("前序结果");
PreOrder(rootNode);
System.out.println();
System.out.print("中序结果");
InOrder(rootNode,count);
System.out.println();
System.out.print("后序结果");
PostOrder(rootNode);
}
void addNode(int value){
TreeNode currentNode=rootNode;
if(rootNode==null){
rootNode=new TreeNode(value);
return;
}
while(true){
if(value<currentNode.value){
if(currentNode.left==null){
currentNode .left=new TreeNode(value);
currentNode.left.parent=currentNode;
return;
}else
currentNode =currentNode.left;
}else{
if(currentNode.right==null){
currentNode.right=new TreeNode(value);
currentNode.right.parent=currentNode;
return;
}else
currentNode =currentNode.right ;
}
}
}
public void searchNode(TreeNode node,int x) {
while (true) {
if (x < node.value) {
node = node.left;
} else if (x > node.value) {
node = node.right;
} else if (x == node.value) {
if (node.parent == null)
System.out.println(x + "无双亲");
else
System.out.println(x + "的双亲是" + node.parent.value);
if (node.left == null && node.right == null) {
System.out.println(x + "无孩子");
} else {
if (node.left == null)
System.out.println(x + "无左孩子右孩子是" + node.right.value);
else if (node.right == null) {
System.out.println(x + "的左孩子是" + node.left.value + "无右孩子");
} else
System.out.println(x + "的左孩子是" + node.left.value + "右孩子是" + node.right.value);
}
if(node.parent==null||node.parent.left==null||node.parent.right==null){
System.out.println(x+"无兄弟");
}else{
if(node.parent.right==node){
System.out.println(x+"的兄弟是"+node.parent.left.value);
}else
System.out.println(x+"的兄弟是"+node.parent.right.value);
}
break;
}
}
}
public void PreOrder(TreeNode node){
if(node!=null){
if(node==rootNode){
System.out.print(node.value);
}else
System.out.print(","+node.value);
PreOrder(node.left);
PreOrder(node.right);
}
}
public void InOrder(TreeNode node,int count ){
if(node!=null){
InOrder(node.left,count);
if(count==0&&node.left==null){
System.out.print(node.value);
count++;
}else{
System.out.print(","+node.value);
count++;
}
InOrder(node.right,count);
}
}
public void PostOrder(TreeNode node){
if(node!=null){
PostOrder(node.left);
PostOrder(node.right);
if(node==rootNode)
System.out.print(node.value);
else
System.out.print(node.value+",");
}
}
}