lec7data
lec7data
Root node A
Subtree
B C D
E F G H I
J K
Leaves nodes Leaves nodes
Binary Tree
A binary tree is a finite set of "nodes". The set might be empty (no nodes,
which is called empty tree). But if the set is not empty, it follows these rules:
1. There is one special node called the root.
2. Each node may be associated with up to two other different nodes,
called its “left child” and its “right child”. If a node c is the child of
another node p, then we say that "p is c's parent".
3. Each node, except the root, has exactly one parent; the root has no
parent.
Sub trees
Types of Trees
1- Full Binary Tree: is a binary tree in which all of the leaves are on the same
level and every non leaf node has two children. The basic shape of a full binary
tree is triangular.
2- Complete Binary Tree: is the binary tree that is either full or full through
the next to the last level, which leaves on the last level as far left as possible.
The shape of complete binary tree is either triangular (if tree is full) or
something like the following:
3- Heap Tree: is a data structure that satisfies two properties one concerning
it’s shape: A heap tree must be a complete tree, and the other concerning the
order of its elements: For every node in the heap (Max Heap), the value stored
in that node is greater than or equal to the value in each of its children. The
root node will always contains the largest value in the heap and thus we always
know where the maximum value is in the root.
4- Strictly Binary Tree: is a binary tree in which each node except the leaf
has two children. A strictly binary tree which has (n) leaves always contains
(2*n-1) nodes.
5- Balanced Binary Tree: In a binary tree, each node has a factor called
“balance factor”. Balance factor of a node is the height of the left subtree
minus the height of the right subtree. If each node in the binary tree has a
balance factor equal to -1 or 0 or 1 then this binary tree is called “balanced”.
B
B C
D C
D M N K L
V M K
V W S
W N S L
Operator
Operand Operand
1 2
The simplest arithmetic expression consisting of single constant, The
corresponding tree consists of a single node.
Y=3 3
3+5
3 5
A more complex expression can be represented as any root in a tree or any subtree
is operator and leaves contain operands (variables and constants).
1- 5-3*6-9 -
- 9
5 *
3 6
2- a+(b-c)*d↑(e*f)
+
a *
- ↑
b c d *
e f
3- (a+b+c)*(e-f/h)
+ -
+ c /
e
a b f h
c
Prepared by Dr. Dunia H. Hameed Page 51
Data Structures 2024-2025
Tree traversals
Tree traversals means visit all the nodes in a tree only once. There are 3 common
ways for traversals of binary tree: in-order traversal, pre-order traversal, and post-
order traversal.
1- Inorder traversal: each node is visited in between its left and right
subtrees.
a. left subtree
A +
b. root
c. right subtree
B C a b
B A C
a + b (infix expression)
2- Preorder traversal: each node is visited before its left and right subtrees.
a. Root
b. left subtree
c. right subtree
A B C
+ a b (prefix expression)
3- Postorder traversal: each node is visited after its left and right subtrees.
a. left subtree
b. right subtree
c. root
B C A
a b + (suffix expression)
Note that there are another methods to traverse general tree, a tree if we convert
left by right.
1- Converse inorder: each node is visited in between its right and left
subtrees.
a. right subtree
b. root
c. left subtree
2- Converse preorder: each node is visited after its right and left subtrees.
a. Root
b. right subtree
c. left subtree
3- Converse postorder: each node is visited before its right and left subtrees.
a. right subtree
b. left subtree
c. root
Note that there are two methods depend on level concept to traverse general
tree.
1- Level by Level:
A. Top-Down: Nodes are visited starting from top level (level 0) down
to the last level. In each level we start from left to right.
B. Bottom-Up: Nodes are visited from the last level up to the top level
(level 0). In each level we start from left to right.
2- Converse Level by Level:
A. Top-Down: Nodes are visited starting from top level (level 0) down
to the last level. We start from right to left.
B. Bottom-Up: Nodes are visited from the last level up to the top level
(level 0). In each level we start from right to left.
20 50
18 25 40 55
22 33 70
37
Inorder: 18-20-22-25-30-33-37-40-50-55-70
Preorder: 30-20-18-25-22-50-40-33-37-55-70
Postorder: 18-22-25-20-37-33-40-70-55-50-30
Converse Inorder: 70-55-50-40-37-33-30-25-22-20-18
Converse Preorder: 30-50-55-70-40-33-37-20-25-22-18
Converse Postorder: 70-55-37-33-40-50-22-25-20-18-30
Level by Level (top-down): 30-20-50-18-25-40-55-22-33-70-37
Level by Level (bottom-up): 37-22-33-70-18-25-40-55-20-50-30
Converse Level by Level (top-down): 30-50-20-55-40-25-18-70-33-22-37
Converse Level by Level (bottom-up): 37-70-33-22-55-40-25-18-50-20-30
Example2: Given the following tree, write the inorder, postorder and preorder
traversals?
B C
R Z M L
H K
R B Z H A M C LK (inorder)
A B R Z H C M LK (preorder)
R H Z B M K L C A (postorder)
Example 3: ((A+B)-(C↑D))+(E/F)
Given the above expression, draw the expression tree then use the preorder and
preorder traversals to obtain prefix and suffix forms(notations)?
- /
+ ↑ E F
A B C D
Example 4: Given the following tree, write the inorder, postorder, preorder
traversals and the original arithmetic expression?
- /
+ C B ↑
A B Z R
A+B-
C+B/Z↑R
(inorder)
AB+C-
BZR↑/+
(postorder)
+-
+ABC/B↑ZR
(preorder)
(((A+B)-C)+(B/(Z↑R))) (original arithmetic expression)
+ 5
- /
* C B 3
A B
b c
d k
h l
2) a
b f
c m h l
d n x
3) a
4)
a
Remark1: We can know the root from postorder traversal then we can know
the left subtree and the right subtree from the inorder traversal.
Remark2: If the tree is in one side left or right then there are two equal
traversals:
1- If inorder = preorder means that the tree is on the right.
2- If inorder = postorder means that the tree is on the left.
3- If inorder=postorder=preorder means that the tree consisted
from one node.
Remark3: If the word left interchanged by right in the traversal, we obtain
convert inorder, converse preorder and converse postorder.