Binary Search Tree
Data structure 2020. 5. 29. 17:43https://en.wikipedia.org/wiki/Binary_expression_tree
https://ko.wikipedia.org/wiki/%EC%9D%B4%EC%A7%84_%ED%8A%B8%EB%A6%AC
http://www.csharpstudy.com/DS/bst.aspx
https://tystory.tistory.com/290
https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/
https://www.youtube.com/watch?v=r3iW552f-kk
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BinarySearchTree;
namespace BinarySearchTree
{
public class BinaryTreeNode<T>
{
public T Data { get; set; }
public BinaryTreeNode<T> Left { get; set; }
public BinaryTreeNode<T> Right { get; set; }
public BinaryTreeNode(T data)
{
this.Data = data;
}
}
}
// 이진검색트리 클래스
public class BST<T>
{
private BinaryTreeNode<T> root = null;
private Comparer<T> comparer = Comparer<T>.Default;
public void Insert(T val)
{
BinaryTreeNode<T> node = this.root;
if (node == null)
{
this.root = new BinaryTreeNode<T>(val);
return;
}
while(node != null)
{
int result = comparer.Compare(node.Data, val);
if (result == 0)
{
//throw new InvalidDataException("Duplicate value");
return;
}
else if (result > 0)
{
if (node.Left == null)
{
node.Left = new BinaryTreeNode<T>(val);
return;
}
node = node.Left;
}
else
{
if (node.Right == null)
{
node.Right = new BinaryTreeNode<T>(val);
return;
}
node = node.Right;
}
}
}
public void PreOrderTraversal()
{
PreOrderRecursive(this.root);
}
private void PreOrderRecursive(BinaryTreeNode<T> node)
{
if (node == null) return;
Console.WriteLine(node.Data);
PreOrderRecursive(node.Left);
PreOrderRecursive(node.Right);
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
namespace study_01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
BST<int> bst = new BST<int>();
bst.Insert(4);
bst.Insert(2);
bst.Insert(6);
bst.Insert(1);
bst.Insert(7);
bst.PreOrderTraversal();
}
}
}
|
cs |
'Data structure' 카테고리의 다른 글
배열로 구현한 Stack in C# (0) | 2020.10.23 |
---|---|
단일연결리스트 (SinglyLinkedList) in c# (0) | 2020.10.23 |
이진 트리 (0) | 2019.08.14 |
이진 탐색 (Binary Search) (0) | 2019.08.14 |
퀵 정렬( Quick Sort) (0) | 2019.08.14 |