Data Structure: Ali Kalakech
Data Structure: Ali Kalakech
Ali Kalakech
[email protected]
1
Binary Trees
2
Linear data structures
Here are some of the data structures we have studied so
far:
Arrays
Singly-linked lists and doubly-linked lists
Circular lists
Stacks, queues
3
Parts of a binary tree
4
Implementation of a BT
struct BinaryTree{
int data;
BinaryTree* leftChild, RightChild;
};
5
Picture of a binary tree
b c
d e f
g h i j k
l
6
Size and depth
The size of a binary tree is the
number of nodes in it
a This tree has size 12
g h i j k
The depth of a binary tree is
the depth of its deepest node
l
This tree has depth 4
7
Balance
a a
b c b
c e
d e f g
d f
h i
g h
A balanced binary tree
i j
An unbalanced binary tree
8
Tree traversals
A binary tree is defined recursively: it consists of a root, a left
subtree, and a right subtree
To traverse (or walk) the binary tree is to visit each node in the
binary tree exactly once
Since a binary tree has three “parts,” there are six possible ways
to traverse the binary tree:
9
Preorder traversal
10
Inorder traversal
11
Postorder traversal
12
Tree traversals using “flags”
The order in which the nodes are visited during a tree traversal
can be easily determined by imagining there is a “flag” attached
to each node, as follows:
A A A
B C B C B C
D E F G D E F G D E F G
14
Binary Tree Traversals
Inorder traversal (recursive version)
Typedef BinaryTree * tree_pointer;
output A / B * C * D + E
:
ptr
L
V
R
15
Binary Tree Traversals
Preorder traversal (VLR) (recursive version)
output + * * / A B C D E
:
V
L
R
16
Binary Tree Traversals
Postorder traversal (LRV) (recursive version)
output A B / C * D * E +
:
L
R
V
17
Binary Tree Traversals
Iterative inorder traversal
we use a stack to simulate recursion
5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+
L
V
19
Binary Search Tree Property
Stored data must satisfy
the binary search tree
property. 56
y in left subtree of x, then
data[y] data[x].
y in right subtree of x,
26 200
then data[y] data[x].
18 28 190 213
12 24 27
20
Binary search in an array
Look at array location (lo + hi)/2
Searching for 5:
(0+6)/2 = 3
Using a binary
hi = 2;
(0 + 2)/2 = 1 lo = 2; search tree
(2+2)/2=2
7
3 13
0 1 2 3 4 5 6
2 3 5 7 11 13 17 2 5 11 17
21
Tree Search
BinaryTree* Tree-Search(BinaryTree**xx,,int
BinaryTree*Tree-Search(BinaryTree intkk))
1.{{ifif((((xx==
1. NULL)||||((kk==
==NULL) ==x->data)
x->data)))
2. returnxx;;
2. return
3. ifif((kk<<x->data)
3. x->data)
4.
4. return Tree-Search(x->leftChild
returnTree-Search( x->leftChild,,kk))
5.
5. else elsereturn Tree-Search(x->rightChild
returnTree-Search( x->rightChild,,kk))
}}
56
26 200
18 28 190 213
12 24 27
22
Iterative Tree Search
BinaryTree*
BinaryTree*Iterative-Tree-Search
Iterative-Tree-Search
(BinaryTree**xx,,int
(BinaryTree intkk))
56
1. while((xx!=
1.{{while !=NULL
NULL&& &&kk!=
!=x->data)
x->data)
26 200 2. {{ifif((kk<<x->data)
2. x->data)
3.
3. thenxx==x->leftChild;
then x->leftChild;
18 28 190 213
4.
4. elsexx==x->rightchild;}
else x->rightchild;}
5. returnxx;;
5. return
}}
12 24 27
23
Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
Tree-Minimum(xx))
Tree-Minimum( Tree-Maximum(xx))
Tree-Maximum(
1. whilex->left
1. while x->left!=NULL
!=NULL 1. whilex->right
1. while x->right==NULL
NULL
2. xx==x->left
2. x->left;; 2.xx==x->right;
2. x->right;
3. returnxx
3. return 3. returnxx
3. return
24
Inserting the entry M into the list
B, E, G, H, J, K, N, P stored as a tree
25
Inserting the entry M into the list
B, E, G, H, J, K, N, P stored as a tree
26
A procedure for inserting a new entry in
a list stored as a binary tree
27
Copying a binary tree
28
Other traversals
The other traversals are the reverse of these three
standard ones
That is, the right subtree is traversed before the left subtree is
traversed
Reverse preorder: root, right subtree, left subtree
Reverse inorder: right subtree, root, left subtree
Reverse postorder: right subtree, left subtree, root
29
30