0% found this document useful (0 votes)
4 views21 pages

EENG212 Lecture13 2023

The document provides an overview of binary trees, including definitions, properties, and traversal methods. It discusses binary search trees as a specific application of binary trees, detailing their structure and operations such as insertion and searching. Additionally, it includes code snippets for implementing these concepts in programming.

Uploaded by

Zeynep Bayram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

EENG212 Lecture13 2023

The document provides an overview of binary trees, including definitions, properties, and traversal methods. It discusses binary search trees as a specific application of binary trees, detailing their structure and operations such as insertion and searching. Additionally, it includes code snippets for implementing these concepts in programming.

Uploaded by

Zeynep Bayram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

EENG212: ALGORITHMS AND DATA STRUCTURES

Fall 2023/24 – Lecture Notes # 13


• Introduction to Trees
▪ Binary Trees: Basic Definition
▪ Traversing Binary Trees
▪ Node Representation of Binary Trees
▪ Primitive Functions in Binary Trees
EENG212: ALGORITHMS AND DATA STRUCTURES

• Binary Tree BINARY TREES


▪ A binary tree is a finite set of elements that are either empty or is partitioned into three
disjoint subsets. The first subset contains a single element called the root of the tree.
The other two subsets are themselves binary trees called the left and right subtrees
of the original tree. A left of the right subtree can be empty.

▪ 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

• Binary Tree BINARY TREES


▪ If A is the root of a binary tree and B is the root of its left subtree, then A is said to be
the father of B and B is said to be the left son of A. Similarly, A is the father of C and C
is the right subtree of A.
▪ A node that has no sons is called the leaf.
▪ Node n1 is the ancestor of node n2 if n1 is either the father of n2 or the father of some
ancestor of n2. In such a case n2 is a descendant of n1.
▪ Two nodes are brothers if they are left and right sons of the same father.
EENG212: ALGORITHMS AND DATA STRUCTURES

• Binary Tree BINARY TREES


▪ If every nonleaf node in a binary tree has nonempty left and right subtrees, the tree is
called a strictly binary tree.
EENG212: ALGORITHMS AND DATA STRUCTURES

• Binary Tree BINARY TREES


▪ The level of a node in a binary tree is defined as follows: The root of the tree has level 0, and
the level of any other node in the tree is one more than the level of its father.

▪ 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

• Traversing Binary Trees BINARY TREES


▪ One of the common operations of a binary tree is to traverse the tree. Traversing a tree is to
pass through all of its nodes once. You may want to print the contents of each node or to
process the contents of the nodes. In either case each node of the tree is visited.
▪ There are three main traversal methods where traversing a binary tree involves visiting the
root and traversing its left and right subtrees. The only difference among these three
methods is the order in which these three operations are performed.

▪ Traversing a binary tree in preorder ( depth-first order)


1. Visit the root.
2. Traverse the left subtree in preorder.
3. Traverse the right subtree in preorder.

▪ Traversing a binary tree in inorder (symmetric order)


1. Traverse the left subtree in inorder.
2. Visit the root.
3. Traverse the right subtree in inorder.

▪ Traversing a binary tree in postorder (breadth-first order)


1. Traverse the left subtree in postorder.
2. Traverse the right subtree in postorder.
3. Visit the root.
EENG212: ALGORITHMS AND DATA STRUCTURES

• Traversing Binary Trees BINARY TREES


▪ preorder traversal ( depth-first order)
root - left subtree - right subtree
▪ inorder traversal (symmetric order)
left subtree - root - right subtree
▪ postorder traversal (breadth-first order)
left subtree - right subtree - root
EENG212: ALGORITHMS AND DATA STRUCTURES

• Node Representation of Binary Trees BINARY TREES


▪ Each node in a binary tree contains info, left, right and father fields. The left, right and father
fields points the node’s left son, right son and the father respectively.

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

• Primitive Functions in Binary Trees BINARY TREES

▪ 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

• Primitive Functions in Binary Trees BINARY TREES

▪ The setleft and setright functions sets a node with content x as the left son and right
son of the node p respectively.

void setleft(nodeptr *p, int x) void setright(nodeptr *p, int x)


{ {
if(p == NULL){ if(p == NULL){
printf(“void insertion\n”); printf(“void insertion\n”);
else if (p->left != NULL) else if (p->right != NULL)
printf(“invalid insertion\n”); printf(“invalid insertion\n”);
else else
p->left = maketree(x); p->right = maketree(x);
} }
EENG212: ALGORITHMS AND DATA STRUCTURES

• Binary Tree Traversal Methods BINARY TREES


▪ Recursive functions can be used to perform traversal on a given binary tree. Assume that
dynamic node representation is used for a given binary tree.

▪ 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.

void pretrav(nodeptr *tree)


{
if(tree != NULL){
printf(“%d\n”, tree->info);
pretrav(tree->left);
pretrav(tree->rigth);
}
}
EENG212: ALGORITHMS AND DATA STRUCTURES

• Binary Tree Traversal Methods BINARY TREES

▪ 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.

void intrav(nodeptr *tree)


{
if(tree != NULL){
intrav(tree->left);
printf(“%d\n”, tree->info);
intrav(tree->rigth);
}
}
EENG212: ALGORITHMS AND DATA STRUCTURES

• Binary Tree Traversal Methods BINARY TREES

▪ 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.

void posttrav(nodeptr *tree)


{
if(tree != NULL){
posttrav(tree->left);
posttrav(tree->rigth);
printf(“%d\n”, tree->info);
}
}
EENG212: ALGORITHMS AND DATA STRUCTURES

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:

3, 4, 4, 5, 5, 7, 9, 9, 14, 14, 15, 16, 17, 18, 20


EENG212: ALGORITHMS AND DATA STRUCTURES

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.

▪ Note that the father link is not required.

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.

int BinSearch(nodeptr *p, int key)


{
if(p == NULL)
return FALSE;
else {
if (key == p->info)
return TRUE;
else{
if(key < p->info)
return BinSearch(p->left, key);
else
return BinSearch(p->right, key);
}
}
}
EENG212: ALGORITHMS AND DATA STRUCTURES

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.

nodeptr *insert(nodeptr *p, int x)


{
if(p == NULL){
p = (nodeptr *) malloc(sizeof(nodeptr)); //getnode();
p->info = x;
p->left = NULL;
p->right = NULL;
return p;
}
else{
if(x < p->info)
p->left = insert(p->left, x);
else
p->right = insert(p->right, x);
return p; nodeptr *getnode(void)
} {
nodeptr *p;
} p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
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
#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

nodeptr *insert(nodeptr *p, int x) void pretrav(nodeptr *tree)


{ {
if(p == NULL){ if(tree != NULL){
p = (nodeptr *) malloc(sizeof(nodeptr)); posttrav(tree->left);
p->info = x; posttrav(tree->right);
p->left = NULL; printf(“%d ”, tree->info);
p->right = NULL; }
return p; }
}
else{ void intrav(nodeptr *tree)
if(x < p->info) {
p->left = insert(p->left, x); if(tree != NULL){
else intrav(tree->left);
p->right = insert(p->right, x); printf("%d ", tree->info);
return p; intrav(tree->right);
} }
} }
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

void posttrav(nodeptr *tree)


{
if(tree != NULL){
posttrav(tree->left);
posttrav(tree->right);
printf("%d ", tree->info);
}
Program Output
}

Sorted in ascending order

You might also like