I have a tree structure that looks like the following:
Tree {
Node root;
}
Node {
List children;
}
And I'm trying to make a method that returns the length of the longest path. I've seen some solutions that work when it's a binary tree, but there is no limit to how many children nodes each node can have and that is where I'm running into problems.
I should do somethinkg like this
int getLongestPathLength(Node node) {
if(node == null) return 0;
int max = 0;
for(Node child : node.children){
max = Math.max(getLongestPathLength(child),max);
}
return 1+max;
}
It seems what you are looking for it called the height of the root. You can see the basic algorithm to compute it here (similar to the one given in the previous answer): http://cs.nyu.edu/courses/fall02/V22.0310-002/lectures/lecture-08.html.
Related
Looking for some help while practicing Binary Search Trees. I am unable to figure out where I am going wrong in my code, as it seems to follow the generic recursion format. Possibly something related to either the temp Node or the return statement? Any help would be appreciated thanks.
public static Node < Integer > mirror(Node < Integer > root) {
if (root == null)
return null;
else {
mirror(root.left);
mirror(root.right);
Node temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
First, it seems you do not allocate memory for the nodes that will belong to the mirrored tree. Note that the instruction
Node temp;
does not allocate in the heap, but in the stack. When the function returns, this memory is freed and whatever you have stored there won't make sense.
Second, it seems you do not have very clear the traversal pattern and I think that because of the line
root.right = temp;
poses an kind of asymmetry.
This is a way (in C++ pseudo-code), among others, for getting a mirrored binary tree that uses a preorder pattern:
Node * mirror(Node * root)
{
if root == NULL
return NULL;
Node * mirror_root = new Node;
mirror_root->left = mirror(root->right); // here is where the mirroring happens
mirror_root->right = mirror(root->left);
return mirror_root;
}
You could try to do the same thing allocating the nodes in postorder or inorder.
I have been given an assignment to create a 2-3 search tree that is supposed to support a few different operations each divided in to different stages of the assignment.
For stage 1 I'm supposed to suport the operations get, put and size. I'm curently trying to implement the get operation but I'm stuck and I can't wrap my head around how to continue so I'm questioning all of my code I have written and felt like a need someone elses input.
I have looked around how to develop a 2-3 search tree but what I found was alot of code that made no sence to me or it just did not do what I needed it to do, and I wanted to try and make it for my self from scratch and here we are now.
My Node class
package kth.id2010.lab.lab04;
public class Node {
boolean isLeaf = false;
int numberOfKeys;
String[] keys = new String[2]; //each node can contain up to 2 keys
int[] values = new int[2]; //every key contains 2 values
Node[] subtrees = new Node[3]; //every node can contain pointers to 3 different nodes
Node(Node n) {
n.numberOfKeys = 0;
n.isLeaf = true;
}
}
My Tree creating class
package kth.id2010.lab.lab04;
public class Tree {
Node root; // root node of the tree
int n; // number of elements in the tree
private Tree(){
root = new Node(root);
n = 0;
}
//Return the values of the key if we find it
public int[] get(String key){
//if the root has no subtrees check if it contain the right key
if(this.root.subtrees.length == 0){
if(this.root.keys[0] == key){
return(this.root.keys[0].values);
}
else if(this.root.keys[1] == key){
return(this.root.keys[1].values);
}
}
//if noot in root, check its subtree nodes
//and here I can't get my head around how to traverse down the tree
else{
for(int i = 0; i < this.root.subtrees.length; i++){
for(int j = 0; j < this.root.subtrees[i].keys.length; j++){
if(this.root.subtrees[i].keys[j] == key){
return(this.root.subtrees[i].keys[j].values);
}
}
}
}
return null;
}
}
What I can tell for my self is that I need to find a way to bind values[] to each key but I can't figure out a way how. Might be the lack of sleep or that I'm stuck in this way of thinking.
bind values[] to each key
It might make more sense to use a HashMap to do that mapping for you, since that's what it's for. Beyond that, if you have two keys and each key has two values, you have 4 values, not 2 ;)
In general, the get method in a tree structure is almost always implementable recursively. Here is a very general implementation of a get algorithm for a 2-3 tree in psudo-code.
V get<K, V>(Node<K, V> node, K key)
{
if(node.is_leaf())
{
return node.keys.get(key); // This will either return the value, or null if the key isn't in the leaf and thus not in the tree
}
if(key < node.left_key)
{
return get(node.left_child, key); // If our key goes to the left, go left recursively
}
else if(node.two_node && key <= node.right_key)
{
return get(node.center_child, key) // If we have two keys, and we're less than the second one, we go down the center recursively
}
else
{
return get(node.right_child, key); // If we've gotten here, we know we're going right, go down that path recursively
}
}
That should get you started in the right direction. Insertion/deletion for 2-3 trees is a bit more complicated, but this should at least get your head around how to think about it. Hint; Your Node class needs to be doubly-linked, that is each node/leaf needs to reference its parent node as well as its children, and the root is simply a node whose parent is null.
I am trying to change my recursive insert method of the BST into non-recursive( maybe While loop)
The reason for this changing because I want to see if it is possible.
Here is the code of insertion:
public void insert(String value)
{
//The node is stored in the root
root = insert(value,root);
}
private Character insert(String value,Character current)
{
if(current == null)
{
//Add the root if the tree empty
current = new Character(value);
}
else
//If the value that we want to insert < root value, then keep going to left till
//it's empty then inserted at left end. Done by recursion
if(value.compareTo(current.getElement())<=-1)
{
current.setLeft(insert(value,current.getLeft()));
}
else
//If the value that we want to insert > root value, then keep going to right till
//it's empty then inserted at right end. Done by recursion
if(value.compareTo(current.getElement())>=1)
{
current.setRight(insert(value,current.getRight()));
}
else
{
//Else, the number we want to insert in already in the tree
System.out.println("Duplicate numbers inserted" + current.getElement());
}
//returning the node tree so that we store it in the root
return current;
}
Could I change this code into non recursive ?
Cheers
Yes, but you need to alter the data structure a little bit to make it works.
The node has to know its left child and right child.
The algorithm looks like this:
current = root;
while(current != null){
if(value.compareTo(current.getElement())<=-1)
{
current = current.left_child;
}else if(value.compareTo(current.getElement())>=1){
current = current.right_child;
}else{
// Duplication
}
}
Actually there are some good examples before, you may want to check those out first:
Write a non-recursive traversal of a Binary Search Tree using constant space and O(n) run time
Nonrecursive/Iterative Binary Search Tree in C (Homework)
Yes, you could define your insert function non-recursively.
However, to do this, your insert function will have to define in-order traversal iterator for BST, which is recursively defined.
I believe there is a way to define in-order traversal non-recursively, but depending on implementation this can be very inefficient.
BST itself is basically recursively defined, and it is always efficient to define your insert function recursively. (I could write some pseudo-code if you really need it, but I think it is kind of meaningless and I do not know about the implementation detail of your in-order traversal iterator)
Please don't forget to select this as an answer :-)
Insert using while loop
public Node insert(Node root,int n) {
while (true) {
if (root.data>n) {
if (root.left==null) {
root.left= new Node(n);
return (root.left);
}
root=root.left;
}
else if (root.data<n) {
if (root.right == null) {
root.right= new Node(n);
}
}
}
}
I am currently trying to implement a trie data structure for integer tuples. And have implemented as follows:
import java.util.ArrayList;
public class TrieNode {
int num;
ArrayList<TrieNode> links;
boolean endOfTuple;
public TrieNode(int num)
{
this.num = num;
links = new ArrayList<TrieNode>();
this.endOfTuple = false;
}
}
I then have a trie class as follows:
public class Trie {
TrieNode root;
public Trie() {
root = new TrieNode(-1);
}
public void insertTuple(int[] tuple)
{
int l = tuple.length;
TrieNode curNode = root;
for (int i = 0; i < l; i++)
{
TrieNode node = new TrieNode(tuple[i]);
if(!curNode.links.contains(node)){
curNode.links.add(node);
}
curNode = curNode.links.get(curNode.links.indexOf(node));
}
curNode.endOfTuple = true;
}
}
I can add values to this trie, but i need to be able to iterate over this and was wondering how i could do this? For example if i wanted to print the tree using an iterator...Any help will be great...
All you need for an interator is to implement the Iterator interface, which only requires that you supply boolean hasNext() and Integer next(). So the question to ask is: how do represent a position in your trie, such that it's possible to (a) fetch the value associated with that position, and (b) figure out the "next" position given a current one?
I'll refrain from posting an actual solution since I'm not sure whether this is homework. But consider: you can represent a "current position" in your trie just by choosing a particular trie node, and the path of trie nodes you used to reach it. Then you can compute the "next" element recursively: if the current element is a node that has children, then find the first child for which endOfTuple is true. If the current element doesn't have children, then go to its parent and advance to that parent's next child. If that parent doesn't have next children, then go it its parent's next child, etc.
I need to traverse a binary tree, skipping the children of any node for which a condition is met.
This implements a tree-guided clustering approach; the leaves of a subtree are considered a cluster when they collectively meet the condition.
It seems like the place to start would be pre-order traversal, but I'm not sure how to modify the algorithm to skip all the children of the current node.
Update
In addition to the two (correct) answers below, one can use the following Java libraries:
MyArch TreeIter - Generic (with adapter class) tree traversal, with child skipping and dynamic maximum traversal depth
Phylosoft Forester - Tree implementation with getAllExternalDescendants and a Newick-to-XML converter
If by skipping all the children you mean not just direct descendants but their entire subtrees, you can do the following:
public void traverse(Node n){
if (n==null)
return;
doSomethingWith(n);
if (!conditionIsMet(n)){
traverse(n.left);
traverse(n.right);
}
}
the first part is easy:
class Tree {
Tree(int data) {
this.data = data;
}
Tree left, right;
Object object;
int data;
}
public class Main {
static void myPreorder(Tree tree) {
if (tree == null) return;
if (tree.data > 2) {
System.out.println("skipping " + tree.data);
return;
}
System.out.println("visiting " + tree.data);
myPreorder(tree.left);
myPreorder(tree.right);
}
public static void main(String[] args) {
Tree root = new Tree(1);
root.left = new Tree(2);
root.right = new Tree(3);
root.right.left = new Tree(4);
root.right.right = new Tree(5);
myPreorder(root);
}
}
the second part: " ... the leaves of a subtree are considered a cluster when they collectively meet the condition. ..." is hard. you would need to examine all of the leaves of a node to see that they met the skip condition.