2. Binary Trees
• According to the definition of trees, a node can
have any number of children.
• A binary tree is restricted to only having 0, 1, or 2
children.
• A complete binary tree is one where all the levels
are full with exception to the last level and it is
filled from left to right.
• A full binary tree is one where if a node has a
child, then it has two children.
3. BINARY TREES
Every node in a binary tree can
have at most two children.
The two children of each node are
called the left child and right child
corresponding to their positions.
A node can have only a left child or
only a right child or it can have no
children at all.
Left child is always less that its
parent, while right child is greater
than its parent.
4. TERMINOLOGY
Each node has to be reachable from the
roots through a unique sequence of arcs
called a path.
The number of arcs in a path is called the
length of the path.
The level of a node is the length of the
path from the root to the node plus 1.
The height of a non-empty tree is the
maximum level of a node in the tree.
5. Picture of a binary tree5
a
b c
d e
g h i
l
f
j k
The root is
drawn at the top
6. Tree traversals
A binary tree is defined recursively: it consists of a root, a
left subtree, and a right subtree
To traverse (or walk) the binary tree is to visit each node
in the binary tree exactly once
Tree traversals are naturally recursive
Since a binary tree has three “parts,” there are six
possible ways to traverse the binary tree:
root, left, right
left, root, right
left, right, root
root, right, left
right, root, left
right, left, root
6
7. Tree Traversals using
“flags”
• The order in which the nodes are visited during a tree
traversal can be easily determined by imagining there is a
“flag” attached to each node, as follows
• To traverse the tree, collect the flags
7
preorder inorder postorder
A
B C
D E F G
A
B C
D E F G
A
B C
D E F G
A B D E C F G D B E A F C G D E B F G C A
8. SORTED BINARY
TREES
A binary tree is sorted if every node in the tree is larger
than (or equal to) its left descendants, and smaller than
(or equal to) its right descendants
Equal nodes can go either on the left or the right (but it
has to be consistent)
8
10
8 15
4 12 20
17