-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_node.h
More file actions
55 lines (49 loc) · 1.81 KB
/
bin_node.h
File metadata and controls
55 lines (49 loc) · 1.81 KB
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
#pragma once
#include <iostream>
#define binNodePos(T) binNode<T>*
#define stature(p) ((p) ? (p)->height_ : -1)
#define IsRoot(x) (!((x).parent_))
#define IsLeftChild(x) (!IsRoot(x) && (&(x) == (x).parent_->left_child_))
#define IsRightChild(x) (!IsRoot(x) && (&(x) == (x).parent_->right_child_))
#define HasParent(x) (!IsRoot(x))
#define HasLeftChild(x) ((x).left_child_)
#define HasRightChild(x) ((x).right_child_)
#define HasChild(x) (HasLeftChild(x) || HasRightChild(x))
#define HasBothChild(x) ( HasLeftChild(x) && HasRightChild(x))
#define IsLeaf(x) (!HasChild(x))
/*useful for avl tree*/
#define Balanced(x) (stature((x).left_child_) == stature((x).right_child_))
#define BalFac(x) (stature((x).left_child_) - stature((x).right_child_))
#define AvlBalanced(x) (-2 < BalFac(x) && BalFac(x) < 2)
#define tallerChild(x) (\
stature((x).left_child_) > stature((x).right_child_) ? (x).left_child_ : ( \
stature((x).left_child_) < stature((x).right_child_) ? (x).right_child_ : ( \
IsLeftChild(x) ? (x).left_child_ : (x).right_child_ ) \
) \
)
template <typename T>
struct binNode {
T data_;
binNodePos(T) parent_;
binNodePos(T) left_child_;
binNodePos(T) right_child_;
int height_;
binNode():
parent_(nullptr), left_child_(nullptr), right_child_(nullptr), height_(0)
{}
binNode(T in_data,
binNodePos(T) in_parent = nullptr,
binNodePos(T) in_left_child = nullptr,
binNodePos(T) in_right_child = nullptr,
int in_height = 0):
data_(in_data),
parent_(in_parent),
left_child_(in_left_child),
right_child_(in_right_child),
height_(in_height)
{}
int size();
binNodePos(T) succ();
binNodePos(T) insertAsLeftChild(T const& data);
binNodePos(T) InsertAsRightChild(T const& data);
};