Note 5: Tree Concept in Data Structure For Application
Note 5: Tree Concept in Data Structure For Application
Note 5: Tree Concept in Data Structure For Application
32
right child
33
Binary Tree
Definition: A binary tree is a finite set of nodes that is either empty or consists of a root
and two disjoint binary trees called the left subtree and the right subtree.
Binary Tree Types:
Regular Binary Tree ( 2 )
Skewed Left Binary Tree ( 1 )
Skewed Right Binary Tree ( 3 )
Three Graphical Pictures of the Binary Tree:
A
B
B
(1)
Skewed Left
Binary Tree
(2)
Regular Binary Tree
(3)
Skewed Right
Binary Tree
34
data
right_child
left_child
right_child
35
36
37
if (ptr->left_child)
addq (front, &rear, ptr->left_child);
if (ptr->right_child)
addq (front, &rear, ptr->right_child);
}
else
Break;
}
}
Result of binary tree example:
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O
Binary Search Tree
Definition: A binary search tree is a binary tree. It may be empty. If it is not empty, it
satisfies the following properties:
(1) Every element has a key, and no two elements have the same key, that is, the keys
are unique.
(2) The keys in a nonempty left subtree must be smaller than the key in the root of the
subtree.
(3) The keys in a nonempty right subtree must be larger than the key in the root of the
subtree.
(4) The left and right subtrees are also binary search trees.
Example of the Binary Search Tree:
20
15
12
25
17
22
30
38
the right subtree subtree can have a key value equal to key. Therefore, we search the left
subtree of root. If key is larger than roots key value, we search the right subtree of root.
Recursive Function for Binary Search Tree:
tree_ptr search ( tree_ptr root, int key )
{
if ( !=root )
return NULL;
if ( key = = root->data )
return root;
if ( key < root->data )
return search ( root->left_child, key );
return search ( root->right_child, key );
}
Inserting Into A Binary Search Tree
To insert a new element, key, we must first verify that the key is different from those of
existing elements. To do this we search the tree. If the search is unsuccessful, then we
insert the element at the point the search terminated.
Insert Function:
void insert_node ( tree_ptr *node, int num )
{
tree_ptr ptr, temp = modified_search ( *node, num ); // **
if ( temp || ! ( *node ) )
{
ptr = new node;
if ( ptr = = NULL)
{
cout << The memory is full \n;
exit ( 1 );
}
ptr->data = num;
ptr->left_child = ptr->right_child = NULL;
if ( *node )
{
if ( num < temp->data )
temp->left_child = ptr;
else
temp->right_child = ptr;
}
else
*node = ptr;
}
}
** The function modified_search that is slightly modified version of function search.
This function searches the binary search tree *node for the key num. If the tree is empty
39
or if num is present, it returns NULL. Otherwise, it returns a pointer to the last node of
the tree that was encountered during the search. The new element is to be inserted as a
child of this node.
Deletion from a Binary Search Tree
Deletion of a leaf node is easy. For example, if a leaf node is left child, we set the
left child field of its parent to NULL and free the node.
The deletion of a nonleaf node that has only a single child is also easy. We erase
the node and then place the single child in the place of the erased node.
When we delete a nonleaf node with two children, we replace the node with either
the largest element in its left subtree or the smallest elements in its right subtree.
Then we proceed by deleting this replacing element from the subtree from which
it was taken.
The Heap
Two type of the heap: Max Heaps and Min Heaps.
Definition
Max Heaps: A max tree is a tree in which the key value in each node is no smaller than
the key values in its children (if any). A max heap is a complete binary tree
that is also a max tree.
Min Heaps: A min tree is a tree in which the key value in each node is no larger than the
key values in its children (if any). A min heap is a complete binary tree that
is also a min tree.
Following insert and delete operation for max heap are using array representation:
Define Max Heap Structure
#define MAX_ELEMENTS 200 /* maximum heap size+1 */
#define HEAP_FULL (n) ( n = = MAX_ELEMENTS-1 )
#define HEAP_EMPTY (n) ( !n )
struct element
{
int key;
/* other fields */
}
element heap [MAX_ELEMENTS];
int n = 0;
Insertion Into A Max Heap
void insert_max_heap ( element item, int *n )
{
int i;
if ( HEAP_FULL ( *n ) )
{
cout << The heap is full! \n;
exit ( 1 );
}
i = + + ( *n );
while ( ( i != 1 ) && ( item.key > heap[ i / 2 ].key ) )
{
heap[ i ] = heap[ i / 2 ];
i / = 2;
}
heap[ i ] = item;
}
Deletion From A Max Heap
element delete_max_heap ( int *n )
{
int parent, child;
element item, temp;
if ( HEAP_EMPTY ( *n ) )
{
cout << The heap is empty! \n;
exit ( 1 );
}
//save value of the element with the highest key
item = heap[1];
//use last element in heap to adjust heap
temp = heap[ ( *n ) - - ];
parent = 1;
child = 2;
while ( child < = *n )
{
//find the larger child of the current parent
if ( child < *n ) && ( heap[ child ].key < heap[ child + 1 ].key)
child + +;
if ( temp.key >= heap[ child ].key )
break;
//move to the next lower level
heap[ parent ] = heap[ child ];
parent = child;
child * = 2;
}
heap[ parent ] = temp
return item;
}
40