Skip to content

Instantly share code, notes, and snippets.

@anujku
Created December 14, 2014 21:03
Show Gist options
  • Save anujku/2512b1614deb319f92a2 to your computer and use it in GitHub Desktop.
Save anujku/2512b1614deb319f92a2 to your computer and use it in GitHub Desktop.
Trinary Tree in Java
/**
* The Class TrinaryTree.
*/
public class TrinaryTree {
/** The root. */
TrinaryNode root;
/**
* The Class TrinaryNode.
*/
class TrinaryNode {
/** The val. */
int val;
/** The left. */
TrinaryNode left;
/** The right. */
TrinaryNode right;
/** The middle. */
TrinaryNode middle;
/**
* Instantiates a new trinary node.
*
* @param val the val
*/
public TrinaryNode(int val) {
this.val = val;
}
}
/**
* This method inserts a node in the Trinary Tree..
*
* @param vals the vals
*/
public void insert(int[] vals) {
if (vals != null) {
if (vals.length != 0) {
TrinaryNode node = new TrinaryNode(vals[0]);
for (int i = 1; i < vals.length; i++)
insert(node, vals[i]);
this.root = node;
}
}
}
/**
* This method Inserts a node in the Trinary Tree.
*
* @param node the node
* @param val the val
*/
public void insert(TrinaryNode node, int val) {
if (val < node.val) {
if (node.left != null) {
insert(node.left, val);
} else {
node.left = new TrinaryNode(val);
}
} else if (val > node.val) {
if (node.right != null) {
insert(node.right, val);
} else {
node.right = new TrinaryNode(val);
}
} else {
if (node.middle != null) {
insert(node.middle, val);
} else {
node.middle = new TrinaryNode(val);
}
}
}
/**
* This method deletes a node in the Trinary Tree.
*
* @param node the node
* @param val the val
* @return the trinary node
*/
public TrinaryNode delete(TrinaryNode node, int val) {
if (val < node.val) {
node.left = delete(node.left, val);
} else if (val > node.val) {
node.right = delete(node.right, val);
} else {
if (node.middle != null) {
node.middle = delete(node.middle, val);
} else if (node.right != null) {
int min = findMin(node.right).val;
node.val = min;
node.right = delete(node.right, min);
} else {
node = node.left;
}
}
return node;
}
/**
* This method finds the minimum node.
*
* @param node the node
* @return the trinary node
*/
private TrinaryNode findMin(TrinaryNode node) {
if (node != null) {
while (node.left != null)
return findMin(node.left);
}
return node;
}
/**
* Prints the Trinary Tree..
*
* @param root the root
*/
public static void print(TrinaryNode root) {
if (root != null) {
System.out.println("Node : " + root.val);
print(root.left);
print(root.middle);
print(root.right);
}
}
public static int countNodes(TrinaryNode node) {
int count = 1;
if (node.left != null) count += countNodes(node.left);
if (node.middle != null) count += countNodes(node.middle);
if (node.right != null) count += countNodes(node.right);
return count;
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
int[] vals = {5, 4, 9, 5, 7, 2, 2};
TrinaryTree trinaryTree = new TrinaryTree();
trinaryTree.insert(vals);
System.out.println("Tree after inserting nodes : ");
print(trinaryTree.root);
System.out.println("Nodes : " + countNodes(trinaryTree.root));
trinaryTree.delete(trinaryTree.root, 7);
System.out.println("Tree after deleting a node : ");
print(trinaryTree.root);
System.out.println("Nodes : " + countNodes(trinaryTree.root));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment