Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Created March 23, 2020 14:36
Show Gist options
  • Save raunaqsingh2020/41e5f04d7dfe531b6f020ea2a4ff0424 to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/41e5f04d7dfe531b6f020ea2a4ff0424 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 (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 makeRoot (BinaryNode root, BinaryNode left, BinaryNode right) {
root.left = left;
root.right = right;
}
public void showValues (BinaryNode node) {
//System.out.println(node.data);
if (node.character != null) {
System.out.println("Associated Character: " + node.character + ", Value: " + node.data);
} else {
System.out.println("Value: " + 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