Professional Documents
Culture Documents
1. To template:
to ra 1 cu trc d liu list/vector/tree cc object l 1 class/struct ch khng phi l kiu
d liu n thun nh int, double,string, ta phi to template lm 1 khun mu p dng cho
tt c object c kiu class bt k m khng cn to li cho mi class khc nhau.
to 1 template , ta s dng c php;
template<typename T>
hoc template<class T> (nn dng khi object lun l class)
Sau s dng T nh 1 object bnh thng .
Khi mun s dng template, ta thay T bng 1 object c kiu bt k.
VD: Khi to Tree mun ly d liu ca 1 node l class T
template<typename _T>
struct _Tree_node{
_Tree_node root,left,right;
}
template<typename T>
class _Tree{
_Node *_root,_left,_right;
_T _data
; //s ly data l d liu ca node;
public:
T getData{
return _data;
}
void setData(_T _input){
_data=_input;
}
}
Khi s dng:
main(){
int k;
_Tree<int>example;
example._root->setData(5)
k = example->root.getData();
cout<<k<<endl;
}
*y ch l v d, cn thm cc function khc chng trnh mi chy c.
II>To binary search Tree:
1.Cu trc 1 Binary Tree:
mi Node trong tree l c nhiu nht 2 con. 1 node con c th rng hoc c node phi
hoc tri hoc c 2.
Level
left
right
Level
Height: 3
right
left
left
Level