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

Binary Tree Lesson

A binary tree is a tree data structure where each node has at most two children. There are different types of binary trees including complete, full, and skewed binary trees. Binary trees can be used for expressions and searching. They allow traversing the tree in preorder, inorder, and postorder traversal by recursively visiting nodes. Binary trees can also be implemented using arrays by assigning index values to nodes and calculating child and parent positions based on the index.

Uploaded by

fsshidiq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Binary Tree Lesson

A binary tree is a tree data structure where each node has at most two children. There are different types of binary trees including complete, full, and skewed binary trees. Binary trees can be used for expressions and searching. They allow traversing the tree in preorder, inorder, and postorder traversal by recursively visiting nodes. Binary trees can also be implemented using arrays by assigning index values to nodes and calculating child and parent positions based on the index.

Uploaded by

fsshidiq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Binary Tree

NH

Binary Tree
A tree in which no node can have more than
two children
Root

TL

TR

Types of Binary Tree:


1 Complete Binary Tree:
a All nodes, except leaves, will only have either
0 or two children
b Each sub tree may have different path length

Binary Tree

NH

2 Full Binary Tree


a Same as complete binary tree
b All sub trees must have same path length

3 Skewed Binary Tree


All nodes, except leaf, have only one child

Use of Binary Tree


1 Used as expression tree.
2 Used for searching (BST).
Expression Tree
(a + b * c) + ((d * e + f) * g)

Binary Tree

NH

+
a

How to construct the expression tree?


1 Convert the infix expression into suffix
2 Create stack of pointers to nodes
3 Make Push and Pop operations on suffix expr.
(a + b * c) + ((d * e + f) * g) a b c * + d e * f + g * +
. . .
*

a
a

+
c
*

a
b

a
c

+
c
d

*
e

Binary Tree

NH

Tree Traversal
An operation to trace nodes in the tree
PreOrder Traversal: (Recursive)
o Visit (and Print) root of the tree
o Visit Left SubTree of the root
o Visit Right SubTree of the root
InOrder Traversal: (Recursive)
o Visit Left SubTree of the root
o Visit (and print) root of the tree
o Visit Right SubTree of the root
PostOrder Traversal: (Recursive)
o Visit Left SubTree of the root
o Visit Right SubTree of the root
o Visit (and print) root of the tree
The Algrithm/Pseudo Code
struct Node{
datatype data;
Node *left,*right;
};

Node *root;

Binary Tree

PreOrder(Node *n)
{
if(n){
print(ndata);
PreOrder(nleft);
PreOrder(nright);
}
}
InOrder(Node *n)
{
if(n){
InOrder(nleft);

NH

print(ndata);
InOrder(nright);
}
}
PostOrder(Node *n)
{
if(n){
PostOrder(nleft);
PostOrder(nright);
print(ndata);
}
}

Binary Tree

NH

Exercises
1. Do preorder and postorder traversals on the tree
above.
2. How to implement binary tree using array?
Hints:
a. Index value of the array refers to node
number
b. Root must be in index value of 0
c. Tree with n nodes will have node number
starting from 0 to (n-1)
d. Position of left child of a node whose index
value p is (2p + 1)
e. Position of right child of a node whose index
value p is (2p + 2)
f. Position of parent of a node whose index value
p is (p 1)/2

You might also like