由一数组建平衡二叉树 并查找 Java实现

package fenshujs;

import java.util.Arrays;

import java.util.Scanner;

public class bishi {

private static class Node

{

public int s;

public Node left = null;

public Node right = null;

}

public static Node CreateTree(int[] a,int left,int right)

{

if(left>right)

       
return null;

        Node node = new Node();

        node.s = a[(left+right)/2];

node.left = CreateTree(a,left,(left+right)/2-1);

node.right = CreateTree(a,(left+right)/2+1,right );

return node;

}

public static void ShowTree(Node node)

{

if(node == null)

return;

System.out.print(“[“);

ShowTree(node.left);

System.out.print(node.s);

ShowTree(node.right);

System.out.print(“]”);

}

public static Node FindNode(Node node,int key)

{

while(node!=null)

{

if(node.s == key)

{

System.out.println(“find success!”);

return node;

}

else if(node.s>key)

{

node = node.left;

}

else if(node.s<key)

{

node = node.right;

}

}

return null;

}

public static void main(String[] args){

   

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int[] a = new int[n];

for(int i = 0;i<n;i++)

a[i] = sc.nextInt();

Arrays.sort(a);

Node node = CreateTree(a,0,a.length-1);

ShowTree(node);

System.out.println(“”);

System.out.println(“please input key”);

FindNode(node,sc.nextInt());

}

}

5
1 2 3 4 5
[[1[2]]3[4[5]]]
please input key

4
find success!

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/K3140911026/article/details/77857434
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞