0% found this document useful (0 votes)
35 views15 pages

B E D F C A Binary Tree

- A binary tree is a set of elements that is either empty or partitioned into a root element and two disjoint binary trees called the left and right subtrees. - There are three methods for traversing a binary tree: preorder, inorder, and postorder. Preorder visits the root first, then traverses the left subtree, then the right subtree. Inorder traverses the left subtree, then visits the root, then the right subtree. Postorder traverses the left subtree, then the right subtree, then visits the root.

Uploaded by

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

B E D F C A Binary Tree

- A binary tree is a set of elements that is either empty or partitioned into a root element and two disjoint binary trees called the left and right subtrees. - There are three methods for traversing a binary tree: preorder, inorder, and postorder. Preorder visits the root first, then traverses the left subtree, then the right subtree. Inorder traverses the left subtree, then visits the root, then the right subtree. Postorder traverses the left subtree, then the right subtree, then visits the root.

Uploaded by

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

Binary Tree

A binary tree is a finite set of elements that is 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 or right subtree can be empty.

Each element of a tree is called a node of the tree.


A Binary tree
B
c
D E
F

G
H I
Binary Tree

Structures that are not binary trees

B c

E F
D

I
G H
Binary Tree

Structures that are not binary trees

B c

E F
D

G
Binary Tree

Structures that are not binary trees

B c

E F
D

H I
Binary Tree
Strictly binary trees:
If every nonleaf node in a binary tree has nonempty left and
right subtrees, the tree is called a strictly binary tree.
A strictly binary tree with n leaves always contains 2n -1
nodes.
A

B C

D E

F G
Binary tree

Structure that is not a strictly binary tree:


because nodes C and E have one son each.

A
B
c
E
D
F

G
H I
Level and depth of a binary Tree
Level of binary tree:
The root of the tree has level 0. And the level of any other node
is one more than the level of its father.
Depth of a binary tree:
The depth of a binary tree is the maximum level of any leaf in
the tree. Level 0
A

C Level 1
B

D E Level 2

F G Level 3

Depth is 3.
A complete binary tree
Complete binary tree of depth d is the strictly binary tree all of
whose leaves are at level d.
A complete binary tree of depth d is the binary tree of depth d
that contains exactly 2 l nodes at each level l between 0 and d.
The total number of nodes = the sum of the number of nodes at
each level between 0 and d.
= 2 d+1 - 1
A
B
C
D E F G

H I J K L M N O
Almost complete binary tree
A binary tree of depth d is an almost complete binary tree if:
- 1. A node nd at level less than d -1 has two sons
- 2. For any node nd in the tree with a right descendant at level d, nd must have
a left son and every left descendant of nd is either a leaf at level d or has
two sons.
A
The strictly binary tree
B C is not almost complete,
since it contains leaves
D E at levels 1, 2, and 3.

Violates condition 1
F G
Almost complete binary tree
A binary tree of depth d is an almost complete binary tree if:
- 1. A node nd at level less than d -1 has two sons
- 2. For any node nd in the tree with a right descendant at level d, nd must have
a left son and every left descendant of nd is either a leaf at level d or has
two sons.
The strictly binary tree
is not almost complete,
A
since A has a right descendant at
B level 3 (J) but also has a left
C descendant that is a leaf at level
D E 2 (E)
F G
Violates condition 2
H I J K
Satisfies the condition1, since every
leaf node is either at level 2 or at
level 3.
Almost complete binary tree
A binary tree of depth d is an almost complete binary tree if:
- 1. A node nd at level less than d -1 has two sons
- 2. For any node nd in the tree with a right descendant at level d, nd must have
a left son and every left descendant of nd is either a leaf at level d or has
two sons.
The binary tree
1 is almost complete,
A
2 Satisfies the condition1, since every
B 3 leaf node is either at level 2 or at
C level 3.
4 5 6 7
D E F G Satisfies the condition 2
8 9
H I
Almost complete binary tree
A binary tree of depth d is an almost complete binary tree if:
- 1. A node nd at level less than d -1 has two sons
- 2. For any node nd in the tree with a right descendant at level d, nd must have
a left son and every left descendant of nd is either a leaf at level d or has
two sons.
The binary tree
1 is almost complete,
A
2 Satisfies the condition1, since every
B 3 leaf node is either at level 2 or at
C level 3.
4 5 6 7
D E F G Satisfies the condition 2
8 9
H I F 10 However, the binary tree is not
strictly binary tree, since node E has
a left son but not a right son
Traversing a binary tree
Three methods:
- 1. preorder Preorder
- 2. inorder 1. Visit the root
- 3 postorder 2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
14 Inorder
1. Traverse the left subtree in inorder
4
15 2. Visit the root
3. Traverse the right subtree in inorder
3 9 18

7 16 20

5 17 Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
Traversing a binary tree
A Preorder
B 1. Visit the root
C 2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
D F
E preorder: ABDGCEHIF
G
H I Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
inorder: DGBAHEICF
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
postorder: GDBHIEFCA
Traversing a binary tree
A Preorder
1. Visit the root
B 2. Traverse the left subtree in preorder
D 3. Traverse the right subtree in preorder
C H preorder: ABCEIFJDGHKL
G
E F K L
Inorder
1. Traverse the left subtree in inorder
I J 2. Visit the root
3. Traverse the right subtree in inorder
inorder: EICFJBGDKHLA
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
postorder: IEJFCGKLHDBA

You might also like