4. Мод
Root node
Leaf nodes
Interior nodes
Height
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
5. Хоѐртын мод
Хоѐртын мод нь модны нэг онцгой тохиолдлын нэг бөгөөд
элемент бүр нь хоѐроос илүүгүй дэд зангилаатай байдаг. Хэрэв
мод нэг ч элемент агуулаагүй бол хоосон мод буюу NULL мод
юмуу элемент гэж нэрлэнэ. Элементийн дэд 2 элементийг зүүн
дэд элемент, баруун дэд элемент гэж нэрэлдэг. Хоѐртын модны
элемент нь 1, 2 дэд элемент агуулж болно. Харин төгсгөлийн
элементүүд нь дэд элементүүдгүй бөгөөд 2 хоосон дэд модтой
байна.
Tree Binary Tree
Tree
Nodes
Each node can have 0 or more children
A node can have at most one parent
Binary tree
Tree with 0–2 children per node
10. Хоёртын мод байгуулах
11 void insert(node ** tree, int val) {
12 node *temp = NULL;
13 if(!(*tree)) {
14 temp = (node *)malloc(sizeof(node));
15 temp->left = temp->right = NULL;
16 temp->data = val;
17 *tree = temp;
18 return;
19 }
20
21 if(val < (*tree)->data) {
22 insert(&(*tree)->left, val);
23 } else if(val > (*tree)->data) {
24 insert(&(*tree)->right, val);
25 }
26 }
Хоѐртын модны элемент нь BinTree загвар классаар тодорхойлогдох
бөгөөд өгөгдөл болон зүүн баруун салаануудыг заах заагчуудыг агуулж
байна. setr(), setl() гишүүн функцүүд нь тухайн элементийн баруун ба зүүн
дэд модыг холбож өгөх ба харин getr(), getl() гишүүн функцүүд нь тухайн
элементийн баруун ба зүүн дэд модыг буцаана.
[Lines 13-19] Check first if tree is empty, then
insert node as root.
[Line 21] Check if node value to be inserted is
lesser than root node value, then
a. [Line 22] Call insert() function recursively
while there is non-NULL left node
b. [Lines 13-19] When reached to leftmost
node as NULL, insert new node.
[Line 23] Check if node value to be inserted is
greater than root node value, then
a. [Line 24] Call insert() function recursively
while there is non-NULL right node
b. [Lines 13-19] When reached to rightmost
node as NULL, insert new node.
13. Хоёртын модоор гүйх
46 node* search(node ** tree, int val) {
47 if(!(*tree)) {
48 return NULL;
49 }
50 if(val == (*tree)->data) {
51 return *tree;
52 } else if(val < (*tree)->data) {
53 search(&((*tree)->left), val);
54 } else if(val > (*tree)->data){
55 search(&((*tree)->right), val);
56 }
57 }
[Lines 47-49] Check first if tree is empty,
then return NULL.
[Lines 50-51] Check if node value to be
searched is equal to root node value,
then return node
[Lines 52-53] Check if node value to be
searched is lesser than root node value,
then call search() function recursively
with left node
[Lines 54-55] Check if node value to be
searched is greater than root node
value, then call search() function
recursively with right node
Repeat step 2, 3, 4 for each recursion
call of this search function until node to
be searched is found.
14. Хоёртын мод устгах
38 void deltree(node * tree) {
39 if (tree) {
40 deltree(tree->left);
41 deltree(tree->right);
42 free(tree);
43 }
44 }
[Line 39] Check first if root node is non-NULL, then
a. [Line 40] Call deltree() function recursively while
there is non-NULL left node
b. [Line 41] Call deltree() function recursively while
there is non-NULL right node
c. [Line 42] Delete the node.
15. Хоёртын мод дүрслэх
Binary tree can be displayed in three forms – pre-order, in-order and post-order.
Pre-order displays root node, left node and then right node.
In-order displays left node, root node and then right node.
Post-order displays left node, right node and then root node
16. Хоёртын мод дүрслэх
28 void print_preorder(node * tree) {
29 if (tree) {
30 printf("%dn",tree->data);
31 print_preorder(tree->left);
32 print_preorder(tree->right);
33 }
34 }
35 void print_inorder(node * tree) {
36 if (tree) {
37 print_inorder(tree->left);
38 printf("%dn",tree->data);
39 print_inorder(tree->right);
40 }
41 }
42 void print_postorder(node * tree) {
43 if (tree) {
44 print_postorder(tree->left);
45 print_postorder(tree->right);
46 printf("%dn",tree->data);
47 }
48 }
Pre-order display
a. [Line 30] Display value of root node.
b. [Line 31] Call print_preorder() function recursively while
there is non-NULL left node
c. [Line 32] Call print_preorder() function recursively while
there is non-NULL right node
In-order display
a. [Line 37]Call print_inorder() function recursively while
there is non-NULL left node
b. [Line38] Display value of root node.
c. [Line 39] Call print_inorder() function recursively while
there is non-NULL right node
Post-order display
a. [Line 44] Call print_postorder() function recursively while
there is non-NULL left node
b. [Line 45] Call print_postorder() function recursively while
there is non-NULL right node
c. [Line46] Display value of root node.