aboutsummaryrefslogtreecommitdiff
path: root/include/libft_rbt.h
blob: 57653cc3731ac71fc9bf367c29fcdf730a83b510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   libft_rbt.h                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/26 16:09:51 by charles           #+#    #+#             */
/*   Updated: 2020/04/26 20:25:36 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef LIBFT_RBT_H
# define LIBFT_RBT_H

/*
** \file   libft_rbt.h
** \brief  Red-black tree
**
** Rules: (from wikipedia)
** 1. Each node is either red or black.
** 2. The root is black. This rule is sometimes omitted. Since the root can
**    always be changed from red to black, but not necessarily vice versa,
**    this rule has little effect on analysis.
** 3. All leaves (NULL) are black.
** 4. If a node is red, then both its children are black.
** 5. Every path from a given node to any of its descendant NULL nodes goes
**    through the same number of black nodes.
**
**
** Unbalance case:
**
** 1. Node and parent are both red
**
**   B1
**    \     left rot         color swap
**   > R2     -->       R2      -->       B2
**      \              /  \              /  \
**     > R3           B1  R3            R1  R3
**
** 2. Node and parent are both red and uncle node is red
**
**          B2                    R2
**         / \     color swap    / \
** uncle> R1  R3      -->       B1  B3
**             \                     \
**             R4                    R4
*/

# include <stdlib.h>

/*
** \brief                    Red-black tree color enum
** \param FTRBT_COLOR_RED    color red
** \param FTRBT_COLOR_BLACK  color black
*/

enum					e_ftrbt_color
{
	FTRBT_COLOR_RED = 0,
	FTRBT_COLOR_BLACK,
};

/*
** \brief         Red-black tree struct
** \param left    Left node
** \param right   Right node
** \param data    Pointer to data
** \param parent  Parent node
** \param color   Color of the node
** \note          The first 3 attricutes are the same
**                as t_ftbt (binary tree) struct
**                which means that we can use all functions
**                of binary tree on red-black tree.
*/

typedef struct			s_ftrbt
{
	struct s_ftrbt		*left;
	struct s_ftrbt		*right;
	void				*data;
	struct s_ftrbt		*parent;
	enum e_ftrbt_color	color;
}						t_ftrbt;

t_ftrbt					*ft_rbtnew(void *data, enum e_ftrbt_color color);
void					ft_rbtrotate_right(t_ftrbt **tree);
void					ft_rbtrotate_left(t_ftrbt **tree);

t_ftrbt					*ft_rbtinsert(
							t_ftrbt *tree,
							void *data,
							int (*cmp)(void*, void*));


#endif