Skip to content

Commit 8abd5b4

Browse files
committed
Add a BinaryTree Structure
1 parent 0423cd2 commit 8abd5b4

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

src/script/binarytree.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
6+
#ifndef BITCOIN_BINARYTREE_H
7+
#define BITCOIN_BINARYTREE_H
8+
9+
10+
#include <script/script.h>
11+
#include <util/memory.h>
12+
#include <memory>
13+
14+
15+
template <typename T>
16+
class BinaryTree
17+
{
18+
protected:
19+
BinaryTree* parent = nullptr;
20+
std::unique_ptr<BinaryTree> left = nullptr;
21+
std::unique_ptr<BinaryTree> right = nullptr;
22+
std::unique_ptr<T> obj = nullptr;
23+
24+
public:
25+
bool IsRoot() const { return parent == nullptr; }
26+
bool IsLeaf() const { return obj != nullptr; }
27+
bool IsNode() const { return !IsLeaf(); }
28+
bool IsEmpty() const { return !left && !right && !IsLeaf(); }
29+
bool IsFull() const { return (left && right) || IsLeaf(); }
30+
31+
const T* GetObj() const
32+
{
33+
if (!IsLeaf()) return nullptr;
34+
return obj.get();
35+
}
36+
37+
bool InsertLeaf(std::unique_ptr<T> sc)
38+
{
39+
if (!IsEmpty()) return false;
40+
if (!sc) return false;
41+
obj = std::move(sc);
42+
return true;
43+
}
44+
bool InsertRight(BinaryTree child)
45+
{
46+
if (right) return false;
47+
child.parent = this;
48+
right = MakeUnique<BinaryTree>(std::move(child));
49+
return true;
50+
}
51+
bool InsertLeft(BinaryTree child)
52+
{
53+
if (left) return false;
54+
child.parent = this;
55+
left = MakeUnique<BinaryTree>(std::move(child));
56+
return true;
57+
}
58+
BinaryTree* GetInitLeftChild()
59+
{
60+
if (IsLeaf()) return nullptr;
61+
if (!left) left = MakeUnique<BinaryTree>(this);
62+
return left.get();
63+
}
64+
BinaryTree* GetInitRightChild()
65+
{
66+
if (!IsNode()) return nullptr;
67+
if (!right) right = MakeUnique<BinaryTree>(BinaryTree(this));
68+
return right.get();
69+
}
70+
71+
BinaryTree* GetInitRightSibling()
72+
{
73+
if (!parent) return nullptr;
74+
if (!parent->right) parent->right = MakeUnique<BinaryTree>(BinaryTree(parent));
75+
if ((&*parent->right) == this) return nullptr;
76+
return parent->right.get();
77+
}
78+
BinaryTree* GetInitLeftSibling()
79+
{
80+
if (!parent) return nullptr;
81+
if (!parent->left) parent->left = MakeUnique<BinaryTree>(BinaryTree(parent));
82+
return parent->left.get();
83+
}
84+
85+
bool RemoveLeftChild()
86+
{
87+
if (!left) return false;
88+
left.reset();
89+
return true;
90+
}
91+
92+
std::unique_ptr<T> ReplaceLeaf(std::unique_ptr<T> sc)
93+
{
94+
obj.swap(sc);
95+
return sc;
96+
}
97+
98+
BinaryTree* GetRawRightChild() const { return right.get(); }
99+
BinaryTree* GetRawLeftChild() const { return left.get(); }
100+
BinaryTree* GetParent() const { return parent; }
101+
102+
explicit BinaryTree() = default;
103+
explicit BinaryTree(T sc) {obj = MakeUnique(std::move(sc));}
104+
explicit BinaryTree(std::unique_ptr<T> sc) {obj = std::move(sc);}
105+
explicit BinaryTree(BinaryTree* par) {parent = par;}
106+
107+
};
108+
109+
110+
#endif

src/script/descriptor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <script/script.h>
99
#include <script/sign.h>
1010
#include <script/signingprovider.h>
11+
#include <script/binarytree.h>
1112

1213
#include <vector>
1314

0 commit comments

Comments
 (0)