0% found this document useful (0 votes)
9 views43 pages

Lec 11

Uploaded by

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

Lec 11

Uploaded by

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

DATA STRUCTURES

AND ALGORITHMS

DR. FATMA ELGENDY


TREE

• A tree T is a set of nodes storing elements such that the nodes have a parent-child
relationship.
• Root: a node with no parent.
• A leaf node has no children.
• Siblings: the children of the same parent.
• Path: A sequence of nodes n1, n2, . . ., nk, such that ni is the parent of ni + 1 for i = 1, 2,. . .,
k–1
• Level: The level of the node refers to its distance from the root.
• The depth of a node is the length of the path (or the number of edges) from the root to
that node.
• The height of a node is the longest path from that node to its leaves.
• The height of a tree is the height of the root.
BASIC TERMS OF A TREE
• TreeNode Implementation
• Tree Implementation
EXAMPLE
LEC 8

Tree
Binary Tree
BASIC TERMS
BINARY TREES

➢ each node can have at most two children (0,1or 2 children).


➢ If h = height of a binary tree (or tree levels), then
a. Maximum number of leaves = 2h
b. Maximum number of nodes = 2h+1 -1
➢ If a binary tree contains m nodes at level 1, it contains at most 2m nodes at level 2.
➢ Since a binary tree can contain at most one node at level 0 (the root), it can contain at most 21
node at level l.
➢ If a binary tree contains m nodes at level L, it contains at most 2m nodes at level L + 1.
BINARY TREE EXAMPLES

(a) (b)
(c) (d) (e)
TYPES OF BINARY TREE

1. Strict Binary Tree: all the Internal nodes must having two
children.
2. Complete Binary Tree: All the levels are completely filled
except possibly the last level.
3. Full Binary Tree: Simply Every node has 0 or 2 children. A full
binary tree of height h has all its leaves at level h.
BINARY TREE TYPES
BINARY TREE IMPLEMENTATION
TRAVERSING A BINARY TREE

➢Traversing a tree means that processing it so that each node is visited


exactly once. A binary tree can be traversed a number of ways.
➢The most common tree traversals are
1. pre-order root-left-right
2. in-order left-root-right
3. Post-order left-right-root
EXAMPLE OF TRAVERSING A TREE
POST-ORDER IMPLEMENTATION
PRE-ORDER IMPLEMENTATION
IN-ORDER IMPLEMENTATION
Binary Search Tree
BINARY SEARCH TREE (BST )

BST Characteristics
1. It is a binary tree.
2. Values of all nodes in its left subtree must be smaller than its root value.
3. Values of all nodes in its right subtree must be greater than its root value.
Example: You have the following sequence of numbers.

45, 36, 76, 23, 89, 115, 98, 39, 41, 56, 69, 48

a. Make a BST for the above sequence of numbers.


a. Traverse the obtained BST in Pre-order, In-order and post-order.

b. Pre-order: root – left - right

• 45, 36, 23, 39, 41, 76, 56, 48, 69, 89, 115, 98

• In-order: left – root - right

• 23, 36, 39, 41, 45, 48, 56, 69, 76, 89, 98, 115

• Post-order: left – right – root

• 23, 41, 39, 36, 48, 69, 56, 98, 115, 89, 76, 45
MOST BST OPERATIONS

• Look up (contains)
• Insert
• remove
LOOKUP STEPS

1. Start from the root.


2. Compare the searching element with root, if less than root, then recursively call
left subtree, else recursively call right subtree.
3. If the element to search is found anywhere,
return true, else return false.
LOOKUP (CONTAINS)
INSERTION

1. Create a new node (node) and assign value to it.

2. Find the parent of it.


3. If parent = NULL then, root = node
else if node.data <parent.data. Then, assign node to the left of parent
else assign node to the right of parent
INSERTION
REMOVAL

• Four scenarios of node removal


1. Removing a leaf node
2. Removing a node with only a left child
3. Removing a node with only a right child
4. Removing a node with both left and right children
• Removing a leaf node: Simply remove it from the tree.
• Deleting a node with one child: remove the node and replace it with its child.
• Delete a node with two children

Case 2

You might also like