-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitree.d
84 lines (70 loc) · 1.4 KB
/
bitree.d
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
import std.stdio;
void main() {
Tree tree;
writeln( "Inserting" );
tree.insert( [9,2,8,3,7,6,4,0,0,1]);
writeln( "printing" );
tree.print;
}
struct Tree {
Node top;
class Node {
Node
left = null,
right = null;
bool leaf = false; // not sure on
alias int data;
data _num;
int num() { return _num; }
this( int num0 ) {
_num = num0;
}
}
typeof(this) insert( int[] nums ) {
foreach( num; nums ) {
insert( num );
}
return this;
}
typeof(this) insert( int num ) {
//Adding
writeln( num );
if ( top is null ) {
top = new Node( num );
return this;
}
auto node = top;
while ( node !is null ) {
if ( node.left !is null && node.right !is null ) {
if ( node.left.num > node.right.num )
node = node.left, writeln( "bigger" );
else
node = node.right, writeln( "smaller" );
} else if ( node.left !is null )
node = node.left, writeln( "left !is null" );
else if ( node.right !is null )
node = node.right, writeln( "right !is null" );
else
node = null, writeln( "null" );
}
node = new Node( num );
return this;
}
typeof(this) print() {
Node seek( Node node ) {
writeln( node.num );
if ( node.left !is null )
seek( node.left );
else if ( node.right !is null )
seek( node.right );
return null;
}
seek(top);
/*
auto node = top;
while( node !is null ) {
}
*/
return this;
}
}