red-black-tree-0.1.0.0: Red Black Trees implemented in Haskell
Copyright(c) 2017 Gabriel Aumala
LicenseBSD3
Maintainergabriel@criptext.com
Stabilityexperimental
PortabilityGHC
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.RedBlackTree.BinaryTree

Description

A Binary Search Tree implementation. It exposes various functions to operate on the tree that are used internally by Data.RedBlackTree. This is meant to be used exclusively by Data.RedBlackTree.Internal, it is not adequate to be used as a standalone binary tree structure.

Synopsis

Documentation

class Ord a => BinaryTreeNode a where Source #

Only types that are members of BinaryTreeNode can be inserted into a BinaryTree.

The purpose of the class is to provide a method to merge nodes with equal values since inserting different nodes with equal values can corrupt the tree.

Methods

mergeNodes :: a -> a -> a Source #

The BinaryTree will call this function when it tries to insert a value that already exists in the tree. The first argument is guaranteed to be the one that is already in the tree, while the second argument is the node that the tree is trying to insert. Since the two nodes can't exist in the same tree The result should be a merged node that will be inserted instead of the other two.

Instances

Instances details
BinaryTreeNode a => BinaryTreeNode (RedBlackNode a) Source # 
Instance details

Defined in Data.RedBlackTree.Internal

data BinaryTree a Source #

A BinaryTree is either a Leaf (empty) or a BinaryTreeNode with 2 BinaryTree children: left and right.

Constructors

Leaf 
Branch (BinaryTree a) a (BinaryTree a) 

Instances

Instances details
(BinaryTreeNode a, Show a) => Show (BinaryTree a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

Eq a => Eq (BinaryTree a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

Methods

(==) :: BinaryTree a -> BinaryTree a -> Bool #

(/=) :: BinaryTree a -> BinaryTree a -> Bool #

Ord a => Ord (BinaryTree a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

data BranchType Source #

A BinaryTree can only have two types of branches: Left or Right

Constructors

LeftBranch 
RightBranch 

type BranchZipper a = (TreeBranch a, TreeDirections a) Source #

A TreeBranch zipper. It is identical to TreeZipper except for the fact that Leaf values are not allowed in the zipper.

data TreeBranch a Source #

Holds the data of a BinaryTree created with the Branch constructor. Useful type when you want to guarantee that the element is not a Leaf

Constructors

TreeBranch (BinaryTree a) a (BinaryTree a) 

Instances

Instances details
(BinaryTreeNode a, Show a) => Show (TreeBranch a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

Eq a => Eq (TreeBranch a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

Methods

(==) :: TreeBranch a -> TreeBranch a -> Bool #

(/=) :: TreeBranch a -> TreeBranch a -> Bool #

Ord a => Ord (TreeBranch a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

data TreeDirection a Source #

Minimum necessary to reconstruct the parent of any focused node. First argument is the BranchType of the focused node relative to the parent. Second argument is the parent's node. The third argument is the sibling tree of the focused node.

Constructors

TreeDirection BranchType a (BinaryTree a) 

type TreeDirections a = [TreeDirection a] Source #

List of TreeDirection

data TreeInsertResult a Source #

The result from inserting a node to the left or right of a tree can be:

  • (InsertOk insertedTree directionToNewTree) if there is a leaf at the attempted insert position
  • (InsertNotYet obstructingTree directionToObstructingTree nodeToInsert) if there already is a tree obstructing the desired position, we must go further down
  • (InsertMerge mergedBranch) the node to insert is equal to the tree's node so they were merged and the tree's size remains the same

Instances

Instances details
(BinaryTreeNode a, Show a) => Show (TreeInsertResult a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

Eq a => Eq (TreeInsertResult a) Source # 
Instance details

Defined in Data.RedBlackTree.BinaryTree

type TreeZipper a = (BinaryTree a, TreeDirections a) Source #

A BinaryTree zipper. the first value of the tuple is the focused BinaryTree, while the second argument is the list of directions used to move up to the parent and other ancestors.

It is used to navigate up an down in a BinaryTree.

binaryTreeInsert :: BinaryTreeNode a => BinaryTree a -> a -> BranchZipper a Source #

inserts an item to the BinaryTree. Returns a BranchZipper focusing on the recently inserted branch.

binaryTreeFind :: BinaryTreeNode a => BinaryTree a -> a -> Maybe a Source #

Looks up an item in the BinaryTree. Returns Nothing if it was not found.