Skip to content

Commit 6b089f1

Browse files
committed
Add docstrings and README (#3)
* Add docstrings * Add README
1 parent 054d38c commit 6b089f1

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,45 @@
22

33
[![Build Status](https://github.com/eliascarv/TreeDataStructures.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/eliascarv/TreeDataStructures.jl/actions/workflows/CI.yml?query=branch%3Amain)
44
[![Coverage](https://codecov.io/gh/eliascarv/TreeDataStructures.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/eliascarv/TreeDataStructures.jl)
5+
6+
## Overview
7+
8+
This package provides a collection of tree data structures implemented in Julia.
9+
10+
Currently, it includes the following tree data structures:
11+
* **AVL Tree**: A self-balancing binary search tree.
12+
13+
## Installation
14+
15+
To install TreeDataStructures.jl, use the Julia's package manager:
16+
17+
```
18+
] add https://github.com/eliascarv/TreeDataStructures.jl
19+
```
20+
21+
## AVL Tree
22+
23+
An AVL tree is a binary search tree that keeps itself balanced to ensure efficient search, insertion, and deletion operations.
24+
25+
### Example Usage
26+
27+
```julia
28+
tree = AVLTree{Int,Float64}()
29+
30+
# add nodes to the tree
31+
tree[2] = 2.2 # root node
32+
tree[1] = 1.1 # left node
33+
tree[3] = 3.3 # right node
34+
35+
# update the value of the node
36+
tree[2] = 2.4
37+
38+
# get the value of the node using its key
39+
tree[2] # 2.4
40+
tree[1] # 1.1
41+
tree[3] # 3.3
42+
43+
# delete nodes from the tree
44+
delete!(tree, 1)
45+
delete!(tree, 3)
46+
```

src/avl.jl

+54
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,78 @@ end
4343
# AVL TREE
4444
# ---------
4545

46+
"""
47+
AVLTree{K,V}()
48+
49+
Construct an empty AVL Tree with keys of type `K`
50+
and values of type `V`.
51+
52+
AVLTree()
53+
54+
Construct an empty AVL Tree that stores keys and values
55+
of any type, alias for `AVLTree{Any,Any}()`.
56+
57+
The keys of AVL Tree must implement sorting operators (`>`, `<`)
58+
and comparison operators (`=`, `≠`)
59+
60+
# Examples
61+
62+
```julia
63+
tree = AVLTree{Int,Float64}()
64+
65+
# add nodes to the tree
66+
tree[2] = 2.2 # root node
67+
tree[1] = 1.1 # left node
68+
tree[3] = 3.3 # right node
69+
70+
# update the value of the node
71+
tree[2] = 2.4
72+
73+
# get the value of the node using its key
74+
tree[2] # 2.4
75+
tree[1] # 1.1
76+
tree[3] # 3.3
77+
78+
# delete nodes from the tree
79+
delete!(tree, 1)
80+
delete!(tree, 3)
81+
```
82+
"""
4683
mutable struct AVLTree{K,V}
4784
root::Union{AVLNode{K,V},Nothing}
4885
end
4986

5087
AVLTree{K,V}() where {K,V} = AVLTree{K,V}(nothing)
5188
AVLTree() = AVLTree{Any,Any}()
5289

90+
"""
91+
getindex(tree::AVLTree{K}, key::K) where {K}
92+
93+
Get the value stored in the node that has `key`.
94+
"""
5395
function Base.getindex(tree::AVLTree{K}, key::K) where {K}
5496
node = _search(tree, key)
5597
isnothing(node) && throw(KeyError(key))
5698
node.value
5799
end
58100

101+
"""
102+
setindex!(tree::AVLTree{K}, value, key::K) where {K}
103+
104+
Add a node to the tree with `key` and `value`.
105+
If a node with `key` already exists, the value
106+
of the node will be updated.
107+
"""
59108
function Base.setindex!(tree::AVLTree{K}, value, key::K) where {K}
60109
tree.root = _insert!(tree.root, key, value)
61110
tree
62111
end
63112

113+
"""
114+
delete!(tree::AVLTree{K}, key::K) where {K}
115+
116+
Delete the node that has `key` from the tree.
117+
"""
64118
function Base.delete!(tree::AVLTree{K}, key::K) where {K}
65119
tree.root = _delete!(tree.root, key)
66120
tree

0 commit comments

Comments
 (0)