Professional Documents
Culture Documents
This lecture prepared by the instructors at the University of Manitoba in Canada and has been modified by Dr. Ahmad Reza Hadaegh
1
Trees
A tree is a linked data structure which reflects some hierarchical structure There are several varieties of trees
Each with its own characteristics v A family of related data structures
Trees
u
B E
D H
J
3
Trees
u
Trees
A B
C
F I G
D H J
Trees
u
Trees
A B E F I
u u
C G
D H J
u
u u
The parent of G is D and the parent of E is B The children of A are B, C, and D B, C, and D are sibling The ancestors of J are G, D, and A The descendants of D are G, H, I and J
Trees
u
Binary Trees
u
Binary trees
A binary tree is a tree in which no node has more than two children
A B D E G C F H
9
10
C
E G F H
11
E
C F G H
12
F
E G H
13
A
B
F
G H
14
Ordered binary trees are often referred to as binary search trees (BST)
They tend to be a co-operative structure on which to work
15
Searching a BST
u
Searching a BST
Algorithm v Start with root node v if search key = node key then stop v else if search key < node key then go left v else go right v Stop if at any time during the search, you fall off the tree, stop
16
Searching a BST
u
D B
E
C F G H
17
Searching a BST
u
D B
E
C F G H
18
Searching a BST
Given an existing BST, searching for a key is simple
Effectively a binary search v Is it as efficient as a binary search? Algorithm is recursive, but can be trivially implemented iteratively Could be implemented as follows:
19
Searching a BST
bool Search(nodeptr curr, char key) {
bool found; nodeptr curr;
curr = root; found = false; while ((curr != NULL) && (! found)) if (curr->data == key) found = true; else if (key < curr->data) curr = curr->left; else curr = curr->right; return found; }
20
Traversing a Tree
u
21
Pre-order Traversal
u
Pre-order traversal
Visit the current node Perform a pre-order traversal of the left sub-tree Perform a pre-order traversal of the right sub-tree A naturally recursive algorithm v Easy to program recursively Not so easy to program iteratively
22
Pre-order Traversal
u
23
Post-order Traversals
Post-order traversal
Perform a post-order traversal of the left sub-tree Perform a post-order traversal of the right sub-tree Visit the current node
24
In-order Traversals
In-order traversal
Perform a in-order traversal of the left sub-tree Visit the current node Perform a in-order traversal of the right sub-tree
25
Insertions
u
Binary trees are normally created by inserting nodes into an initially empty tree
If tree empty, first insert will create the root node Otherwise, tack new node onto the (unique) appropriate node that has less than 2 children This will require a search routine
26
Insertions
u
27
Insertions
u
28
Insertions
u
void r_insert (nodeptr& if (root == null) root = newnode; else if (newnode->data r_insert (root->le else r_insert (root->rig }
29
Insertions
u
30
Insertions
u
19
31
Insertions
u
19
32
Insertions
u
8
5 12
19
23
33
Insertions
// This procedure implements a recursive insert. // Probably better to do this iteratively. Why? void r_insert (nodeptr& root, nodeptr newnode) { if (root == null) root = newnode; else if (newnode->data < root->data) r_insert (root->left, newnode); else r_insert (root->right, newnode); }
34
Deletions
This is an optional topic. I strongly recommend you to learn it but it will not be in the exam.
35
Deletions
u
When deleting from a BST, the algorithm must often reorganize tree while maintaining the inherent ordering
Three cases to consider Deleting a leaf node Easy -- detach leaf node from tree by setting its parents pointer to nil
36
Deletions
u
Delete 12
15
8
5 12
19
23
37
Deletions
u
Delete 12
15
8
5 12
19
23
38
Deletions
u
Done!
15
8
5
19
23
39
Deletions
Deleting a node with a single child Easy -- replace the node to be deleted with its child
40
Deletions
u
Delete 19
15
8
5 12
19
23
41
Deletions
u
Delete 19
15
8
5 12
19
23
42
Deletions
u
Done!
15
8
5 12
23
43
Deletions
Deleting
a node with two children A little more difficult Both subtrees of the deleted node must be linked back into the tree while retaining order
44
Deletions
u
Delete 21
12 8 21 10 17
23
14
19
22
26
45
Deletions
u
Delete 21
12 8 21 10 17 These nodes must be properly linked back into the tree
23
14
19
22
26
46
Deletions
u
47
Deletions
u
Delete 21
12 8 7 10 17 21 23
Replace the deleted node with one of its children, say the left child (i.e. node 17)
14
19
22
26
48
Deletions
u
Delete 21
12 8 7 10 17 23
14
19
22
26
49
Deletions
u
Delete 21
12 8 7 10 17 23 Place it (node 23) on the right of the largest nose of the new left subtree
14
19
22
26
50
Deletions
u
Done!
12
8
7
17
10
14
19
23
22
23
51
Deletions
Another way
Locate the in-order successor of the node to be deleted (the node that contains the next highest value) v To locate the in-order successor, go right, then go left as far as possible Replace the contents of the node to be deleted with the contents of its in-order successor Delete the successor node v This algorithm is possible since the in-order successor node will never have more than one child
52
Deletions
u
Delete 21
12 8 21 10 17
23
14
19
22
26
53
Deletions
u
Delete 21
12 8 21 10 17 Replace the contents of the node to be deleted with the contents of its in-order successor
23
14
19
22
26
54
Deletions
u
Delete 21
12 8 22 10 17
23
14
19
21
26
55
Deletions
u
Delete 21
12 8 22 10 17 Delete the successor node
23
14
19
21
26
56
Deletions
u
Delete 21
12 8 22 10 17 Delete the successor node
23
14
19
21
26
57
Deletions
u
Done!
12 8 7 10 17 22 23
14
19
26
58
Deletions
59