1 cse 1342 programming concepts trees. 2 basic terminology trees are made up of nodes and edges. a...

26
1 CSE 1342 Programming Concepts Trees

Upload: oswald-carson

Post on 31-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

1

CSE 1342 Programming Concepts

Trees

Page 2: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

2

Basic Terminology• Trees are made up of nodes and edges.• A tree has a single node known as a root.

– The root is typically drawn as the highest node in the tree

• Every node other than the root is connected to some other node called the parent.

P

C

parent

child

edgenode

root

Page 3: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

3

Basic Terminology• A node can have no more that one parent• A node can have zero or more children• Parents are typically drawn above their children• A tree is connected in that if we start at any node

other than the root and move to its parent and so on, we will eventually arrive at the root of the tree.

P

C2C1 Cn. . .

Page 4: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

4

Paths, Ancestors and Descendants

• A node by itself is considered a tree.• A node and all of its descendants are considered a

tree.• An empty tree (root pointer == NULL) is

considered a tree.

n1

n3n2 n4

n5 n6 n7

root

Page 5: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

5

Paths, Ancestors, and Descendants

• Trees are built upon recursive definitions.• Any node an all of its descendants is considered a

subtree.

n1

n3n2 n4

n5 n6 n7

root

Sub-tree

Page 6: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

6

Paths, Ancestors and Descendants

• Nodes with no descendants are leaf nodes.• Node n1 (the root) is an ancestor of all remaining

nodes in the tree.

n1

n3n2 n4

n5 n6 n7

Leaf nodes

Page 7: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

7

Paths, Ancestors and Descendants

• Nodes n2 .. n7 are all descendants of n1.• The path from n4 to n7 has a length of 1.

– This is equal to the number of nodes on the path - 1.

n1

n3n2 n4

n5 n6 n7

Descendants of n1

Page 8: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

8

Paths, Ancestors and Descendants

• The height of a node is the length of the longest path from the node to a leaf.– The height of the tree is 2, the height of the root.– The height of n1 is 2.– The height of n4 is 1– The height on n6 is 0.

n1

n3n2 n4

n5 n6 n7

Height of tree is 2

Page 9: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

9

Paths, Ancestors and Descendants

• The depth of a node is the length of the longest path from the root to the node.– The depth of n5 is 2.

– The height of n5 is 0.

– The depth of n3 is 1.

– The height of n3 is 0.

n1

n3n2 n4

n5 n6 n7

The depth of n5 is 2

Page 10: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

10

Binary Trees

• A binary tree node can have at most two children, a left child node and a right child node.

• Typical node definition for a binary tree:

struct Node {

int data;

Node *leftChild, *rightChild;

};

n1

n2 n3

Left child Right child

Page 11: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

11

Binary Trees

struct Node {

int data;

Node *leftChild, *rightChild;

};

• The pointer fields are used to implement the edges of the tree.

• The data in the node may be simple or complex.– One of the data fields must serve as a key field.

Page 12: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

12

Binary Trees• A general recursive scheme for traversing a binary

tree is:{

action 1;

recursive call on left subtree

action 2;

recursive call on right subtree

action 3;

}

• Not all three actions are required.• The right subtree can be visited before the left

subtree.

Page 13: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

13

Binary Search Trees

• A binary search tree is a labeled binary tree in which the following properties hold at every node x in the tree:– All nodes in the left subtree of x have labels (values)

less than the value of x.

– All nodes in the right subtree of x have labels (values) greater than the value of x.

Page 14: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

14

Binary Search Trees

• Order of insertion: 9, 3, 1, 4, 7, 6, 10 results in the above tree.

9

7

3 10

1 4

6

Page 15: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

15

Binary Search Trees

• Changing the order of insertion to: 6, 4, 1, 7, 3, 9, 10 results in the above tree.

6

10

4 7

1 9

3

Page 16: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

16

Binary Search Trees• The following recursive algorithm displays the

contents of a binary tree in ascending order.

void ascending (Node *t) {

if (t != NULL) {

ascending(t->leftChild);

(1) cout << t-> data;

ascending(t->rightChild);

(2) }

}

Page 17: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

17

Binary Search Trees• A binary search tree must support the following

operations:– Insertion of new nodes.

– Deletion of existing nodes.

– Verification that a node is/isn’t in the tree

• A data set upon which we can execute an insert, delete, and look-up is called a dictionary, no matter how the set is implemented or what it is used for.

Page 18: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

18

Search Tree Verification//This function returns a 1 (true) if x is in the tree and 0 (false)

//if x is not in the tree

int lookUp (elementType x, Node *t) {

if (t = = NULL)

return 0;

else if (x = = t->element)

return 1;

else if (x < t->element)

return lookUp(x, t->leftChild);

else // x must be > t->element

return lookUp(x, t->rightChild);

}

Page 19: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

19

Search Tree Insertion//This function inserts a new x into the treeNode* insert (elementType x, Node *t) {

if (t = = NULL) {

t = new Node;

t->element = x;

t->leftChild = NULL;

t->rightChild = NULL;

}

else if (x < t->element)

(1) t->leftChild = insert(x, t->leftChild);

else if (x > t->element)

(2) t->rightChild = insert(x, t->rightChild);

return t;

}

Page 20: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

20

Search Tree Deletion• There are 3 cases that must be supported with a

binary search tree deletion.– Case 1: Node to be deleted (b) is a leaf

10

20

a

b

delete a->rightChild;

a->rightChild = NULL;

10 a

(before) (after)

Page 21: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

21

Search Tree Deletion– Case 2: Node to be deleted (b) has only 1 subtree

tempPtr = b->rightChild;

delete a->rightChild;

a->rightChild = tempPtr;

30

10

20

25 40

a

b 30

10

25 40

a

(before) (after)

c

d e

c

d e

Page 22: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

22

Search Tree Deletion– Case 3: Node to be deleted (b) has 2 children

• Replace the node to be deleted (b) with the smallest node in the right subtree of b.

40

10

30

35 50

a

b(before)

37

20

15

c d

e f g

h

40

10

35

37 50

a

f(after)

20

15

c d

e h g

Page 23: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

23

Search Tree Deletion– Case 3: Node to be deleted (b) has 2 children

40

10

30

35 50

a

b(before)

37

20

15

c d

e f g

h

40

10

35

37 50

a

f(after)

20

15

c d

e h g

Smallest node will never have a left child.

Note how the right subtree of the smallest right subtree was reattached.

Page 24: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

24

Deletion Algorithm

Page 25: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

25

Height-Balanced Binary Trees

• Trees should be kept as short and bushy as possible.

• Order of insertion: 6, 4, 9, 10, 7, 5, 1 results in a height-balanced tree. This is a best case insertion scenario resulting in a search time of O(log n).

6

10

4 9

1 75

Page 26: 1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is

26

Skewed Binary Trees

Order of insertion: 1, 3, 4, 6, 7, 9, 10 results in a skewed tree. This is a worst case insertion scenario resulting in a search time of O(n).

6

10

4

9

1

7

3