I'm supposed to create a binary search tree that takes in integer array, I want to know how can I access the left and right nodes in Node class to create the left and right nodes, please assist This is the Node class
private int element; private BinaryNode left; private BinaryNode right; public Node( ){ this( 0, null, null );} public( int theElement, BinaryNode lt, BinaryNode rt ){ element = theElement; left = lt; right = rt;} public int getElement( ){ return element;} public BinaryNode getLeft( ){ return left;} public BinaryNode getRight( ){ return right;} public void setElement( int x ){ element = x;} public void setLeft( BinaryNode t ){ left = t;} public void setRight( BinaryNode t ){ right = t;}
static Node obj = new Node(); static Node root = new Node(); static Node BinaryTree(int[] array, int begin, int end){ if(begin > end){ //checks in the array is still in bound return null; //returns null so the next node can be null } int middle = (begin + end)/2; //finds the middle index obj.setElement(array[middle]); root = new Node(obj.getElement(), obj.getLeft(), obj.getRight()); //creates the root node of the tree/subtree root.setLeft(BinaryTree(array, begin, middle-1)); //creates the left tree or node recursively root.setRight(BinaryTree(array, middle+1, end)); //creates the right tree or node recursively return root; //places/returns the node of the tree in its place }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)