Skip to content

Add docstrings and README #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,45 @@

[![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)
[![Coverage](https://codecov.io/gh/eliascarv/TreeDataStructures.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/eliascarv/TreeDataStructures.jl)

## Overview

This package provides a collection of tree data structures implemented in Julia.

Currently, it includes the following tree data structures:
* **AVL Tree**: A self-balancing binary search tree.

## Installation

To install TreeDataStructures.jl, use the Julia's package manager:

```
] add https://github.com/eliascarv/TreeDataStructures.jl
```

## AVL Tree

An AVL tree is a binary search tree that keeps itself balanced to ensure efficient search, insertion, and deletion operations.

### Example Usage

```julia
tree = AVLTree{Int,Float64}()

# add nodes to the tree
tree[2] = 2.2 # root node
tree[1] = 1.1 # left node
tree[3] = 3.3 # right node

# update the value of the node
tree[2] = 2.4

# get the value of the node using its key
tree[2] # 2.4
tree[1] # 1.1
tree[3] # 3.3

# delete nodes from the tree
delete!(tree, 1)
delete!(tree, 3)
```
54 changes: 54 additions & 0 deletions src/avl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,78 @@ end
# AVL TREE
# ---------

"""
AVLTree{K,V}()

Construct an empty AVL Tree with keys of type `K`
and values of type `V`.

AVLTree()

Construct an empty AVL Tree that stores keys and values
of any type, alias for `AVLTree{Any,Any}()`.

The keys of AVL Tree must implement sorting operators (`>`, `<`)
and comparison operators (`=`, `≠`)

# Examples

```julia
tree = AVLTree{Int,Float64}()

# add nodes to the tree
tree[2] = 2.2 # root node
tree[1] = 1.1 # left node
tree[3] = 3.3 # right node

# update the value of the node
tree[2] = 2.4

# get the value of the node using its key
tree[2] # 2.4
tree[1] # 1.1
tree[3] # 3.3

# delete nodes from the tree
delete!(tree, 1)
delete!(tree, 3)
```
"""
mutable struct AVLTree{K,V}
root::Union{AVLNode{K,V},Nothing}
end

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

"""
getindex(tree::AVLTree{K}, key::K) where {K}

Get the value stored in the node that has `key`.
"""
function Base.getindex(tree::AVLTree{K}, key::K) where {K}
node = _search(tree, key)
isnothing(node) && throw(KeyError(key))
node.value
end

"""
setindex!(tree::AVLTree{K}, value, key::K) where {K}

Add a node to the tree with `key` and `value`.
If a node with `key` already exists, the value
of the node will be updated.
"""
function Base.setindex!(tree::AVLTree{K}, value, key::K) where {K}
tree.root = _insert!(tree.root, key, value)
tree
end

"""
delete!(tree::AVLTree{K}, key::K) where {K}

Delete the node that has `key` from the tree.
"""
function Base.delete!(tree::AVLTree{K}, key::K) where {K}
tree.root = _delete!(tree.root, key)
tree
Expand Down