You are on page 1of 59

Trees

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

A tree gets its name from its structure which is tree-like


A tree has a root, branches, leaves
Root node A C F I Leaf nodes G Branches

B E

D H

J
3

Trees
u

A tree consists of a number of connected nodes


The nodes are organized in a top-down hierarchical manner

A tree is an instance of a general structure called a graph


A graph consists of vertices (nodes) and edges (branches), but is not necessarily hierarchical in nature v Sometimes tree and graph terminology is interchanged
4

Trees
A B

C
F I G

D H J

This tree has


10 nodes, 9 branches, 1 root node, and 6 leaf nodes
5

Trees
u

Other defining characteristics


If node A points to node B, then A is the parent of B v Furthermore, B is the child of A Children of the same parent are siblings All nodes beneath a given node (children, children of children, ) are descendants of that node All nodes on the path between the root and a given node are ancestors of that node
6

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

Some typical operations upon a tree


Insert v Add a new node into a tree Delete v Remove a node from a tree Search v Find a node (if it exists) that contains a given key Traverse v Systematically examine (an operate on) all nodes in a tree
8

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

Ordered Binary Trees

Ordered binary trees


An ordered binary tree is a binary tree in which

left descendants < given node < right descendants


applied recursively throughout the tree structure

10

Ordered Binary Trees


u

The following binary tree is not ordered


A B

C
E G F H
11

Ordered Binary Trees


u

The following binary tree is ordered


D B

E
C F G H
12

Ordered Binary Trees


u

The following binary tree is also ordered


D C

F
E G H
13

A
B

Ordered Binary Trees


u

The following binary tree is also ordered


A B C D E

F
G H
14

Ordered Binary Trees

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

Find node with key C

D B

E
C F G H
17

Searching a BST
u

Find node with key X

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

// Think about doing the search recursively

Traversing a Tree
u

Tree traversal is a common operation


In a tree traversal, every node is visited once There are three traversal orders of general interest v Pre-order, post-order, and in-order traversals

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

A pre-order traversal could be implemented as follows:


void postOrder (nodeptr sroot) { if (sroot != NULL) { postOrder (sroot->left); postOrder (sroot->right); cout << sroot->data; } }

Think about the run-time stack frames created

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

Insert 15, 8, 12, 19, 5, and 23 into an empty BST

27

Insertions
u

Insert 15, 8, 12, 19, 5, and 23 into an empty BST


root 15

28

Insertions
u

Insert 15, 8, 12, 19, 5, and 23 into an empty BST


? 15 8 root

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

Insert 15, 8, 12, 19, 5, and 23 into an empty BST


15 8 12

30

Insertions
u

Insert 15, 8, 12, 19, 5, and 23 into an empty BST


15 8 12

19

31

Insertions
u

Insert 15, 8, 12, 19, 5, and 23 into an empty BST


15 8 5 12

19

32

Insertions
u

Insert 15, 8, 12, 19, 5, and 23 into an empty BST


15

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

Deleting is a little more difficult


Why?

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

Two possible algorithms


Replace the deleted node with one of its children, say the left child (i.e. node 17) v The remaining right child (i.e. node 23) must now be linked back into the tree Place it on the right of the largest node of the new left subtree

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

Deletion code left as an exercise

59