i want to find the height of a tree in sml.The tree is not a regular one it could have any number of nodes at a level.Futher the datatypes that it holds is also abstract.could someone help me out with it.
As other have said, then please post what ever definition of a tree you have, and what code you have come up with so far.
I assume that you have already defined your own tree structure or you have had it defined in the assignment, anyways this ought not to be your problem so here is simple binary tree structure using a Node and an empty Leaf as constructors.
datatype 'a tree = Leaf
| Node of ('a tree) * 'a * ('a tree)
val t1 = Node (Leaf, 1, Leaf)
val t2 = Node (t1, 2, Leaf)
val t3 = Node (Leaf, 3, Leaf)
val t = Node (t2, 4, t3)
Ascii representation of t
4
/ \
/ \
2 3
/ \ / \
1 * * *
/ \
* *
Where * represents the leafs
Given this representation of a binary tree, you could create a function that calculates the height in a bottom up maner. You mainly have to consider two cases:
What are the height of a tree only containing a leaf?
What are the height of a Node that has a left sub-tree of height l and a right sub-tree of height r?
If we look at t2 from the above example
2
/ \
1 *
/ \
* *
Then obviously the right sub-tree has height x (depends on how you define the height of leafs) and the left sub-tree has height 0. The Height of t2 must then be 1 (0+1).
Considering t then it has a left sub-tree of height 1 (as we have just found out) and the right sub-tree has height 0. Thus t must have height 2 (1+1)
I have seen many quick implementation of a height function that counts the root node, but this is not correct.
Here
height is defined as the number of links from the root to the deepest leaf
Related
We know that an AVL tree is usually very close to being balanced. Let's say we take an AVL tree and put it into an array (very similar to a heap, where the parent is index i, left child is 2i, and right child is 2i+1), how many empty indices would you get in terms of big O complexity?
So I know that the minimum number of nodes in a tree of height h = Fibonacci(h+2) - 1. So number of empty indices = 2^h - 1 - (Fibonacci(h+2) - 1) = 2^h - Fibonacci(h+2). But I don't know what to do next to prove it's complexity. I think it's O(log(n)), but I'm not sure.
If h is 0-based (as is the usual convention), the minimum number of nodes in an AVL tree with height h is F(h+3) - 1.
Let n = F(h+3) - 1 and let's try to solve for h to find the maximum height of an AVL tree with n nodes.
The closed form for F(x) is given by Binet's formula (see here for details):
F(x) = (phi^n - psi^n)/sqrt(5)
Therefore,
n = (phi^(h+3) - psi^(h+3))/sqrt(5) - 1
>= (phi^(h+3) - 1)/sqrt(5) - 1 since |psi| < 1
Solving for h yields
h <= log_phi(sqrt(5)(n + 1) + 1) - 3
<= 1.4405 log2(n)
A full tree with height h has 2^(h+1) - 1 nodes. Or in terms of n:
2^(h+1) - 1
<= 2^(1.4405 log2(n) + 1) - 1
= 2 * (2^log(n))^1.4405 - 1
= 2n^1.4405 - 1
Hence the number of empty nodes is bounded by
2n^1.4405 - 1 - n = O(n^1.4405)
I'm trying to write my own A* pathfinding algorithm and have a problem; when seeking the shortest cost path the following error arises:
Where the green filled node is the start node, the red filled node is the goal node and the pink filled nodes are the actual path that the algorithm has taken.
This is of course not the shortest path and I'm certain the problem lies with my heuristic function (manhattan distance):
int Map::manhattanDistance(Node evaluatedNode, Node goalNode)
{
int x = evaluatedNode.xCoord - goalNode.xCoord;
int y = evaluatedNode.yCoord - goalNode.yCoord;
if(x + y < 0)
return (x + y) * -1;
else
return x + y;
}
It seems to me that manhattan distance formula is not ideal here and my question is therefore: what other distance measuring algorithm would work for me here?
EDIT: for sake of clarity, each nodes width is determined by number of nodes in row / screen width = 640 and each nodes height is determined by number of nodes in column / screen height = 480.
A negative cost would imply that a route is free, or more than that it discounts previous moves. It looks like you have tried to account for this by multiplying by -1 of the aggregate, however the damage has already been done. That is why the solution has used diagonals, (1 - 1) * -1 is still zero and thus is considered to be free.
I would replace the if statement with the following, which uses the magnitude of each component by applying abs against it separately:
return Math.abs(x) + Math.abs(y);
Say I have an AVL tree such that it's nodes store their own balance factor as a single integer.
How can I calculate the balance factor of a node N if I know the balance factor of both it's left and right child.
Note that I DO NOT have the rHeight and lHeight, so bal(N) = lHeight - rHeight is not an option.
Short answer - you can't.
Long answer:
Consider these two trees:
A
/ \
B C A
/ \ / \ / \
D E F G B C
/ \ / \ / \ / \
H I J K L M N O
They have the same balance factor, but they aren't the same height.
So, if you only have the balance factor of the child, you don't know how high that subtree is, thus you can't use only that to calculate the balance factor of the parent.
I need to find the size of a perfect quad tree.
This means I have 1 root node that splits into 4 nodes that splits into 4 nodes etc.
so a quad tree of height 1 would be size 1
height 2 = size 5 (1 + 4)
height 3 = size 21 (1 + 4 + 16)
height 4 = size 85 (1 + 4 + 16 + 64)
etc..
I know that the size of a perfect binary tree can be found with: size = 2^(height+1)-1
So I believe that a similar equation exists for quad tree.
So what is it?
This is a geometric series. So the relevant formula is:
S = a * (1 - r^n) / (1 - r)
where a is the first value, r is the common ratio, n is the number of terms, and ^ denotes "to-the-power-of".
For a quad tree the algorithm is
((4^depth)-1)/3
For example with depth 3 you get
(64-1)/3 = 21
and if you count three layers you get
1 + 4 + 16 = 21
In my implementation I have even divided it into two arrays
where the size for all nodes that arent leave nodes is
((4^(depth-1))-1)/3
and leave nodes is
4^(depth-1)
I do these calculations at compile time with meta programming for pow, and a template argument for the depth. So i just allocate my nodes in two arrays.
Just in case anyone will need a code sample (in swift3)
public func tileCount(forLevelRange levelRange: Range<UInt>) -> UInt64 {
var tileCount: UInt64 = 0
for level in levelRange.lowerBound ..< levelRange.upperBound {
tileCount += UInt64(pow(Double(1 << level), 2) )
}
return tileCount
}
To calculate the balance factor of a node in an AVL tree we need to find the height of its left subtree and the height of its right subtree. Then we subtract the height of right subtree from the height of its left subtree:
balancefactor = leftsubtreeheigh - rightsubtreeheight
My question is: How do I calculate the height of the left subtree or the right subtree?
For example in the given figure1 the height of the left subtree of the root node 40 is 4 and the height of the right subtree of 40 is 2 so the difference of the heights is 2.
How do I do this in C++? I don't want to use recursion because it's very confusing. Using an explicit stack instead of recursion is much more understandable.
1 The figure has been deleted from the imgur servers.
The depth of a node is 1 more then the depth of it's deepest child.
You can do this quite simply with recursion.
unsigned int depth(node *n)
{
if (n == NULL)
return 0;
else
return MAX(depth(n->left), depth(n->right)) + 1;
}
-1, 0, and +1 are the three balance state of an AVL tree.
The balance factor is left height - right height of the tree.