FWNX- build a binary search tree using array - java version <<recursive>>

来源:互联网 发布:淘宝超级店长在哪里 编辑:程序博客网 时间:2024/04/30 10:35
package test_kepler;import test_kepler.TreefromA.Node;public class BinarySTA {public class Node {int value;public Node leftChild;public Node rightChild;public boolean isVisted;public Node(int i){value = i;isVisted = false;}public void print(){System.out.print(this.value +" ~~ ");}}public Node AddTree(int a[],int left,int right){if( right < left){return null;}int mid = (left+right)/2;Node newNode = new Node(a[mid]);newNode.leftChild = AddTree(a,left,mid-1);newNode.rightChild = AddTree(a,mid+1,right);return newNode;}public void exe(int a[]){Node root = AddTree(a,0,a.length-1);BFS(root);}void BFS(Node root){ qbytwostc testq = new qbytwostc<Node>(); testq.enQueue(root); while(!testq.isEmpty()) { Node head = (Node) testq.dequeue(); if(head.isVisted == false) head.print();    if(head.rightChild!=null && head.rightChild.isVisted == false) { testq.enQueue(head.rightChild); }  if(head.leftChild!=null && head.leftChild.isVisted == false) { testq.enQueue(head.leftChild); } }}public static void main(String[] args) {BinarySTA bsta = new BinarySTA();int a[] = {1,2,3,43,56,67,100};bsta.exe(a);}}

0 0
原创粉丝点击