EENG212 Lecture13 2023
EENG212 Lecture13 2023
▪ Each element of a binary tree is called a node of the tree. The following figure shows
a binary tree with 9 nodes where A is the root.
EENG212: ALGORITHMS AND DATA STRUCTURES
▪ The depth of a binary tree is the maximum level of any leaf in the tree.
▪ A complete binary tree of depth d is the strictly binary all of whose leaves are at level d. A
complete binary tree with depth d has 2d leaves and 2d-1 nonleaf nodes.
EENG212: ALGORITHMS AND DATA STRUCTURES
struct node{
int info; /* can be of different type*/
struct node *left;
struct node *right;
struct node *father; /*optional to use */
};
typedef struct node nodeptr;
EENG212: ALGORITHMS AND DATA STRUCTURES
▪ The maketree function allocates a node and sets it as the root of a single node binary
tree.
nodeptr *maketree(int x)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr)); //getnode();
p->info = x;
p->left = NULL;
p->right = NULL;
return p;
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
EENG212: ALGORITHMS AND DATA STRUCTURES
▪ The setleft and setright functions sets a node with content x as the left son and right
son of the node p respectively.
▪ In the following traversal methods, the tree is traversed always in downward directions.
Therefore the father field is not needed.
▪ The following recursive preorder traversal function displays the info part of the nodes in
preorder. Note that the info part is integer number and tree is a pointer to the root of the
tree.
▪ The following recursive inorder traversal function displays the info part of the nodes in
inorder. Note that the info part is integer number and tree is a pointer to the root of the
tree.
▪ The following recursive postorder traversal function displays the info part of the nodes in
postorder. Note that the info part is integer number and tree is a pointer to the root of the
tree.
BINARY TREES
• Binary Search Tree: An Application of Binary Trees
▪ A binary tree, that has the property that all elements in the left subtree of a node n are less
than the contents of n, and all elements in the right subtree of n are greater than or equal
to the contents of n, is called a Binary Search Tree or Ordered Binary Tree.
▪ Given the following sequence of numbers,
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
The following binary search tree can be constructed.
EENG212: ALGORITHMS AND DATA STRUCTURES
BINARY TREES
• Binary Search Tree: An Application of Binary Trees
▪ The inorder (left-root-right) traversal of the above Binary Search Tree and printing the info
part of the nodes gives the sorted sequence in ascending order. Therefore, the Binary search
tree approach can easily be used to sort a given array of numbers.
▪ The inorder traversal on the following Binary Search Tree is:
BINARY TREES
• Searching through the Binary Search Tree
▪ Searching operation of the binary search tree is always in downward direction. Therefore,
the following node structure can be used to represent the node of a given binary search
tree.
struct node{
int info; /* can be of different type*/
struct node *left;
struct node *right;
};
typedef struct node nodeptr;
EENG212: ALGORITHMS AND DATA STRUCTURES
BINARY TREES
• Searching through the Binary Search Tree
▪ The following recursive function can be used to search for a given key element in a given
array of integers. The array elements are stored in a binary search tree. Note that the
function returns TRUE (1) if the searched key is a member of the array and FALSE (0) if the
searched key is not a member of the array.
BINARY TREES
• Application of Binary Search Tree
▪ The following recursive function can be used to insert a new node into a given binary search
tree.
BINARY TREES
• Application of Binary Search Tree
Ex: Given the following array of integer values create a binary search tree.
9,5,1,3,8,4,6,2,0,7,5,4,3,2,1
#include <stdio.h>
#include <stdlib.h> printf("\nOriginal List:\n");
struct node{ for(i=0;i<15;i++){
int info; printf("%d ", x[i]);
struct node *left; }
struct node *right; for(i=0;i<15;i++)
}; ptree=insert(ptree, x[i]);
typedef struct node nodeptr; printf("\n\nThe Tree content in
nodeptr *insert(nodeptr *, int); PreOrder:\n");
void pretrav(nodeptr *); pretrav(ptree);
void intrav(nodeptr *); printf("\nThe Tree content in
void posttrav(nodeptr *); InOrder(***SORTED***):\n");
int main() { intrav(ptree);
nodeptr *ptree; printf("\nThe Tree content in
int x[15]={9,5,1,3,8,4,6,2,0,7,5, PostOrder:\n");
4,3,2,1},i; posttrav(ptree);
return 0;
}
EENG212: ALGORITHMS AND DATA STRUCTURES
BINARY TREES
• Application of Binary Search Tree
Ex: Given the following array of integer values create a binary search tree.
9,5,1,3,8,4,6,2,0,7,5,4,3,2,1
BINARY TREES
• Application of Binary Search Tree
Ex: Given the following array of integer values create a binary search tree.
9,5,1,3,8,4,6,2,0,7,5,4,3,2,1