Skip to content

Commit

Permalink
Merge pull request #4 from Jejulia/v0.2.0
Browse files Browse the repository at this point in the history
Modify type / improve coverage
  • Loading branch information
yufongpeng authored Dec 24, 2020
2 parents d8cb8dc + ae95e3e commit 4f94990
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TreesHeaps"
uuid = "11f3e0e9-035a-4796-9f15-e39fd2303e8e"
authors = ["Jejulia <Jejulia@users.noreply.github.com>"]
version = "0.1.0"
version = "0.2.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[codecov-img]: https://codecov.io/gh/Jejulia/TreesHeaps.jl/coveage.svg
[codecov-url]: https://codecov.io/gh/Jejulia/TreesHeaps.jl

Implementation of various tree and heaps(to do) structures. Only binary search tree, AVL tree, splay tree are supported now. Available operations includes `search!`, `insert!`, `delete!`, `splay!`, `topdownsplay!`, `findmin`, `findmax`. Outputs are formatted through `AbstractTrees.jl`. Other tree structures and plotting recipe are under developments!
Implementation of various tree and heaps (to do) structures. Only binary search tree, AVL tree, splay tree are supported now. Available operations includes `search`, `insert!`, `topdowninsert!`, `delete!`, `topdowndelete!`, `splay!`, `topdownsplay!`, `findmin`, `findmax`. Outputs are formatted through `AbstractTrees.jl`. Other structures and plotting recipe are under developments!

## TO DO
1. Red-black tree
Expand Down
85 changes: 43 additions & 42 deletions src/Balance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ isnull(Node::AbstractNode) = false
isnull(Node::NullNode) = true

height(Node::AbstractNode) = 0
height(Node::HeightBinaryNode) = Node.height
height(Node::HBN) = Node.height
height(Node::NullNode) = -1

"""
Expand Down Expand Up @@ -49,7 +49,7 @@ cut!(::NullNode, ::AbstractNode, ::Symbol) = nothing
Get direction of parent to child.
"""
getdir(parent::AbstractNode, child) = parent.left == child ? (:left) : (:right)
getdir(parent::AbstractBinaryNode, child) = parent.left == child ? (:left) : (:right)
getdir(::NullNode, ::AbstractNode) = nothing
getdir(::NullNode, ::NullNode) = nothing

Expand All @@ -59,9 +59,9 @@ getproperty(::NullNode, ::Nothing) = NullNode
# -------------------------------------------------------------------------------------------------
# rotations

function SingleRotation!(grandparent::AbstractNode,
parent::AbstractNode,
child::AbstractNode,
function SingleRotation!(grandparent::HBN,
parent::HBN,
child::HBN,
dir1::Symbol)
dir2 = opposite(dir1)
sister = getproperty(parent, dir2)
Expand All @@ -74,9 +74,9 @@ function SingleRotation!(grandparent::AbstractNode,
return ggparent, parent, child, dir3
end

function DoubleRotation!(grandparent::AbstractNode,
parent::AbstractNode,
child::AbstractNode,
function DoubleRotation!(grandparent::HBN,
parent::HBN,
child::HBN,
dir1::Symbol,
dir2::Symbol)
ggparent = grandparent.parent
Expand All @@ -94,8 +94,8 @@ function DoubleRotation!(grandparent::AbstractNode,
return ggparent, child, parent, dir3
end

function Zig!(grandparent::SimpleBinaryNode,
parent::SimpleBinaryNode,
function Zig!(grandparent::SBN,
parent::SBN,
dir1::Symbol)
dir2 = opposite(dir1)
child = getproperty(parent, dir2)
Expand All @@ -106,9 +106,9 @@ function Zig!(grandparent::SimpleBinaryNode,
end


function ZigZig!(grandparent::SimpleBinaryNode,
parent::SimpleBinaryNode,
child::SimpleBinaryNode,
function ZigZig!(grandparent::SBN,
parent::SBN,
child::SBN,
dir1::Symbol,
dir2::Symbol)
sister = getproperty(parent, dir2)
Expand All @@ -123,9 +123,9 @@ function ZigZig!(grandparent::SimpleBinaryNode,
return ggparent, dir3
end

function ZigZag!(grandparent::SimpleBinaryNode,
parent::SimpleBinaryNode,
child::SimpleBinaryNode,
function ZigZag!(grandparent::SBN,
parent::SBN,
child::SBN,
dir1::Symbol,
dir2::Symbol)
grandchild1 = getproperty(child, dir1)
Expand All @@ -140,17 +140,17 @@ function ZigZag!(grandparent::SimpleBinaryNode,
return ggparent, dir3
end

function TopDownZig!(parent::SimpleBinaryNode,
child::SimpleBinaryNode,
function TopDownZig!(parent::SBN,
child::SBN,
dir::Symbol)
cut!(parent, child, dir)
dir == :left ? (return child, NullNode(), parent) : (return child, parent, NullNode())
end


function TopDownZigZig!(grandparent::SimpleBinaryNode,
parent::SimpleBinaryNode,
child::SimpleBinaryNode,
function TopDownZigZig!(grandparent::SBN,
parent::SBN,
child::SBN,
dir1::Symbol,
dir2::Symbol)
sister = getproperty(parent, dir2)
Expand All @@ -161,9 +161,9 @@ function TopDownZigZig!(grandparent::SimpleBinaryNode,
dir1 == :left ? (return child, NullNode(), parent) : (return child, parent, NullNode())
end

function TopDownZigZag!(grandparent::SimpleBinaryNode,
parent::SimpleBinaryNode,
child::SimpleBinaryNode,
function TopDownZigZag!(grandparent::SBN,
parent::SBN,
child::SBN,
dir1::Symbol,
dir2::Symbol)
cut!(grandparent, parent, dir1)
Expand All @@ -174,8 +174,8 @@ end
# -----------------------------------------------------------------------------------------------
# BinarySearchTree traverse

function traverse!(tree::BinarySearchTree,
node::HeightBinaryNode,
function traverse!(tree::BST,
node::HBN,
nodes...)
value = max(-1, [child.height for child in node]...) + 1
if node.height != value && (node.height = value; true)
Expand All @@ -184,11 +184,11 @@ function traverse!(tree::BinarySearchTree,
return tree
end

traverse!(tree::BinarySearchTree,
::SimpleBinaryNode,
traverse!(tree::BST,
::SBN,
nodes...) = tree

function traverse!(tree::BinarySearchTree,
function traverse!(tree::BST,
::NullNode,
nodes...)
tree.height = first(nodes).height
Expand All @@ -201,7 +201,7 @@ end
## Root case
function traverse!(tree::AVLTree,
::NullNode,
parent::HeightBinaryNode,
parent::HBN,
nodes...)
tree.height = parent.height
return tree
Expand All @@ -210,7 +210,7 @@ end
# traverse back to root
function AVLtraverse!(tree::AVLTree,
::NullNode,
parent::HeightBinaryNode,
parent::HBN,
nodes...)
tree.root = parent
tree.height = parent.height
Expand All @@ -219,8 +219,8 @@ end

## insert! case
function traverse!(tree::AVLTree,
parent::HeightBinaryNode,
child::HeightBinaryNode)
parent::HBN,
child::HBN)
if parent.height == 0
parent.height = 1
return AVLtraverse!(tree, parent.parent, parent, child, getdir(parent.parent, parent), getdir(parent, child))
Expand All @@ -230,9 +230,9 @@ function traverse!(tree::AVLTree,
end

function AVLtraverse!(tree::AVLTree,
grandparent::HeightBinaryNode,
parent::HeightBinaryNode,
child::HeightBinaryNode,
grandparent::HBN,
parent::HBN,
child::HBN,
dir1::Symbol,
dir2::Symbol)
delta = [child.height for child in grandparent]
Expand All @@ -258,7 +258,7 @@ function AVLtraverse!(tree::AVLTree,
end

## delete! case
function traverse!(tree::AVLTree, grandparent::HeightBinaryNode)
function traverse!(tree::AVLTree, grandparent::HBN)
parent = grandparent.left
aunt = grandparent.right
if height(parent) > height(aunt)
Expand All @@ -270,8 +270,8 @@ end


function AVLtraverse!(tree::AVLTree,
grandparent::HeightBinaryNode,
aunt::Union{NullNode, HeightBinaryNode},
grandparent::HBN,
aunt::Union{NullNode, HBN},
dir1::Symbol,
dir2::Symbol)
parent, child1, child2 = findchild(grandparent, dir1)
Expand All @@ -281,6 +281,7 @@ function AVLtraverse!(tree::AVLTree,
if height(child1) > height(child2)
grandparent, parent, child, dir3 = SingleRotation!(grandparent, parent, child1, dir1)
elseif height(child1) < height(child2)
println("good")
grandparent, parent, child, dir3 = DoubleRotation!(grandparent, parent, child2, dir1, dir2)
else
grandparent, parent, child, dir3 = DoubleRotation!(grandparent, parent, child2, dir1, dir2)
Expand Down Expand Up @@ -309,8 +310,8 @@ end
# ------------------------------------------------------------------------------------
# Splay tree traverse
function Splaytraverse!(tree::SplayTree,
parent::SimpleBinaryNode,
child::SimpleBinaryNode,
parent::SBN,
child::SBN,
dir2::Symbol)
grandparent = parent.parent
if isnull(grandparent)
Expand All @@ -332,7 +333,7 @@ end
# Root case
function Splaytraverse!(tree::SplayTree,
::NullNode,
child::SimpleBinaryNode,
child::SBN,
::Nothing)
tree.root = child
return tree
Expand Down
Loading

2 comments on commit 4f94990

@yufongpeng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/26870

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 4f949903e4deee7b653cbca6cf058f3557a6a2f1
git push origin v0.2.0

Please sign in to comment.