Chciałbym obliczyć sumowanie głębi każdym węźle binarne drzewo poszukiwań.
Poszczególne głębokości elementów nie są już zapisane.
Chciałbym obliczyć sumowanie głębi każdym węźle binarne drzewo poszukiwań.
Poszczególne głębokości elementów nie są już zapisane.
Dla danego drzewa, liczba węzłów jest 1 do korzenia powiększonej o liczbę węzłów w lewym poddrzewie powiększonej o liczbę węzłów w prawym poddrzewie :)
Szczegóły, jak upewniając się, że rzeczywiście jest w lewo lub w prawo poddrzewo, są „pozostawiamy czytelnikowi”.
Coś takiego:
int countChildren(Node node)
{
if ( node == null )
return 0;
return 1 + countChildren(node.getLeft()) + countChildren(node.getRight());
}
I aby uzyskać sumę głębi każdego dziecka:
int sumDepthOfAllChildren(Node node, int depth)
{
if ( node == null )
return 0; // starting to see a pattern?
return depth + sumDepthOfAllChildren(node.getLeft(), depth + 1) +
sumDepthOfAllChildren(node.getRight(), depth + 1);
}
Teraz mam nadzieję, że dla wyjaśnienia informacyjny w przypadku jest to praca domowa. Zliczanie liczby węzłów jest dość prosta. Przede wszystkim, jeśli węzeł nie jest węzłem ( node == null) zwraca wartość 0. Jeśli jest to węzeł, najpierw liczy się jego własny (The 1), a także liczbę węzłów w jej lewym sub-tree plus liczbę węzłów w jego prawo sub-tree. Innym sposobem, aby myśleć o tym jest odwiedzić każdy węzeł poprzez BFS i dodasz do zliczania dla każdego węzła odwiedzić.
Podsumowanie głębokościach jest podobna, z wyjątkiem zamiast dodawania tylko jeden dla każdego węzła, węzeł dodaje głębi swojej jaźni. I zna głębię swojej jaźni, ponieważ jego rodzic powiedział to. Każdy węzeł wie, że głębokość swojego wieku są to własny głębokość plus jeden, więc kiedy się głębokość lewej i prawej dzieci węzła, powiedzieć im ich głębokość jest głębokość bieżący węzeł jest Plus 1.
I znowu, jeśli węzeł nie jest węzłem, nie ma głębi. Więc jeśli chcesz sumę głębokości wszystkich dzieci węzła głównego, w zdać w węźle głównym i głębokości węzła głównego jest tak:sumDepthOfAllChildren(root, 0)
Rekurencja jest bardzo przydatna, to po prostu zupełnie inny sposób myślenia o rzeczach i wymaga praktyki, aby przyzwyczaić się do niego
public int numberOfNodes()
{
// This node.
int result = 1;
// Plus all the nodes from the left node.
Node left = getLeft();
if (left != null)
result += left.numberOfNodes();
// Plus all the nodes from the right node.
Node right = getRight();
if (right != null)
result += right.numberOfNodes();
return result;
}
public int countNodes(Node root)
{
// Setup
// assign to temps to avoid double call accessors.
Node left = root.getLeft();
Node right = root.getRight();
int count = 1; // count THIS node.
// count subtrees
if (left != null) count += countNodes(left);
if (right != null) count += countNodes(right);
return count;
}
public class Node {
private Node left;
private Node right;
public int size() { return 1+ (left==null?0:left.size())+ (right==null?0:right.size());}
}
private static int getNumberOfNodes(Node node) {
if (node == null) {
return 0;
}
return 1 + getNumberOfNodes(node.left) + getNumberOfNodes(node.right);
}
int depth(treenode *p)
{
if(p==NULL)return(0);
if(p->left){h1=depth(p->left);}
if(p=>right){h2=depth(p->right);}
return(max(h1,h2)+1);
}
int maxDepth(Node node) {
if (node == null) {
return (-1); // an empty tree has height −1
} else {
// compute the depth of each subtree
int leftDepth = maxDepth(node.left);
int rightDepth = maxDepth(node.right);
// use the larger one
if (leftDepth > rightDepth )
return (leftDepth + 1);
else
return (rightDepth + 1);
}
}
public int getDepthHelper( TreeNode< T > node ) {
int treeHeightLeft;
int treeHeightRight;
//get height of left subtree
if( node.leftNode == null )
treeHeightLeft = 1;
else {
treeHeightLeft = getDepthHelper( node.leftNode) + 1;
}
//get height of right subtree
if( node.rightNode == null )
treeHeightRight = 1;
else {
treeHeightRight = getDepthHelper( node.rightNode) + 1;
}
return Math.max(treeHeightLeft, treeHeightRight);
}
To rozwiązanie jest jeszcze prostsze.
public int getHeight(Node root)
{
if(root!=null)
return 1+ Math.max(getHeight(root.leftchild),getHeight(root.rightchild));
else
return 0;
}