Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Created March 11, 2020 18:26
Show Gist options
  • Save raunaqsingh2020/ad1680f20e5b49053a5297d4349acac8 to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/ad1680f20e5b49053a5297d4349acac8 to your computer and use it in GitHub Desktop.
public class BinaryTree
{
// Root of Binary Tree
BinaryNode root;
// Constructors
public BinaryTree(int key)
{
root = new BinaryNode(key);
}
public BinaryTree()
{
root = null;
}
public void insert(BinaryNode node, int value) {
/*
if (node == null) {
return (new BinaryNode(data));
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
*/
if (value < node.data) {
if (node.left != null) {
insert(node.left, value);
} else {
//System.out.println(" Inserted " + value + " to left of Node " + node.data);
node.left = new BinaryNode(value);
}
} else if (value > node.data) {
if (node.right != null) {
insert(node.right, value);
} else {
//System.out.println(" Inserted " + value + " to right of Node " + node.data);
node.right = new BinaryNode(value);
}
}
}
public void showValues (BinaryNode node) {
System.out.println(node.data);
if (node.left != null) {
System.out.println("Left of " + node.data);
showValues(node.left);
}
if (node.right != null) {
System.out.println("Right of " + node.data);
showValues(node.right);
}
}
public boolean showValues (BinaryNode root, int value) {
System.out.println(root.data);
if(root.data == value){
return true;
}
if (root.left != null) {
if(showValues(root.left, value))
return true;
}
if (root.right != null) {
if(showValues(root.right, value))
return true;
}
return false;
}
public int findHighest(BinaryNode root){
int val = -1;
if(root.right == null){
return root.data;
}else{
if(findHighest(root.right)>val){
val = findHighest(root.right);
}
}
return val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment