Professional Documents
Culture Documents
CENG205 Ders11
CENG205 Ders11
Data structures
Lecture 11:
Trees
Content
• Terminology
• The Binary Tree
• The Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Terminology
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Terminology
• Only access point is the root
• All nodes, except the root, have one parent
• like the inheritance hierarchy in Java
• Traditionally trees are drawn upside down
root
leaves
Terminology
• (a) a tree;
(b) a subtree of the tree in part a
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Terminology
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Properties of Trees and Nodes
root
• siblings: two nodes that have the same
parent edge
• edge: the link from one node to another
• path length: the number of edges that
must be traversed to get from one node
to another
siblings
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Binary Tree
• A Binary tree is a data structure in that each node has at most two
nodes left and right.
• A Binary tree can be empty.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
n -ary tree
• A generalization of a binary tree whose nodes each can have no
more than n children.
n
Example: Algebraic Expressions.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Level of a Node
• Definition of the level of a node n :
• If n is the root of T, it is at level 1.
• If n is not the root of T, its level is 1 greater than the level of its parent.
Level = 1
Level = 2
Level = 3
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Height of Trees
• The height of a node is the number of edges on the longest downward path
between that node and a leaf.
Height of tree = 2
The Height of Trees
• The path length from the root of the tree to this node.
g h i j k
l
Full, Complete, and Balanced Binary Trees
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Full Binary Trees
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Facts about Full Binary Trees
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Complete Binary Trees
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Full,
Complete or A not binary
Other?
B C D
I H G F E
J K L M N O
T S R Q P
Full,
Complete or A
other?
B D
I H G F
K L M N O
T S R P
Full, A
Complete or
other?
B D
I H G F
K L O
M N
T S R P
Full, A
Complete or
other?
B D
I H G F
K L O
M N
T S R P
Full, A
Complete or
other?
B D
I H G F
K L O R P
M N
T S
Full, A
Complete or
Other?
B D
I H G F
K L M N O P Q R
T S
Full, A
Complete or
other?
B D
I H G F
K L M N O P Q R
T S
Full, A
Complete or
Other?
B D
I H G F
K L M N O P Q R
• A balanced binary tree has the minimum possible height for the leaves
a
b
c e
d f
g h
i j
An unbalanced binary
tree
Number of Nodes in a Binary Tree
•
depth: h level: h+1
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Traversals of a Binary Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Preorder Traversal
1. Visit the root
2. Visit the left subtree
1
A
3. Visit the right subtree
B
2 11
D
3I H
8 G
12 F
13
K
4 5
L O
14
M
9 N
10
T
6 S7 R
15 P
16
Algorithm TraversePreorder(n)
Process node n
if n is an internal node then
TraversePreorder( n -> leftChild)
TraversePreorder( n -> rightChild)
Inorder Traversal
1. Visit Left subtree
2. Visit the root 10
1A
3. Visit Right subtree
6B
1 12
D
2I
1 8
H G
11 16
F
K
1 4
L 14
O
M
7 N9
T
3 5
S R
13 P
15
Algorithm TraverseInorder(n)
if n is an internal node then
TraverseInorder( n -> leftChild)
Process node n
if n is an internal node then
TraverseInorder( n -> rightChild)
Postorder Traversals
1. Visit Left subtree
A
16
2. Visit Right subtree
3. Visit the root
B
9 D
15
5I H
8 G
10 F
14
K
1 L
4
2 O
13
M
6 N
7
T2 S
3 R
11 P
12
Algorithm TraversePostorder(n)
if n is an internal node then
TraversePostorder( n -> leftChild)
TraversePostorder( n -> rightChild)
Process node n
Traversals of a Binary Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Binary Tree Operations
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Binary Tree Operations
• Remove the node containing a given data item from a binary tree.
• Remove all nodes from a binary tree.
• Retrieve a specific entry in a binary tree.
• Test whether a binary tree contains a specific entry.
• Traverse the nodes in a binary tree in preorder, inorder, or postorder.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Represention of Binary Tree ADT
A binary tree can be represented using
- Linked List
- Array
Note : Array is suitable only for full and complete binary trees
struct node
{
int key_value;
struct node *left;
struct node *right;
};
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Binary Search Tree Operations
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Binary Search Tree Operations
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Searching a Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
struct node *search(int key, struct node *leaf)
{
if( leaf != 0 )
{
if(key==leaf->key_value)
{
return leaf;
}
else if(key<leaf->key_value)
{
return search(key, leaf->left);
}
else
{
return search(key, leaf->right);
}
}
else return 0;
}
Creating a Binary Search Tree
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
void insert(int key, struct node **leaf)
{
if( *leaf == 0 )
{
*leaf = (struct node*) malloc( sizeof( struct node ) );
(*leaf)->key_value = key;
/* initialize the children to null */
(*leaf)->left = 0;
(*leaf)->right = 0;
}
else if(key < (*leaf)->key_value)
{
insert(key, &(*leaf)->left );
}
else if(key > (*leaf)->key_value)
{
insert(key, &(*leaf)->right );
}
}
Deleting An Item from a BST
• To delete an item from a BST, we have to locate that item in that BST.
50 50
➔
40 60 40 60
30 45 70
30 45
42
42
50
50
➔ 40 60
40 60
30 42 70
30 45 70
42
50
50
➔ 40 70
40 60
30 45
30 45 70
42
42
• Copy the item in this node into the node which contains the item which will be deleted.
50 50
➔ 42 60
40 60
70 30 45 70
30 45
42
Delete 2
Deletion from a BST
if ((*leaf)->left == NULL) {
int key = (*leaf)->key_value;
struct node *deleted = *leaf;
*leaf = (*leaf)->right;
deleted->right = NULL; // defense
free(deleted);
return key;
}
else
return processLeftmost((*leaf)->left);
}
Efficiency of Binary Search Tree
Operations
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Saving a BST into a file and
restoring it to its original shape
• Save:
– Use a preorder traversal to save the nodes of the BST into a file
• Restore:
– Start with an empty BST
– Read the nodes from the file one by one and insert
them into the BST
Saving a BST into a file and
restoring it to its original shape
Preorder: 60 20 10 40 30 50 70
Saving a BST into a file and
restoring it to a minimum-height BST
• Save:
– Use an inorder traversal to save the nodes of the BST into a file. The
saved nodes will be in ascending order
– Save the number of nodes (n) in somewhere
• Restore:
– Read the number of nodes (n)
– Start with an empty BST
– Read the nodes from the file one by one to create a minimum-
height binary search tree
Building a minimum-height
BST
// Builds a minimum-height binary search tree from n sorted
// values in a file. treePtr will point to the tree’s root.
readTree(out treePtr:TreeNodePtr, in n:integer)
if (n>0) {
treePtr = pointer to new node with NULL child pointers
A pointer-based implementation of
a general tree can also represent
a binary tree.
N-ary
Tree
An n-ary tree is a generalization of a binary whose
nodes each can have no more than n children.