From 05b1674c6e55354f13114f01aad126eeaf5e4158 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 21 Oct 2024 16:25:51 -0300 Subject: [PATCH 1/2] Add docstrings --- src/avl.jl | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/avl.jl b/src/avl.jl index 7686591..2b55250 100644 --- a/src/avl.jl +++ b/src/avl.jl @@ -43,6 +43,43 @@ 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 a node +tree[2] = 2.4 + +# get the value of a 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 @@ -50,17 +87,34 @@ 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 From 6f78df4e908e698a8eb4b88b18211af4b57b93c0 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 21 Oct 2024 16:45:23 -0300 Subject: [PATCH 2/2] Add README --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ src/avl.jl | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c22693a..e18f39f 100644 --- a/README.md +++ b/README.md @@ -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) +``` diff --git a/src/avl.jl b/src/avl.jl index 2b55250..ff68597 100644 --- a/src/avl.jl +++ b/src/avl.jl @@ -67,10 +67,10 @@ tree[2] = 2.2 # root node tree[1] = 1.1 # left node tree[3] = 3.3 # right node -# update the value of a node +# update the value of the node tree[2] = 2.4 -# get the value of a node using its key +# get the value of the node using its key tree[2] # 2.4 tree[1] # 1.1 tree[3] # 3.3