0% found this document useful (0 votes)
79 views55 pages

Understanding Binary Trees Basics

The document provides a comprehensive overview of binary trees, covering their structure, properties, types, and traversal methods. It explains key terminologies such as root, node, parent, child, and various tree operations including insertion, deletion, and searching. Additionally, it discusses the construction of binary trees from traversal data and introduces heap trees, including max and min heaps.

Uploaded by

amidelo2018
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)
79 views55 pages

Understanding Binary Trees Basics

The document provides a comprehensive overview of binary trees, covering their structure, properties, types, and traversal methods. It explains key terminologies such as root, node, parent, child, and various tree operations including insertion, deletion, and searching. Additionally, it discusses the construction of binary trees from traversal data and introduces heap trees, including max and min heaps.

Uploaded by

amidelo2018
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

BINARY TREES
Outline

1. Review on stacks and Queues
2. Introduction to non-linear data structures
3. Basics of Binary Trees
4. Constructing a Binary Tree
5. Binary Tree Traversals
6. Applications
7. Wrap up
TREE: INTRODUCTION

• Non- linear data structure which simulates a hierarchy

• Grows from top to bottom ( direct or undirected)

• Thus a tree is a non-linear data structure with collection of nodes(entities) linked together to
simulate hierarchy
TREE TERMINOLOGIES

• Root − The top of the tree is called root. First element in

the tree hierarchy

• Node --- Elements of a tree

• Parent − immediate predecessor of any node

• Child − immediate successor of parent node


TREE TERMINOLOGIES

• Leaf − Nodes without any children

• Levels − The generation of a node. If the root node is at level 0,


then its next child node is at level 1, its grandchild is at level 2,
and so on.

• Level of node- No of edges from root to that node

• Path − sequence consecutive edges from source node to


destination node
TREE TERMINOLOGIES

 Ancestors- predecessors on the path from the root to that node
 Descendants- sequence nodes on the path from that node to leaf
node
 Sub-tree – of tree T contains a node of that tree T and all its
descendants
 Siblings- children of same parent
 Degree- Number of children of that node
TREE
TERMINOLOGIES

 Depth of tree- length of path from root to that node

 Height – no of edges in the longest path from that node to

leaf node
Binary tree

 Each node has at most two children i.e 0,1,2 but not
more
Tree logical representation

Memory Representation/
implementation

Implementation code

struct node

char data;

struct node *Llink;

struct node *Rlink;

};
Applications of trees

 Implement the file system

 Routing protocols

 Organizing data for binary search


Activity

 Draw a memory implementation of the binary tree
below
Properties of binary trees

 At level i maximum number of children 2(i) where i
is the level
 maximum number of nodes of height h= 2 (0) + 2 (1) +2
(2)----+2 (h) =2 (h+1) -1

 Maximum number of nodes =h+1 i.e level


0=height=0+1=1
Properties of binary tree
cont….

• A node's left child must have a value less than its parent's
value
• The node's right child must have a value greater than its
parent value.
Types of binary trees

 Full/proper/strict binary tree

 Complete binary tree

 Perfect binary tree

 Degenerate binary tree


Activity

 Learners to point out the typical differences between

the binary tree types in the previous slide


BST Operations

1. Insert − Inserts an element in a tree/create a tree.

2. Pre-order Traversal − Traverses a tree in a pre-order manner.

3. In-order Traversal − Traverses a tree in an in-order manner.

4. Post-order Traversal − Traverses a tree in a post-order


manner.

5. Search − Searches an element in a tree.


Insert Operation

• The very first insertion creates the root of the tree.

• Root pointer should be maintained

• Once its know then traversal of other nodes will be


possible

• malloc () used to dynamically allocate memory to nodes


Implementation code
struct node

{
int data;
struct node *left, *right;
};
struct node *create()
{
int x;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data (Enter -1 for no node:\n)");
scanf("%d", &x);
if(x==-1)
{
return 0;
}
newnode->data=x;
printf("Enter the leftchild of %d", x);
newnode->left=create();
printf("Enter the rightchild of %d", x);
newnode->right=create();
return newnode;

}
void main()
{
struct node *root;
root=0;
root=create();
}
Array representation of binary
tree

 Array indexing: zero based, 1-based or n-based
 Consider the following tree:

 Representation using zero based and one based indexing


will be as follows
Array representation of binary
tree

 Case 1: starting at index zero

A B C D E F G H I
0 1 2 3 4 5 6 7 8

 Finding child node or parent in the above case


 If a node is at ith index
 Left child would be at : [(2*i)+1]
 Right child would be at : [(2*i)+2]
 Parent would be at : [(i-1)/2]
Array representation of binary
tree

 Case 2: starting at index 1

A B C D E F G H I
1 2 3 4 5 6 7 8 9

 Finding child node or parent in the above case


 If a node is at ith index
 Left child would be at : [(2*i)]
 Right child would be at : [(2*i)+1]
 Parent would be at : [(i/2)]
Activity

 Draw the array representation for the following
binary trees.
Solution

 The trees in the slide above are not complete binary
trees.
 Insert empty nodes to make them complete binary
trees
 The draw their array representation
 Remember to leave the indexes with empty nodes
unoccupied
Traversal

• Traversal is a process to visit all the nodes of a tree and may print their values too

• Since all nodes are connected via edges (links) we always start from the root (head)

node

• There are three ways which we use to traverse a tree:

• In-order Traversal

• Pre-order Traversal

• Post-order Traversal
In-order traversal

 Method:
• First visit the left sub-tree
• Then the root
• Later the right sub-tree.
• Note: Every node may represent a sub-tree
• If a binary tree is traversed in-order, the output will produce
sorted key values in an ascending order.
Algorithm

 Until all nodes are traversed −

 Step 1 − Recursively traverse left sub-tree.

 Step 2 − Visit root node.

 Step 3 − Recursively traverse right sub-tree.


Example

 We start from A, and following in-order traversal, we move to its left sub-tree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of in-order
traversal of this tree will be −
D→B→E→A→F→C→G
Pre-order traversal

 Method

 The root node is visited first

 Then the left sub-tree

 Finally the right sub-tree


Algorithm

 Until all nodes are traversed −

 Step 1 − Visit root node.

 Step 2 − Recursively traverse left sub-tree.

 Step 3 − Recursively traverse right sub-tree.


Example & Explanation

• First visit A

• Then move to its left sub-tree B.

• Traverse B in pre-order.

• Continue until all the nodes are visited.

• The output of pre-order traversal of this tree will be −

A→B→D→E→C→F→G
Post-order traversal

 Method

• The root node is visited last, hence the name.

• First we traverse the left sub-tree,

• Then the right sub-tree

• Finally the root node.


Algorithm

 Until all nodes are traversed −

 Step 1 − Recursively traverse left sub-tree.

 Step 2 − Recursively traverse right sub-tree.

 Step 3 − Visit root node.


Example & Explanation

 Start from A

 Visit the left sub-tree B.

 Traverse B in traversed post-order.

 Continue until all the nodes are visited.

 The output of post-order traversal of this tree will be −

D→E→B→F→G→C→A
Constructing binary tree from
given in-order and post-order
traversals

 Consider the following in-order and post-order traversals
in-order= {4, 8, 2, 5, 1, 6, 3, 7}
post-order = {8, 4, 5, 2, 6, 7, 3, 1}
 Procedure:
1. Find the last node in post-order traversal. i.e [1]
 -this value is root as root always appear in the end of post-
order traversal.
2. Search “1” in in-order traversal to find left and right
subtrees of root.
Constructing binary tree from
given in-order and post-order
traversals

 Everything on left of “1” in in-order traversal is in
left subtree and everything on right is in right
subtree.
Constructing binary tree from
given in-order and post-order
traversals

 We recur the above process for following two.

 Recur for in[] = {6, 3, 7} and post[] = {6, 7, 3}

 Make the created tree as right child of root.

 Recur for in[] = {4, 8, 2, 5} and post[] = {8, 4, 5, 2}.

 Make the created tree as left child of root.


Activity

 Construct a binary tree from the following in-order

and post-order traversals

In-order: 9,5,1,7,2,12,8,4,3,11

Post-order: 9,1,2,12,7,5,3,11,4,8
Binary tree construction from pre-
order and post-order traversals

 The first item in the pre-order or the last item in the

post-order traversal will always be the root

 Check immediate successor of root and locate it in

the post-order traversal


Binary tree construction from pre-
order and post-order traversals

 Element to the left of the successor of root belong to

the left subtree while those to the right belong to the

right subtree

 Reclusively repeat the step above until the whole

tree is generated
Activity

Construct a binary tree from the following pre-order
and post-order traversals.
1. Pre-order: FBADCEGIH
Post-order: ACEDBHIGF

2. Pre-order: FBADCEGJIHK

Post-order: ACEDBJHKIGF
Insertion and deletion in
BST

 BST has at most 2 children- 0,1,or 2
 Left subtree has values less than the node under
consideration and right subtree has values greater than
that node
 Activity:
 Draw a BST by inserting the following numbers from left to
right
11,6,8,19,4,10,5,7,43,49,39
Deletion

 There are three possible cases

1. Delete a node with zero child


 Delete and free that node

2. Delete a node with one child


 Replace that node with its child
Deletion

3. Delete a node with two children

i. Replace the node being deleted with its in-order


predecessor

 Largest element in the left of that node

ii. Replace the element with its in-order successor

 Smallest element in the right sub-tree





Search Operation

• Start searching from the root node,

• Then if the data is less than the key value, search for the

element in the left sub-tree.

• Otherwise, search for the element in the right sub-tree.

• Follow the same algorithm for each node.


Algorithm

1. If [Link] is equal to [Link]
2. return root
3. else
4. while data not found
5. If data is greater than [Link]
6. goto right subtree
7. else
8. goto left subtree
9. If data found
10. return node
11. endwhile
12. return data not found
13. endif
Heap tree

• Is a special case of balanced/complete binary tree data structure
• The root-node key is compared with its children and arranged
accordingly.
 Types of heaps
• Max Heap- The value of parent is greater than that of child
• Min-Heap − The value of the root node is less than or equal to
either of its children.
Min Heap

Max Heap

Max Heap Construction
Algorithm

 Step 1 − Create a new node at the end of heap.
 Step 2 − Assign new value to the node.
 Step 3 − Compare the value of this child node with its parent.
 Step 4 − If value of parent is less than child, then swap them.
 Step 5 − Repeat step 3 & 4 until Heap property holds.
 Note − In Min Heap construction algorithm, we expect the value of
the parent node to be less than that of the child node.
Max Heap Deletion Algorithm

 Deletion in Max (or Min) Heap always happens at the root to remove
the Maximum (or minimum) value.
 Step 1 − Remove root node.
 Step 2 − Move the last element of last level to root.
 Step 3 −Compare the value of this child node with its parent.
 Step 4 − If value of parent is less than child, then swap them.
 Step 5 − Repeat step 3 & 4 until Heap property holds.

You might also like