9 Trees
9 Trees
Trees
Ma:H.A.S
Type of Trees
• Binary Search.
• Binary Tree.
• B Tree.
• AVL Tree.
• Heaps Tree.
• Multiway Tree.
• Applications specific tree.
06/26/2024
Ma:H.A.S
Binary Search
I L
MUST Be Sorted
M=(I+L)/2
Searche Key=55
If arr(6)> key
{
06/26/2024
Ma:H.A.S
Hashing (Hash Table)
06/26/2024
Ma:H.A.S
Graph
06/26/2024
Ma:H.A.S
Tree
Ma:H.A.S
Tree
Ma:H.A.S
Tree Terminology
1. Root
•Root is the
topmost node of the tree.
•There is only one root per
tree and one path from
the root node to any
node.
2. Edge
Trees:
Here’s a purty picture of a tree:
2 is the root
2, 5, 11, and 4 are leaves
7, 5, 6, and 9 are interior
nodes
page 18
Ma:H.A.S
9. Level
A path is a sequence of
nodes
(a0, a1, ..., an)
where ak + 1 is a child of ak
Ma:H.A.S
14. Forest
• A forest is a set of
disjoint trees
Ma:H.A.S
Tree Application
• Applications:
– Organization charts
– File systems
– Programming environments
File System
Organization charts two files in different directories can share the same name.
Ma:H.A.S
Tree Application
Programming environments
Ma:H.A.S
Tree Application
Library
Example
Ma:H.A.S
Implementation of Trees
First way to implement a tree
• Represent Each node with list contains the data of node and
link to each child.
• Since the number of children node can vary so greatly and is
not known in advance.
• There would be too much wasted space.
data
........................ data
Ma:H.A.S
Implementation of Trees
Ma:H.A.S
Binary Trees
{ -1 , 0 ,1 }
Ma:H.A.S
Special Kinds of Binary Trees
• A full binary tree is a binary tree in which every node has either 0 or 2 children (a)
• A perfect binary tree is a binary tree in which all interior nodes have 2 children and all
leaves have the same depth or same level (b).
• A complete binary tree is a binary tree in which all the levels are completely filled
except possibly the lowest one, which is filled from the left. (c).
• A skewed binary tree is a binary tree in which all the nodes have only either 0 or 1 child
Ma:H.A.S
Some Numbers
h=3
Ma:H.A.S
Time Complexity
Min:Short left 40
Max:Short Right
30 45
30 50
35 42
22
06/26/2024
Ma:H.A.S
Binary Tree Implementation
1- using array
2- using linked list
Ma:H.A.S
Binary Tree Implementation- using Array
C
D
E
struct TreeNode
{ int data;
TreeNode *left;
TreeNode *right;
};
TreeNode* root;
Ma:H.A.S
Tree Traversal
Ma:H.A.S
Depth First Traversals
Ma:H.A.S
06/26/2024
Ma:H.A.S
Binary Tree Traversals
outpu A / B* C * D + E
t:
pt
L r
V
R
Ma:H.A.S
Breadth
06/26/2024
Ma:H.A.S
Preorder
06/26/2024
Ma:H.A.S
Inorder
06/26/2024
Ma:H.A.S
Postorder
06/26/2024
Ma:H.A.S
06/26/2024
Ma:H.A.S
Depth First Traversals
Ma:H.A.S
Depth First Traversals
Ma:H.A.S
Breadth First Traversal
Ma:H.A.S
Int TreeSize (node* root)
int THeight (TreePointer root)
{
{
if (root==null) if (root==null)
return 0; return 0;
else if (root -> left==null)& (root -> right==null)
return( 1+TreeSize(root->left)+ TreeSize(root->right)); return 0;
} else
return (1 + Max(THeight(root->left),THeight(root->right));
Ma:H.A.S
Ma:H.A.S
AVL Rotation
06/26/2024
Ma:H.A.S
Rotation
06/26/2024
Ma:H.A.S
تمرين
06/26/2024
Ma:H.A.S
Binary Tree Traversals – Practice Problems
3
Practice Tree #1 13 28
Solutions on page 36
22 9 15 36
75 10
33 7 44
19
26 14 87 63 69
54 71 11 8 56 59 68
page 56
Ma:H.A.S
Binary Tree Traversals – Practice Problems
3
28 13 Practice Tree #2
Solutions on Page 37
36 15 9 22
10 75
44 7 33
19
69 63 87 14 26
68 59 56 8 11 71 54
page 57
Ma:H.A.S
Practice Problem Solutions – Tree
#1
Preorder Traversal:
3, 13, 22, 19, 26, 54, 71, 33, 14, 11, 87, 8, 56, 9, 75, 28, 15, 10, 63, 36, 7, 69,
59, 68, 44
Inorder Traversal:
54, 26, 71, 19, 22, 11, 14, 33, 8, 87, 56, 13, 9, 75, 3, 63, 10, 15, 28, 59, 69, 68,
7, 36, 44
Postorder Traversal:
54, 71, 26, 19, 11, 14, 8, 56, 87, 33, 22, 75, 9, 13, 63, 10, 15, 59, 68, 69, 7, 44,
36, 28, 3
page 58
Ma:H.A.S
Practice Problem Solutions – Tree
#2
Preorder Traversal:
3, 28, 36, 44, 7, 69, 68, 59, 15, 10, 63, 13, 9, 75, 22, 33, 87, 56, 8, 14, 11, 19,
26, 71, 54
Inorder Traversal:
44, 36, 7, 68, 69, 59, 28, 15, 10, 63, 3, 75, 9, 13, 56, 87, 8, 33, 14, 11, 22, 19,
71, 26, 54
Postorder Traversal:
44, 68, 59, 69, 7, 36, 63, 10, 15, 28, 75, 9, 56, 8, 87, 11, 14, 33, 71, 54, 26, 19,
22, 13, 3
page 59
Ma:H.A.S