DEV Community

Cover image for AVL Tree Time Complexity Analysis
Paul Ngugi
Paul Ngugi

Posted on

AVL Tree Time Complexity Analysis

Since the height of an AVL tree is O(log n), the time complexity of the search, insert, and delete methods in AVLTree is O(log n). The time complexity of the search, insert, and delete methods in AVLTree depends on the height of the tree. We can prove that the height of the tree is O(log n).

Let G(h) denote the minimum number of the nodes in an AVL tree with height h. Obviously, G(1) is 1 and G(2) is 2. The minimum number of nodes in an AVL tree with height h >= 3 must have two minimum subtrees: one with height h - 1 and the other with height h - 2. Thus,

G(h) = G(h - 1) + G(h - 2) + 1

Recall that a Fibonacci number at index i can be described using the recurrence relation F(i) = F(i - 1) + F(i - 2). Therefore, the function G(h) is essentially the same as F(i). It can be proven that

h < 1.4405 log(n + 2) - 1.3277

where n is the number of nodes in the tree. Hence, the height of an AVL tree is O(log n).

The search, insert, and delete methods involve only the nodes along a path in the tree. The updateHeight and balanceFactor methods are executed in a constant time for each node in the path. The balancePath method is executed in a constant time for a node in the path. Thus, the time complexity for the search, insert, and delete methods is O(log n).

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay