How to resolve the algorithm AVL tree step by step in the Haskell programming language
How to resolve the algorithm AVL tree step by step in the Haskell programming language
Table of Contents
Problem Statement
In computer science, an AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; at no time do they differ by more than one because rebalancing is done ensure this is the case. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations. Note the tree of nodes comprise a set, so duplicate node keys are not allowed. AVL trees are often compared with red-black trees because they support the same set of operations and because red-black trees also take O(log n) time for the basic operations. Because AVL trees are more rigidly balanced, they are faster than red-black trees for lookup-intensive applications. Similar to red-black trees, AVL trees are height-balanced, but in general not weight-balanced nor μ-balanced; that is, sibling nodes can have hugely differing numbers of descendants.
Implement an AVL tree in the language of choice, and provide at least basic operations.
Red_black_tree_sort
Let's start with the solution:
Step by Step solution about How to resolve the algorithm AVL tree step by step in the Haskell programming language
The provided code defines a data type Tree a
to represent a binary search tree, and various functions to manipulate it.
Data Type:
data Tree a = Leaf | Node Int (Tree a) a (Tree a)
:Leaf
represents an empty tree.Node
represents a tree node with an integerInt
for height, a left subtree(Tree a)
, a valuea
, and a right subtree(Tree a)
.
Functions:
-
foldTree :: Ord a => [a] -> Tree a
:- Folds a list
[a]
into a binary search treeTree a
using theinsert
function.
- Folds a list
-
height :: Tree a -> Int
:- Calculates the height of a tree.
Leaf
has a height of-1
, andNode
has a height stored in itsInt
field.
- Calculates the height of a tree.
-
depth :: Tree a -> Tree a -> Int
:- Calculates the depth of the deeper subtree between two trees
a
andb
.
- Calculates the depth of the deeper subtree between two trees
-
insert :: Ord a => a -> Tree a -> Tree a
:- Inserts a value
v
into a treet
. Ifv
is less than the current node's value, it goes to the left subtree; if greater, to the right subtree. The tree is then balanced using therotate
function.
- Inserts a value
-
max_ :: Ord a => Tree a -> Maybe a
:- Finds the maximum value in a tree. If the tree is empty (
Leaf
), it returnsNothing
; otherwise, it recursively searches the right subtree until it finds a maximum value.
- Finds the maximum value in a tree. If the tree is empty (
-
delete :: Ord a => a -> Tree a -> Tree a
:- Deletes a value
x
from a treet
. If the value is found, the tree is modified accordingly and rebalanced usingrotate
.
- Deletes a value
-
rotate :: Tree a -> Tree a
:- Rotates a tree to maintain balance. It checks for four different cases (Left Left, Right Right, Left Right, Right Left) and applies the appropriate rotation to maintain the height difference between subtrees within
1
.
- Rotates a tree to maintain balance. It checks for four different cases (Left Left, Right Right, Left Right, Right Left) and applies the appropriate rotation to maintain the height difference between subtrees within
-
draw :: Show a => Tree a -> String
:- Draw the tree in an ASCII format for visualization. It recursively traverses the tree and constructs a string representation with appropriate padding for each level.
-
main :: IO ()
:- Calls
draw
on a tree created byfoldTree
and prints the ASCII representation of the tree.
- Calls
Example:
Suppose we want to create a binary search tree with values [1, 7, 4, 23, 8, 9, 42, 5]
.
Using the foldTree
function:
tree = foldTree [1, 7, 4, 23, 8, 9, 42, 5]
The resulting tree will look like:
42
/ \
23 8
/ \ / \
7 9 4 5
\
1
The ASCII representation generated by draw
will be:
(42,3)
/ \
(23,2) (8,1)
/ \ / \
(7,1) (9,0) (4,0) (5,0)
\
(1,0)
Source code in the haskell programming language
data Tree a
= Leaf
| Node
Int
(Tree a)
a
(Tree a)
deriving (Show, Eq)
foldTree :: Ord a => [a] -> Tree a
foldTree = foldr insert Leaf
height :: Tree a -> Int
height Leaf = -1
height (Node h _ _ _) = h
depth :: Tree a -> Tree a -> Int
depth a b = succ (max (height a) (height b))
insert :: Ord a => a -> Tree a -> Tree a
insert v Leaf = Node 1 Leaf v Leaf
insert v t@(Node n left v_ right)
| v_ < v = rotate $ Node n left v_ (insert v right)
| v_ > v = rotate $ Node n (insert v left) v_ right
| otherwise = t
max_ :: Ord a => Tree a -> Maybe a
max_ Leaf = Nothing
max_ (Node _ _ v right) =
case right of
Leaf -> Just v
_ -> max_ right
delete :: Ord a => a -> Tree a -> Tree a
delete _ Leaf = Leaf
delete x (Node h left v right)
| x == v =
maybe left (rotate . (Node h left <*> (`delete` right))) (max_ right)
| x > v = rotate $ Node h left v (delete x right)
| x < v = rotate $ Node h (delete x left) v right
rotate :: Tree a -> Tree a
rotate Leaf = Leaf
rotate (Node h (Node lh ll lv lr) v r)
-- Left Left.
| lh - height r > 1 && height ll - height lr > 0 =
Node lh ll lv (Node (depth r lr) lr v r)
rotate (Node h l v (Node rh rl rv rr))
-- Right Right.
| rh - height l > 1 && height rr - height rl > 0 =
Node rh (Node (depth l rl) l v rl) rv rr
rotate (Node h (Node lh ll lv (Node rh rl rv rr)) v r)
-- Left Right.
| lh - height r > 1 =
Node h (Node (rh + 1) (Node (lh - 1) ll lv rl) rv rr) v r
rotate (Node h l v (Node rh (Node lh ll lv lr) rv rr))
-- Right Left.
| rh - height l > 1 =
Node h l v (Node (lh + 1) ll lv (Node (rh - 1) lr rv rr))
rotate (Node h l v r) =
-- Re-weighting.
let (l_, r_) = (rotate l, rotate r)
in Node (depth l_ r_) l_ v r_
draw :: Show a => Tree a -> String
draw t = '\n' : draw_ t 0 <> "\n"
where
draw_ Leaf _ = []
draw_ (Node h l v r) d = draw_ r (d + 1) <> node <> draw_ l (d + 1)
where
node = padding d <> show (v, h) <> "\n"
padding n = replicate (n * 4) ' '
main :: IO ()
main = putStr $ draw $ foldTree [1 .. 31]
You may also check:How to resolve the algorithm Rep-string step by step in the Go programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Picat programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the VBA programming language
You may also check:How to resolve the algorithm Empty string step by step in the jq programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the NewtonScript programming language