0% found this document useful (0 votes)
7 views

Unit 4 Tree

The document provides an overview of tree data structures, including definitions of depth, height, node degree, and tree degree. It explains binary trees, their types, and traversal methods such as in-order, pre-order, and post-order. Additionally, it covers binary search trees and their operations, including insertion and deletion.

Uploaded by

molej65141
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)
7 views

Unit 4 Tree

The document provides an overview of tree data structures, including definitions of depth, height, node degree, and tree degree. It explains binary trees, their types, and traversal methods such as in-order, pre-order, and post-order. Additionally, it covers binary search trees and their operations, including insertion and deletion.

Uploaded by

molej65141
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/ 63

Tree

Depth and Height

Depth: The depth of a node in the tree is the length of the path from the root to that node.

Height : Height of the tree is equal to the depth of the deepest leaf.
Height of tree is maximum level of any node in a tree

root->child[i]->ch_count
Node Degree

? Node degr ee: The number of children of a node


Tree Degree

? Tree Degr ee: determined by the node with the highest degree

Tree Degree = 3
Binary Tree

? A binary tree is a tree in which no node can have mor e


than two subtr ees.
? A node can have 0,1 or 2 subtrees

? These subtrees are designated as left and right subtrees of the


binary tree
A
L eft Subtree Right Subtree

B
C

D E
F
Binary Tree
Binary Tr ee- ordered tree with all nodes having at the most 2 children
R ecursive Definition:
A binary tree is either
1. a empty tree or
2. consists of a node , called a root and two children, left and right, each of which are themselves

O rder ed Tree is one in which the children of each node are ordered
Sample Binary Trees

A A A

B B

A
C

B C
Difference Between a Tree & a Binary Tree

A binary tree may be empty; a tree cannot be empty.

No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a
tree.

The subtrees of abinary tree are ordered; those of a tree are not ordered
Binary Tree for Expressions
Types of Binary Trees
? Full Binary Tree
? Strictly Binary Tree
? Complete Binary Tree
? Almost Complete Binary Tree
? Skewed Binary Tree
Full Binary Tree
• A full binary tree of height has exactly -1 nodes. (root at level 1)

• Numbering the nodes in a full binary tree


1. Number the nodes 1 through -1
2. Number by levels from top to bottom
3. Within a level, number from left to right
Node Number Property of Full Binary Tree
• Parent of node isnode (i/2) , unlessi = 1
• Right child of node is node +1,
• Left child of node is node ,
• If , node has no right child and if , node has no left child , where is the total number of nodes.
Strictly Binary Tree
? If every non leaf node in binary tree has non empty left and right subtree, the tree is termed as Strictly Binary
Tree

A A

B C B C

? Strictly Binary Tree Not Strictly Binary Tree


D E F
D E

G H I
F G
Complete Binary Tree
? A complete binary tree of depth d is the strictly binary tree whose all leaves are at level d.

B
C
? Since all leaf nodes in such tree are at level d, the tree contains 2d leaf nodes and 2 d -1 non leaf
nodes
D E F
G
Almost complete binary tree
• A binary tree of depth d is an almost complete binary tree if:
1. Any node nd at level than d-1 has two children
2. For any node nd in the tree with right descendant at level d, nd must have left child and every left
descendant of nd is either a leaf at level d or has two children.
Skewed Binar y Tree-
A skewed binary tree is a binary tree that satisfies the following 2 properties-

• All the nodes except one node has one and only one child.
• The remaining node has no child.
Or

A skewed binary tree is a binary tree of n nodes such that its


depth is (n-1).
Tree Representation

? Using Array

? Using Linked List


Tree Representation: Using Array

? In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
? To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n + 1.
Tree Representation: Using Array
Tree Representation: Using Linked List

? We use a double linked list to represent a binary tree.


? In a double linked list, every node consists of three fields.
? First field for storing left child address, second for storing actual data and third for storing right child
address.
Tree Representation: Using Linked List
Binary Tree Traversals

? To display a binary tree, we need to follow some order in which all the nodes of that binary tree must be displayed.

? In any binary tree, displaying order of nodes depends on the traversal method.

? Depth First Traversal : process all descendents of a child before going on to the next child.

In - Order Traversal
Pre - Order Traversal
Post - Order Traversal

? Breadth First Traversal : each level is completely processed before the next level is started.

Level order
Binary Tree Traversals

? In - Order Traversal : The root node is visited between the left child and right child.

? In this traversal, the left child node is visited first, then the root node is visited and later we go for visiting
the right child node.

? This in-order traversal is applicable for every root node of all subtrees in the tree.

? This is performed recursively for all nodes in the tree.


In - Order Traversal

Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Recursive Inorder Traversal

Algorithm inorder( root)


If(root is not null)
1. inorder(Root ->LeftSubtree);
2. print (root)
3. inorder(Root->RightSubtree);
End if
Non Recursive Inorder traversal

1) Create an empty stack S.

2) Initialize current node as root

3) Push the current node to S and


set current = current->left until current is NULL

4) If current is NULL and stack is not empty then

a) Pop the top item from stack.

b) Print the popped item, set current = popped_item->right

c) Go to step 3.

5) If current is NULL and stack is empty then we are done.


Example

Root/Current 10

8 11

Stack

7 9
Example

Push 10 onto stack and


10
move onto left branch

current = current->left
Root/Current
8 11

7 9
10
Example

Push 8 onto stack and move


10
onto left branch

current = current->left

8 11

oot/Current
8
7 9
10
Example

Push 7 onto stack and move


10
onto left branch

current = current->left

8 11

8
7 9
10

Root/Current = NULL
Example As current==NULL we will pop stack and
print the element.
Then move
10 current = current->right

Output: 7
8 11

7 9

8
Root/Current = NULL
10
Example As new current is also equals to NULL ,
we will again pop stack and print the
element.

10 Now new current= popped elements right


child
i.e. 9
Output: 7 ,8
8 11

7 9

Root/Current = NULL
10
Example As new current is also equals to NULL ,
we will again pop stack and print the
element.

10 Now new current= popped elements right


child
i.e. 9
Output: 7 ,8
8 11

7 9

Root/Current = 9

10
Example Push current onto stack

And current=current->left

10

Output: 7 ,8
8 11

7 9

Root/Current = NULL 9

10
Example As current== Null

Pop stack

10 And current=current->right

Output: 7 ,8,9
8 11

7 9

Root/Current = NULL 9

10
Example As current== Null

Pop stack

10 And current=current->right

Output: 7 ,8,9
8 11

7 9

Root/Current = NULL

10
Example As current== Null

Pop stack

10 current=popped elemets right child

i.e. 11
Output: 7 ,8,9,10
8 11

7 9

Root/Current = NULL
Example As current== Null

Pop stack

10 current=popped elements right child

i.e. 11
Output: 7 ,8,9,10
8 11

7 9 Root/Current=NULL
Example Push current on stack.

current=current->left child

10

Output: 7 ,8,9,10
8 11
Root/Current

7 9

11
Example As current== Null

Pop stack

10

Output: 7 ,8,9,10,11
8 11

7 9
Root/Current=NULL
Binary Tree Traversals

? Pre - Order Traversal : The root node is visited before the left child and right child.

? In this traversal, the root node is visited first, then the left node is visited and later we go for visiting the
right child node.

? This pre-order traversal is applicable for every root node of all subtrees in the tree.

? This is performed recursively for all nodes in the tree.


Pre- Order Traversal

Algorithm Preorder(tree)
1. Visit the root.
2.Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Recursive Preorder Traversal

Algorithm preorder( root)


If(root is not null)
1. print (root)
2. preorder(Root ->LeftSubtree);
3. preorder(Root->RightSubtree);
End if
Non Recursive Preorder Traversal
1) Create an empty stack and push root node to stack.
2) Do following while is not empty
….a) Pop an item from stack and print it.
….b) Push right child of popped item to stack
….c) Push left child of popped item to stack
Right child is pushed before left child to make sure that left subtree is
processed first.
Binary Tree Traversals

? Post - Order Traversal : The root node is visited after the left and right child node..

? In this traversal, the left child node is visited first, then the right child node is visited and later we go for
visiting the root node.

? This post-order traversal is applicable for every root node of all subtrees in the tree.

? This is performed recursively for all nodes in the tree.


Post- Order Traversal

Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
Recursive Postorder Traversal

Algorithm postorder( root)


If(root is not null)
1. postorder(Root ->LeftSubtree);
2. postorder(Root->RightSubtree);
3. print (root)

End if
Non Recursive Postorder Traversal
1.1 Create an empty stack
2.1 Do while root is not NULL
a)if root->right!=Null
Push root's right child.
b) then push root to stack
c) Set root as root's left child.
2.2. Pop an item from stack and set it as root
while root != NULL
a) If the popped item has a right child and
if the right child is at top of stack,
then remove the right child from stack,
push the root back and set root as root's right child.
else push the root to stack and set root to right
b) Else print root's data and set root as NULL.
Pop an item from stack and set it as root
2.3 Repeat steps 2.1 and 2.2 while stack is not empty.
1.1 Non Recursive Postorder Traversal
Create an empty stack
2.1 Repeat steps 2.2 and 2.3 while stack is not empty.
2.2 Do while root is not NULL
a)if root->right!=Null
Push root's right child.
b) then push root to stack
c) Set root as root's left child.
2.3 Pop an item from stack and set it as root
Print root
while root != NULL
a) if (node->left && node->right ==null)
pop
If the popped item has a right child and if the right child is at top of
stack,
then remove the right child from stack,
push the root back and set root as root's right child.
else push the root to stack and set root to right
Break;
Print inorder, Preorder and Postorder traversal for given tree.

ghdiebJfca
Binary Tree Construction

• Inoder: B C A E D G H F I
• Preoder: A B C D E F G H I

B,C D, E, F, G, H, I

B D, E, F, G, H, I

C
Binary Tree Construction

• Inoder: B C A E D G H F I
• Preoder: A B C D E F G H I

B D,

C E F, G, H, I
Binary Tree Construction

• Inoder: B C A E D G H F I
• Preoder: A B C D E F G H I

B D,

C E F

I
G,H
Binary Tree Construction

• Infix: B C A E D G H F I
• Prefix: A B C D E F G H I

B D,

C E F

I
G

H
Binary Search Tree

? Definition: A binary tree where every node's left sub-tree has values less than the node's value, and every right
sub-tree has values greater than the node's value

? Binary search tree is a Binary Tree with following Properties:


All items in the left subtrees are less than the root.
All items in the right subtrees are greater than root
Each subtreee is itself a BST.
Binary Search Tree

Key

All < Key Key < All


Binary Search Trees (BST) Operations

? Insertion

? Deletion

? Traversals

? Search
BST Operations - Insertion

? Case 1: The Tree is Empty


Set the root to a new node containing the item

? Case 2: The Tree is Not Empty


Call a recursive helper method to insert the item
BST Operations - Insertion

? Algorithm:
1. Read the value for which node to be created, and store it in node called New.

1. If(root== NULL) then(root=New)

1. If( New->data < root-> data)

1. Attach New node as left child Otherwise attach New node


as a right child.
2. Repeat step 3 for constructing binary search tree completely.
BST Operations - Insertion

? Void Binarytree::insert(node *root, node *New)


{
if(root== NULL)
root=New;
if(New->data <= root->data)
{
if(root->left ==NULL)
root->left=New
else
insert(root->left, New);
}
if(root->data < New->data)
{
if(root->right ==NULL)
root->right=New
else
insert(root->right, New);
}
}
BST Operations - Search

Algorithm SearchBST (root, target)

1. If (empty tree) /*Not found target


return(null)
2. end if
3. If ( target < root)
return SearchBST (left subtree, target)
4. else if ( target > root)
return SearchBST (right subtree, target)
5. else
return root
6. end if

end SearchBST
BST Operations : Deletion

? First locate the node which is to be deleted.

? The node to be deleted has


1. No children
2. Only a right subtree
3. Only left subtree
4. Two subtrees

You might also like