0% found this document useful (0 votes)
8 views59 pages

9 Trees

Uploaded by

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

9 Trees

Uploaded by

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

10 ‫المحاضرة‬

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

• The Array Must Be Ordered.


• Too Fast Search.
• Sequential search Vs Binary Search.
• Depend on Divide and conquer.
• How it work?.
• BinarySearch(Arr[],I,j,key).
• Time complexity(
– best case : O(1)
– Avr case: Olog(n)
– Worst case: Olog(n) ).
• Space complexity ( O(1)).
06/26/2024
Ma:H.A.S
Example

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)

Insert time complexity O(1)

06/26/2024
Ma:H.A.S
Graph

06/26/2024
Ma:H.A.S
Tree

• A tree is a collection of nodes;


• The collection can be empty; otherwise, a tree consist of a
distinguished node, called the root, and zero or more nonempty (sub)
trees T1, T2, ….,Tk , each of whose roots are connected by a directed
edge from r.
• The root of each subtree is a child of r
and r is the parent of each subtree root
• Tree is a non-linear data structure compared to arrays, linked lists,
stacks and queues which are linear data structures.
• Tree organizes data in a hierarchical structure.

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

• The connecting link


between any two nodes
is called as an edge.
• In a tree with n number
of nodes, there are
exactly (n-1) number of
edges
3. Parent

• The node which has an


edge from it to any
other node is called as
a parent node.
• In a tree, a parent node
can have any number of
child nodes
4. Child

• The node below a given


node connected by its
edge downward is
called its child node.
• All the nodes except
root node are child
nodes.
5. Siblings

• Nodes which belong to


the same parent are
called as siblings.
• In other words, nodes
with the same parent
are sibling nodes.
6. Degree

• Degree of a node is the


total number of
children of that node.
• Degree of a tree is the
highest degree of a
node among all the
nodes in the tree.
7. Internal Node

• The node which has at


least one child is called
as an internal node.
• Internal nodes are also
called as non-terminal
nodes.
8.Leaf Node

• The node which does not


have any child is called as
a leaf node.
• Leaf nodes are also called
as external
nodes or terminal nodes
Tree Stuff

 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

• Level of a node represents


the generation of a node.
• If the root node is at level
0, then its next child node
is at level 1, its grandchild
is at level 2, and so on.
• The level count starts with
0 and increments by 1 at
each level or step
10. Height

• Total number of edges


that lies on the longest
path from any leaf node
to a particular node is
called as height of that
node.
• Height of a tree is the
height of root node.
• Height of all leaf nodes
=0
11. Depth

• The number of edges from


root node to a particular
node is called as depth of
that node.
• Depth of a tree is the the
number of edges from root
node to a leaf node in the
longest path.
• Depth of the root node = 0
• The terms “level” and
“depth” are used
interchangeably
12. Subtree

• In a tree, each child from


a node forms
a subtree recursively.
• Every child node forms a
subtree on its parent
node.
13.Path

A path is a sequence of
nodes
(a0, a1, ..., an)
where ak + 1 is a child of ak

The length of this path is n

E.g., the path (B, E, G)


has length 2

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

Consider the following XHTML document


<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>

<p>This is a paragraph with some


<u>underlined</u> text.</p>
</body>
</html>

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 data data

........................ data

Ma:H.A.S
Implementation of Trees

Second way to implement a tree left child – right


sibling
Represent the tree with linked list , each node of
list contains data, left child node , right sibling
node

Ma:H.A.S
Binary Trees

• A binary tree is a tree in which no node can have more than


two children.
• a binary tree consists of a root and two subtrees, TL and TR,
both of which could possibly be empty.
• the average value of the depth is O(logN)
• Binary tree example:

{ -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

(a) (b) (c)


(d)

Ma:H.A.S
Some Numbers

For binary tree of height h:


- max # of tree nodes: 2h+1-1 root height=0
some book considered the height of the root is as1 and the max # of tree nodes 2h-1

- max # of tree leaves: 2h


- min # of tree leaves: 1
- min # of tree nodes: h+1
- max # of nodes on level i: 2i, i 0.

h=3

Ma:H.A.S
Time Complexity

Avg time complexity: o(n)


Tree Have 10 Node : log2(10)=4
Tree Have 1000 Node : log2(1000):10
06/26/2024 Tree Have 1000000 Node : log2(1000000):20 Ma:H.A.S
• 40,45,30,35,30,22,50
• Insert 42 o(n)
• Search 35

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

A complete binary tree with n node can be represented


using an array with an index starting at 1 , for node i we
have:
Using Array

Ma:H.A.S
Binary Tree Implementation- using Array

Waste spaces: in the worst case, a skewed tree of depth k requires


2k spaces. Only k spaces will be occupied

C
D
E

Insertion or deletion of nodes from the middle of a tree requires the


movement of potentially many nodes to reflect the change in the
level of these nodes
Ma:H.A.S
Binary Tree Implementation- using Linked list

struct TreeNode
{ int data;
TreeNode *left;
TreeNode *right;
};
TreeNode* root;

Ma:H.A.S
Tree Traversal

• Tree traversal is visiting each and every node of


the tree only once.
• Unlike linear data structures (Array, Linked List,
Queues, Stacks, etc) which have only one logical
way to traverse them, trees can be traversed in
different ways.
– Depth First Traversal (ex: Queue)
– Breadth First Traversal

Ma:H.A.S
Depth First Traversals

1.Inorder (Left, Root, Right) : 4 2 5 1 3


2.Preorder (Root, Left, Right) : 1 2 4 5 3
3.Postorder (Left, Right, Root) : 4 5 2 3 1

Ma:H.A.S
06/26/2024
Ma:H.A.S
Binary Tree Traversals

 Inorder traversal (LVR) (recursive version)

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

Therefore we can do inorder traversal of the binary


tree and evaluate the expression

Ma:H.A.S
Depth First Traversals

struct Node { void printInorder( Node* node) int main()


int data; { {
Node *left, *right; if (node == NULL) Node* root = newNode(1);
return; root->left = newNode(2);
};
printInorder(node->left); root->right = newNode(3);
Node* newNode(int data) cout << node->data << " "; root->left->left = newNode(4);
{Node* temp = new Node; printInorder(node->right); root->left->right = newNode(5);
temp->data = data; } cout << "\nPreorder \n";
temp->left = temp->right = printPreorder(root);
NULL; cout << "\nInorder \n";
return temp; void printPreorder( Node* node) printInorder(root);
{ cout << "\nPostorder \n";
}
if (node == NULL) printPostorder(root);
return; return 0;
void printPostorder( Node* cout << node->data << " "; }
node) printPreorder(node->left);
{ printPreorder(node->right);
if (node == NULL) }
return;
printPostorder(node->left);
printPostorder(node->right);
cout << node->data << " ";
}

Ma:H.A.S
Breadth First Traversal

 Breadth First Traversal is Level order traversal of a tree.


 We visit the root first, then the root’s left child, followed by the root’s right child.
 We continue in this manner, visiting the nodes at each new level from the leftmost node to
the rightmost nodes
void printLevelOrder(Node* root)
int main()
{ if (root == NULL) return;
{
queue<Node*> q;
Node* root = newNode(1);
q.push(root);
root->left = newNode(2);
while (q.empty() == false)
root->right = newNode(3);
{ Node* node = q.front();
root->left->left = newNode(4);
cout << node->data << "
"; root->left->right = newNode(5);
q.pop(); cout << " breadth first \n";
if (node->left != NULL) printLevelOrder(root);
q.push(node->left); cout << "\n";
if (node->right != NULL) return 0;
q.push(node->right); }
}}

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));

int NumOfLeaves (node* root) }


{ if (root==null)
return 0;
else if ((root->left=null) && (root->right=null)) return 1;
else
return (NumOfLeaves(root->left) + NumOfLeaves(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

You might also like