03 DataStructures BinaryTree
03 DataStructures BinaryTree
The two primary operations on linked lists are insertion and deletion.
Stacks
15 99
11 88
Special Types of Trees
• Full - Every node has exactly 4
2 i
2 k 1
1
Let n be the number of vertices in
i1
a binary tree T of
height k. Then:
1 A
B B
Skewed Binary Tree
2
C B C
D 3 D E F G
E 5
4 H I
Binary Tree representation
Sequential representation
If a complete binary tree with n nodes (depth =log n )
is represented sequentially, then for any node with
index i, 1<=i<=n, we have:
parent(i) is at i/2 If i=1, i is at the root and has no parent.
leftChild(i) is at 2i. If 2i >n, then i has no left child.
rightChild(i) is at 2i+1 . If 2i +1 >n, then i has no right child.
Linked Representation
data
left right
Traversing a Binary Tree
• Inorder :
– Root is printed between the values of its left and
right subtrees: left, root, right
• Preorder :
– root printed first: root, left, right
• Postorder :
– root printed last: left, right, root
5 Inorder: 2 3 5 5 7 9
Preorder: 5 3 2 5 7 9
3 7
2 5 9 Postorder: 2 5 3 9 7 5
Traversing a Binary Tree
INORDER (x)
1. if x NULL
2. then INORDER ( x left )
3. print ( x key)
4. INORDER (( x right )
• Running time:
– (n), where n is the size of the tree rooted at x
Binary Search Trees
• Tree representation:
– A linked data structure in which each
node is an object
• the binary-search-tree property. parent
L R
– If y is in left subtree of x, key data
then key [y] < key [x]
3 7
2 5 9
Binary Search Trees
• Support many dynamic set operations
– SEARCH, MINIMUM, MAXIMUM, INSERT, DELETE…
TREE-SEARCH(x, k)
1. if x = NULL or k = x key
2. then return x
3. if k < x key
4. then return TREE-SEARCH(x left ,k )
5. else return TREE-SEARCH(x right , k )
15
6 18
3 7 17 20
2 4 13 • Search for key 13:
9 – 15 6 7 13
Finding the Minimum/Maximum in a BST
5. do x ← x right 3 7 17 22
2 4 13
6. return x
9
Insertion
• Goal: Insert value v into a binary search tree
• Idea:
– Beginning at the root, go down the tree:
– If key [x] < v move to the right child of x,
else move to the left child of x
– When x is NULL, we found the correct position
12
Insert value 13
5 18
2 9 15 19
1 3 13 17
Deletion
• Goal:
– Delete a given node z from a binary search tree
• Idea:
– Case 1: z has no children
• Delete z by making the parent of z point to NIL
15 15
5 16 5 16
3 12 20 3 12 20
z
10 13 18 23 10 18 23
6 delete 6
7 7
Deletion
• Case 2: z has one child
– Delete z by making the parent of z point to z’s child,
instead of to z
15 delete 15
z
5 16 5 20
3 12 20 3 12 18 23
10 13 18 23 10
6 6
7 7
Deletion
• Case 3: z has two children
– z’s successor (y) is the minimum node in z’s right subtree
– Replace z’s key and with y’s.
– Delete y from the tree (via Case 1 or 2)
6 15 15
delete z
5 16 6 16
3 12 20 3 12 20
10 13 18 23 10 13 18 23
y 6 7
7
The Heap Data Structure
• A heap is a complete binary tree with the following two
properties:
– Structural property: complete. all levels are full, except
possibly the last one, which is filled from left to right
– Order (max-heap) property: for any node x, Parent(x) ≥ x
7 4
5 2
From the heap property, it follows that:
Heap The root is the maximum element of the heap!”
Array Representation of Heaps
• A heap can be stored as an array A.
– Root of tree is A[1]
– Left child of A[i] = A[2i]
– Right child of A[i] = A[2i + 1]
– Parent of A[i] = A[ i/2 ]
– Heapsize[A] ≤ length[A]
• The elements in the subarray
A[(n/2+1) .. n] are leaves
26
Heap Operations
• Maintain/Restore the max-heap property
– Shift up / shift down
• Insert / delete
• Create a max-heap from an unordered array
• Sort an array in place
27
Shift-up
Suppose the key stored in the 10th position of the heap shown
in Fig. 1 is changed from 5 to 25.
move H[i] up along the unique path from H[i] to the root until its proper
location along this path is found.
At each step along the path, the key of H[i] is compared with the key of its
parent H[[ i/2 ].
Procedure SHIFT-UP
Input: An array H[1..n] and an index i between 1 and n.
Output: H[i] is moved up, if necessary, so that it is not larger than its
parent.
1. done = false
2. if i = 1 then exit {node i is the root}
3. repeat
4. if key(H[i]) > key(H[ [i/2]]) then interchange H[i] and H[ [i/2]]
5. else done = true
6. i = [i/2]
7. until i = 1 or done
Shift-down
Suppose we change the key 17 stored in the second position of the heap
shown in Fig. 1 to 3.
At each step along the path, its key is compared with the maximum of
the two keys stored in its children nodes (if any).
Procedure SHIFT-DOWN
Input: An array H[1..n] and an index i between 1 and n.
Output: H[i] is percolated down, if necessary, so that it is
not smaller than its children.
1. done = false
2. if 2i > n then exit {node i is a leaf}
3. repeat
4. i = 2i
5. if i + 1 ≤ n and key(H[i + 1]) > key(H[i]) then i =i + 1
if key(H[ [i/2]]) < key(H[i]) then
interchange H[i] and H[ [i/2] ]
7. else done = true
8. end if
9. until 2i > n or done
Insertion into a Heap
• Increase the size of H by 1.
• append x to the end of H
• shift x up, if necessary (to satisfy heap property).
Algorithm INSERT
Input: A heap H[1..n] and a heap element x.
Output: A new heap H[1..n + 1] with x being one of its elements.
1. n =n + 1 {increase the size of H}
2. H[n] = x
3. SHIFT-UP(H , n)
Algorithm DELETE
Input: A nonempty heap H[1..n] and an index i between 1 and n.
Output: A new heap H[1..n - 1] after H[i] is removed.
1. x H[i]; y H[n]
2. n n - 1 {decrease the size of H}
3. if i = n + 1 then exit {done}
4. H[i] y
5. if key(y) ≥ key(x) then SHIFT-UP(H , i)
6. else SHIFT-DOWN(H , i)
7. end if
Time Complexity is O(logn)
Building a Heap
• Convert an array A[1 … n] into a max-heap (n = length[A])
• The elements in the subarray A[(n/2+1) .. n] are leaves
• Apply shift-down on elements between 1 and n/2
4
BUILD-MAX-HEAP(A) 2 3
1 3
1. n = length[A] 4 5 6 7
2 16 9 10
2. for i ← n/2 downto 1
8 9 10
14 8 7
3. do shift-down(A, i, n)
:A 4 1 3 2 16 9 10 14 8 7
34
Example: A 4 1 3 2 16 9 10 14 8 7
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8 2 9 10
16 9 10 8 14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
35
Running Time of BUILD MAX HEAP
BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do shift-down(A, i, n) O(lgn)
h1 = 2 i=1 21
h2 = 1 i=2 22
h3 = 0 i = 3 (lgn) 23
38
Example: A=[7, 4, 3, 1, 2]
Shift-down(A, 1, 1)
39
Algorithm HEAPSORT