Skip to main content

Given an AVL tree, I want to compute its height as efficiently as possible. $\newcommand{\bf}{\text{bf}}\newcommand{\height}{\text{height}}$

Each node of an AVL tree stores its balance factor ($\bf$), defined as

$$\bf(v)=\height(v.\text{left}) - \height(v.\text{right}).$$

We can recursively compute the height of an AVL tree in $O(\log n)$ time, using the following recursive procedure:

Height($v$):

  1. If $v$ is a leaf node, return 0.
  2. If $\bf(v)=+1$, return Height($v.\text{left}$).
  3. If $\bf(v)=0$$\bf(v)=-1$, return Height($v.\text{right}$).
  4. If $\bf(v)=-1$$\bf(v)=0$, return Height($v.\text{left}$). (returning Height($v.\text{right}$) would work too)

This recursive algorithm works, because the balance factor tells us which is taller, the left subtree or the right subtree, and the algorithm always recurses into the taller subtree.

My question is: Is there a faster algorithm? Can we do better than $O(\log n)$ time? Assume we can't augment the tree to store extra information in it; we are restricted to only the information that is already normally stored in an AVL tree.

Given an AVL tree, I want to compute its height as efficiently as possible. $\newcommand{\bf}{\text{bf}}\newcommand{\height}{\text{height}}$

Each node of an AVL tree stores its balance factor ($\bf$), defined as

$$\bf(v)=\height(v.\text{left}) - \height(v.\text{right}).$$

We can recursively compute the height of an AVL tree in $O(\log n)$ time, using the following recursive procedure:

Height($v$):

  1. If $v$ is a leaf node, return 0.
  2. If $\bf(v)=+1$, return Height($v.\text{left}$).
  3. If $\bf(v)=0$, return Height($v.\text{right}$).
  4. If $\bf(v)=-1$, return Height($v.\text{left}$). (returning Height($v.\text{right}$) would work too)

This recursive algorithm works, because the balance factor tells us which is taller, the left subtree or the right subtree, and the algorithm always recurses into the taller subtree.

My question is: Is there a faster algorithm? Can we do better than $O(\log n)$ time? Assume we can't augment the tree to store extra information in it; we are restricted to only the information that is already normally stored in an AVL tree.

Given an AVL tree, I want to compute its height as efficiently as possible. $\newcommand{\bf}{\text{bf}}\newcommand{\height}{\text{height}}$

Each node of an AVL tree stores its balance factor ($\bf$), defined as

$$\bf(v)=\height(v.\text{left}) - \height(v.\text{right}).$$

We can recursively compute the height of an AVL tree in $O(\log n)$ time, using the following recursive procedure:

Height($v$):

  1. If $v$ is a leaf node, return 0.
  2. If $\bf(v)=+1$, return Height($v.\text{left}$).
  3. If $\bf(v)=-1$, return Height($v.\text{right}$).
  4. If $\bf(v)=0$, return Height($v.\text{left}$). (returning Height($v.\text{right}$) would work too)

This recursive algorithm works, because the balance factor tells us which is taller, the left subtree or the right subtree, and the algorithm always recurses into the taller subtree.

My question is: Is there a faster algorithm? Can we do better than $O(\log n)$ time? Assume we can't augment the tree to store extra information in it; we are restricted to only the information that is already normally stored in an AVL tree.

Tweeted twitter.com/StackCompSci/status/750837956710137856
added 680 characters in body; edited title
Source Link
D.W.
  • 168.8k
  • 23
  • 236
  • 519

Effective finding Compute height of avlAVL tree as efficiently as possible

Given an AVL tree, I am searching effective waywant to find outcompute its height as efficiently as possible. $\newcommand{\bf}{\text{bf}}\newcommand{\height}{\text{height}}$

Each node of an AVL tree. In each node there is stores its balance factor ($bf$$\bf$). $$bf(root)=height(root.left) - height(root.right). $$
And now we, defined as

$$\bf(v)=\height(v.\text{left}) - \height(v.\text{right}).$$

We can findrecursively compute the height of an AVL tree in $O(\log n)$ time in a, using the following recursive wayprocedure:

when bf = 0 choose left or right subtree. when bf = 1 choose left subtree. when bf = -1 choose right subtree. 

Height($v$):

  1. If $v$ is a leaf node, return 0.
  2. If $\bf(v)=+1$, return Height($v.\text{left}$).
  3. If $\bf(v)=0$, return Height($v.\text{right}$).
  4. If $\bf(v)=-1$, return Height($v.\text{left}$). (returning Height($v.\text{right}$) would work too)

TheThis recursive algorithm works, because the balance factor tells us which is taller, the left subtree or the right subtree, and the algorithm always recurses into the taller subtree.

My question is: IfIs there exists more effective waya faster algorithm? Can we do better than $O(\log n)$ time? Assume we can't augment the tree to store extra information in it; we are restricted to only the information that is already normally stored in an AVL tree.

Effective finding height of avl tree

I am searching effective way to find out height of AVL tree. In each node there is balance factor ($bf$). $$bf(root)=height(root.left) - height(root.right). $$
And now we can find height in $O(\log n)$ time in a following recursive way:

when bf = 0 choose left or right subtree. when bf = 1 choose left subtree. when bf = -1 choose right subtree. 

The question is: If there exists more effective way ?

Compute height of AVL tree as efficiently as possible

Given an AVL tree, I want to compute its height as efficiently as possible. $\newcommand{\bf}{\text{bf}}\newcommand{\height}{\text{height}}$

Each node of an AVL tree stores its balance factor ($\bf$), defined as

$$\bf(v)=\height(v.\text{left}) - \height(v.\text{right}).$$

We can recursively compute the height of an AVL tree in $O(\log n)$ time, using the following recursive procedure:

Height($v$):

  1. If $v$ is a leaf node, return 0.
  2. If $\bf(v)=+1$, return Height($v.\text{left}$).
  3. If $\bf(v)=0$, return Height($v.\text{right}$).
  4. If $\bf(v)=-1$, return Height($v.\text{left}$). (returning Height($v.\text{right}$) would work too)

This recursive algorithm works, because the balance factor tells us which is taller, the left subtree or the right subtree, and the algorithm always recurses into the taller subtree.

My question is: Is there a faster algorithm? Can we do better than $O(\log n)$ time? Assume we can't augment the tree to store extra information in it; we are restricted to only the information that is already normally stored in an AVL tree.

Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags
Link
Raphael
  • 73.4k
  • 31
  • 184
  • 406
edited tags
Link
Raphael
  • 73.4k
  • 31
  • 184
  • 406
Loading
Source Link
user40545
  • 583
  • 6
  • 19
Loading