-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cs
More file actions
173 lines (149 loc) · 4.07 KB
/
BST.cs
File metadata and controls
173 lines (149 loc) · 4.07 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class BinaryTree
{
private Node root;
private int count;
public BinaryTree()
{
root = null;
count = 0;
}
public bool isEmpty()
{
return root == null;
}
public void insert(int d)
{
if (isEmpty())
{
root = new Node(d);
}
else
{
root.insertData(ref root, d);
}
count++;
}
public bool search(int s)
{
return root.search(root, s);
}
public bool isLeaf()
{
if (!isEmpty())
return root.isLeaf(ref root);
return true;
}
public void display()
{
if (!isEmpty())
root.display(root);
}
public int Count()
{
return count;
}
}
class Node
{
private int number;
public Node rightLeaf;
public Node leftLeaf;
public Node(int value)
{
number = value;
rightLeaf = null;
leftLeaf = null;
}
public bool isLeaf(ref Node node)
{
return (node.rightLeaf == null && node.leftLeaf == null);
}
public void insertData(ref Node node, int data)
{
if (node == null)
{
node = new Node(data);
}
else if (node.number < data)
{
insertData(ref node.rightLeaf, data);
}
else if (node.number > data)
{
insertData(ref node.leftLeaf, data);
}
}
public bool search(Node node, int s)
{
if (node == null)
return false;
if (node.number == s)
{
return true;
}
else if (node.number < s)
{
return search(node.rightLeaf, s);
}
else if (node.number > s)
{
return search(node.leftLeaf, s);
}
return false;
}
public void display(Node n)
{
if (n == null)
return;
display(n.leftLeaf);
Console.Write(" " + n.number);
display(n.rightLeaf);
}
}
class wykonywujacy
{
public static void program()
{
BinaryTree b = new BinaryTree();
int a = 0;
while (a != 9)
{
Console.Clear();
Console.WriteLine("Witaj, którą czynność chcesz wykonać:");
Console.WriteLine("1.Wprowadź wartosć do drzewa");
Console.WriteLine("2.przeszukaj drzewo");
Console.WriteLine("3.Wyświetl drzewo");
Console.WriteLine("9.Powrót do menu głównego");
if (!Int32.TryParse(Console.ReadLine(), out a))
{
continue;
}
if (a == 1)
{
Console.WriteLine("Jaką wartosć chcesz wprowadzić?");
int q = Int32.Parse(Console.ReadLine());
b.insert(q);
}
if (a == 2)
{
Console.WriteLine("Jaką wartosć chcesz znaleść?");
int q = Int32.Parse(Console.ReadLine());
b.search(q);
}
if (a == 3)
{
Console.WriteLine("To są wartosci w drzewie:");
b.display();
}
Console.ReadKey();
}
}
}
}