-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.py
More file actions
145 lines (120 loc) · 4.04 KB
/
BinarySearchTree.py
File metadata and controls
145 lines (120 loc) · 4.04 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
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 29 18:19:12 2022
@FileName: .py
@author: YUNJUSEOK
"""
import gc;
class Node:
def __init__( self, data ):
self.data = data;
self.parent = None;
self.left = None;
self.right = None;
class BST:
def __init__( self, root ):
self.head = Node( root );
def insert( self, data ):
pos = self.head;
while( True ):
if( pos.data > data ):
if( pos.left != None ):
pos = pos.left;
continue;
pos.left = Node( data );
pos.left.parent = pos;
break;
else:
if( pos.right != None ):
pos = pos.right;
continue;
pos.right = Node( data );
pos.right.parent = pos
break;
def search( self, data ):
pos = self.head;
while( True ):
if( pos == None or pos.data == data ):
return pos
elif( pos.data > data ):
pos = pos.left;
elif( pos.data < data ):
pos = pos.right;
def delete( self, data ):
pos = self.search( data );
if( pos == None ):
print("삭제하시려는 값이 Tree에 존재하지 않습니다. ");
return
if( self.getLeaf( pos )== 0 ):
idx = self.getChildPath( pos.parent, pos.data );
if( idx == 1 ):
pos.parent.left = None;
print(" 삭제 완료: {0}", pos.data );
elif( idx == 2 ):
pos.parent.right = None;
print(" 삭제 완료: {0}", pos.data );
else:
raise Exception(" Error 발생 ");
elif( self.getLeaf( pos )== 1 ):
idx = self.getChildPath( pos.parent, pos.data );
if( pos.right == None ):
if( idx == 1 ):
pos.parent.left = pos.left;
print(" 삭제 완료: {0}", pos.data );
elif( idx == 2 ):
pos.parent.right = pos.left;
print(" 삭제 완료: {0}", pos.data );
else:
raise Exception(" Error 발생 ");
else:
if( idx == 1 ):
pos.parent.left = pos.right;
print(" 삭제 완료: {0}", pos.data );
elif( idx == 2 ):
pos.parent.right = pos.right;
print(" 삭제 완료: {0}", pos.data );
else:
raise Exception(" Error 발생 ");
else:
def inOrder( self, node ):
if( node == None ):
return ;
self.inOrder( node.left );
print( node.data );
self.inOrder( node.right );
def getChildPath( pos, value ):
if( pos.left == value ):
return 1;
elif( pos.right == value ):
return 2;
else:
return 0;
#inOrder
def toString( self ):
self.inOrder( self.head );
def getMin( self, subTree):
while( self.getLeaf( subTree) == 0 ):
if()
def getLeaf( Node ):
idx = 0;
if( Node.left != None ):
idx += 1;
if( Node.right != None ):
idx += 1;
return idx;
def main():
Bst = BST( 50 );
Bst.insert( 30 );
Bst.insert( 20 );
Bst.insert( 40 );
Bst.insert( 70 );
Bst.insert( 90 );
Bst.search( 15 );
if( Bst.search( 20 ) == None ):
print(" 찾으시는 게 없습니다. ");
else:
print(" 찾으시는 게 있습니다. 부모:", end= str( Bst.search( 20 ).parent.data ) );
print( "\n" );
Bst.toString();
if( __name__ == "__main__"):
main();