0% found this document useful (0 votes)
10 views58 pages

BinaryTree BST

Engineering notes

Uploaded by

ijantkartanishka
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)
10 views58 pages

BinaryTree BST

Engineering notes

Uploaded by

ijantkartanishka
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/ 58

Trees

Objectives
Upon completion you will be able to

• Explain Basic Tree concepts.


• Implement Binary search tree (BST)

Data Structures: A Pseudocode Approach with C 1


Data Structures: A Pseudocode Approach with C 2
In a non-linear list, each element can
have more than one successor.

In a tree, an element can have only one


predecessor.

In a graph, an element can have one or


more predecessors.

Data Structures: A Pseudocode Approach with C 3


A Tree consists of a finite set of elements, called
nodes, and a finite set of directed lines, called
branches, that connect the nodes.

The number of branches associated with a node is


the degree of the node.

When the branch is directed toward the node, it is an


indegree branch

When the branch is directed away from the node, it is


an outdegree branch.

The sum of the indegree and outdegree branches is


the degree of the node.

Data Structures: A Pseudocode Approach with C 4


If the tree is not empty, the first node is called
the root. The indegree of the root is zero.

Data Structures: A Pseudocode Approach with C 5


A leaf is any node with an outdegree of zero, that is, a node with no successors.

A node that is not a root or a leaf is known as an internal node.

A node is a parent if it has successor nodes—that is, if it has an outdegree greater than
zero.

A node with a predecessor is a child. A child node has an indegree of one.

Two or more nodes with the same parent are siblings.

An ancestor is any node in the path from the root to the node.

A descendant is any node in the path below the parent node; that is, all nodes in the
paths from a given node to a leaf are descendants of that node.

A path is a sequence of nodes in which each node is adjacent to the next one. Every node
in the tree can be reached by following a unique path starting from the root.

The level of a node is its distance from the root. Because the root has a zero distance
from itself, the root is at level 0.
Data Structures: A Pseudocode Approach with C 6
Data Structures: A Pseudocode Approach with C 7
The height of the tree is the level of the leaf in the longest path from the root plus 1.

By definition the height of an empty tree is -1.

A tree may be divided into subtrees. A subtree is any connected structure below the
root. The first node in a subtree is known as the root of the subtree and is used to name
the subtree.

Subtrees can also be further subdivided into subtrees.

Data Structures: A Pseudocode Approach with C 8


A Binary Tree is a tree in which no node can have
more than two subtrees; the maximum outdegree for a
node is two. In other words, a node can have zero,
one, or two subtrees. These subtrees are designated
as the left subtree and the right subtree.

Data Structures: A Pseudocode Approach with C 9


Null Tree is a tree with no nodes

Data Structures: A Pseudocode Approach with C 10


Properties of Binary
Trees
Height of Binary Trees

Maximum Height
Given that we need to store N nodes in a binary tree, the maximum height,
Hmax, is

Minimum
Height

Data Structures: A Pseudocode Approach with C 11


Minimum Number of Nodes

Maximum Number of Nodes

The balance factor of a binary tree is the difference in


height between its left and right subtrees.

In a balanced binary tree, the height of its subtrees


differs by no more than one (its balance factor is –1, 0, or
+1), and its subtrees are also balanced.

Data Structures: A Pseudocode Approach with C 12


A Complete Tree has the maximum number of entries for its height (see formula
Nmax in “Height of Binary Trees”). The maximum number is reached when the last
level is full. A tree is considered Nearly Complete if it has the
minimum height for its nodes (see formula Hmin) and all nodes in the last level are
found on the left.

Data Structures: A Pseudocode Approach with C 13


Binary Tree Traversals

A binary tree traversal requires that each node of the tree be processed once
and only once in a predetermined sequence. The two general approaches to the
traversal sequence are depth first and breadth first.

In the depth-first traversal, the processing proceeds along a path from the root
through one child to the most distant descendent of that first child before processing a
second child. In other words, in the depth-first traversal, we process all of the
descendants of a child before going on to the next child.

In a breadth-first traversal, the processing proceeds horizontally from the root to


all of its children, then to its children’s children, and so forth until all nodes have been
processed. In other words, in the breadth-first traversal, each level is completely
processed before the next level is started.

Data Structures: A Pseudocode Approach with C 14


Depth-first Traversals
Given that a binary tree consists of a root, a left subtree,
and a right subtree, there are different depth-first traversal
sequences.

Data Structures: A Pseudocode Approach with C 15


Data Structures: A Pseudocode Approach with C 16
Preorder Traversal (NLR)
In the preorder traversal, the root node is processed first,
followed by the left subtree and then the right subtree. It
draws its name from the Latin prefix pre, which means to go
before. Thus, the root goes before the subtrees.

Data Structures: A Pseudocode Approach with C 17


Data Structures: A Pseudocode Approach with C 18
Inorder Traversal (LNR)
The inorder traversal processes the left subtree first,
then the root, and finally the right subtree. The meaning of
the prefix in is that the root is processed in between the
subtrees.

Data Structures: A Pseudocode Approach with C 20


Data Structures: A Pseudocode Approach with C 21
struct node {
int data;
struct node* left;
struct node* right;
};

struct node* newNode(int data)


{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return (node);
}

/* Given a binary tree, print its nodes in inorder*/


void printInorder(struct node* node)
{
if (node == NULL)
return;

/* first recur on left child */


printInorder(node->left);

/* then print the data of node */


printf("%d ", node->data);

/* now recur on right child */


printInorder(node->right);
}

Data Structures: A Pseudocode Approach with C 22


void inOrder(struct node* r)
{
if(r!=NULL){
inOrder(r->left);
printf("%d ", r->data);
inOrder(r->right);
}
}

Data Structures: A Pseudocode Approach with C 23


void preOrder(struct node* r)
{
if(r!=NULL){
printf("%d ", r->data);
preOrder(r->left);
preOrder(r->right);
}
}
void postOrder(struct node* r)
{
if(r!=NULL){
postOrder(r->left);
postOrder(r->right);
printf("%d ", r->data);

}
}
Data Structures: A Pseudocode Approach with C 24
Postorder Traversal (LRN)
The last of the standard traversals is the postorder
traversal. It processes the root node after (post) the left and
right subtrees have been processed. It starts by locating the
far-left leaf and processing it. It then processes its right
sibling,
including its subtrees (if any). Finally, it processes the root
node.

Data Structures: A Pseudocode Approach with C 25


Data Structures: A Pseudocode Approach with C 26
Data Structures: A Pseudocode Approach with C 27
Data Structures: A Pseudocode Approach with C 28
Breadth-first Traversals(Level order traversal)
In the breadth-first traversal of a binary tree, we process all of the children of a
node before proceeding with the next level. In other words, given a root at level n,
we process all nodes at level n before proceeding with the nodes at level n + 1. To
traverse a tree in depth-first order, we used a stack. (Remember that recursion uses a
stack.) To traverse a tree in breadth-first order, we use a queue.

Data Structures: A Pseudocode Approach with C 29


Data Structures: A Pseudocode Approach with C 30
struct node {
int data;
struct node* left;
struct node* right;
};

/* frunction prototypes */
struct node** createQueue(int*, int*);
void enQueue(struct node**, int*, struct node*);
struct node* deQueue(struct node**, int*);

Data Structures: A Pseudocode Approach with C 31


/* Given a binary tree, print its nodes in level order
using array for implementing queue */
void printLevelOrder(struct node* root)
{
int rear, front;
struct node** queue = createQueue(&front, &rear);
struct node* temp_node = root;

while (temp_node) {
printf("%d ", temp_node->data);

/*Enqueue left child */


if (temp_node->left)
enQueue(queue, &rear, temp_node->left);

/*Enqueue right child */


if (temp_node->right)
enQueue(queue, &rear, temp_node->right);

/*Dequeue node and make it temp_node*/


temp_node = deQueue(queue, &front);
}
}

Data Structures: A Pseudocode Approach with C 32


Data Structures: A Pseudocode Approach with C 33
Data Structures: A Pseudocode Approach with C 34
Construct Binary Tree from given Inorder and Preorder traversals
Let us consider the below traversals:
Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
In a Preorder sequence, leftmost element is the root of the tree. So we know ‘A’
is root for given sequences. By searching ‘A’ in Inorder sequence, we can find
out all elements on left side of ‘A’ are in left subtree and elements on right are
in right subtree. So we know below structure now.

35
Construct Binary Tree from given Inorder and Preorder
traversals
Let us consider the below traversals:
Preorder sequence: A B D E F C G H J L K
Inorder sequence: D B E F A G C L J H K

Construct Binary Tree from given Inorder and Preorder


traversals
Let us consider the below traversals:
Preorder sequence: 10,5,2,6,14,12,15
Inorder sequence: 2,5,6,10,12,14,15

Data Structures: A Pseudocode Approach with C 36


BINARY SEARCH TREE (BST)

Data Structures: A Pseudocode Approach with C 37


In the design of the linear list structure, we had two choices:
an array or a linked list.

The array structure provides a very efficient search


algorithm but its insertion and deletion algorithms are very
inefficient.

On the other hand, the linked list structure provides efficient


insertion and deletion, but its search algorithm is very
inefficient.

What we need is a structure that provides an efficient


search algorithm and at the same time efficient insert and
delete algorithms.

The binary search tree and the AVL tree provide that
structure
Data Structures: A Pseudocode Approach with C 38
BINARY SEARCH TREE (BST)

A binary search tree (BST) is a binary tree with the


following properties:
• All items in the left subtree are less than the root.
• All items in the right subtree are greater than or equal to
the root.
• Each subtree is itself a binary search tree.

Data Structures: A Pseudocode Approach with C 39


Data Structures: A Pseudocode Approach with C 40
Data Structures: A Pseudocode Approach with C 41
Data Structures: A Pseudocode Approach with C 42
BST Operations

Traversals

Searches
• Find the Smallest Node
• Find the Largest Node
• BST Search

Insertion

Deletion

Construct a binary search tree given :


23, 18, 12, 20, 44, 35, 52
Data Structures: A Pseudocode Approach with C 43
Preorder BST Traversal

Postorder BST Traversal

Inorder BST Traversal

Data Structures: A Pseudocode Approach with C 44


Data Structures: A Pseudocode Approach with C 45
Data Structures: A Pseudocode Approach with C 46
Data Structures: A Pseudocode Approach with C 47
Data Structures: A Pseudocode Approach with C 48
Data Structures: A Pseudocode Approach with C 49
Data Structures: A Pseudocode Approach with C 50
Data Structures: A Pseudocode Approach with C 51
Data Structures: A Pseudocode Approach with C 52
Data Structures: A Pseudocode Approach with C 53
Data Structures: A Pseudocode Approach with C 54
Data Structures: A Pseudocode Approach with C 55
Data Structures: A Pseudocode Approach with C 56
Data Structures: A Pseudocode Approach with C 57
Integer Application

The BST tree integer application reads integers from


the keyboard and inserts them into the BST.

Data Structures: A Pseudocode Approach with C 58

You might also like