27
Trees Eric McCreath

Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

Trees

Eric McCreath

Page 2: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

2

OverviewIn this lecture we will explore:

general trees,

binary trees,

binary search trees, and

AVL and B-Trees.

Page 3: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

3

TreesTrees are recursive data structures.

They are useful for:

representing items that have a tree structure (such as xmldocuments, mathematical expressions, natural language,programming languages),

operating systems structures (file directory structure, diskallocation, processes),

representing tables,

search a space of possible states (used in planning, machinelearning, or game playing),

representing sets, and

some algorithms.

Page 4: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

4

TreesA tree is either :

a leaf node with no children, or

an inner node with one or more children, these children are alsotrees.

An empty tree is a tree with no nodes. (as it is sometimes useful tobe able to represent a tree that is completely empty)

Nodes of a tree will normally contain some data.

A

B D E

C F G

Page 5: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

5

Trees - TerimnologyAn edge of a tree connects a parent node to a child node. These

are directed in edges. Nodes will have exactly 1 parent (exceptthe root node which has none).

The root node of a tree is the node from which all other nodesdescend from.

Sibling nodes share the same parent.

The size of a tree is the number of nodes that make up the tree(both leaf and inner nodes).

The path from the root node to node X is an ordered list of nodesthat follows the edges from the root node to X.

The the depth of node X is the number of edges in the path fromthe root node to node X.

The height of a tree is the maximum depth over all the nodes ofthe tree.

Page 6: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

6

Trees - TerimnologyA, B, C, D, E, F, G are the nodes of the tree. C, D, F and G are

leaf nodes. A, B, and E are inner nodes.

A is the root node of the tree.

E is the parent of F and G. F and G and children of E.

B, D and E are siblings. In this case B is the eldest and E is theyoungest sibling. C and F are not siblings.

The tree has size 7 and height 2.

The path from A to C is A->B->C and C has depth 2.

A

B D E

C F G

Page 7: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

7

TraversalAlgorithms on trees often involve traversing (visiting) the nodes ofthe tree. Examples include:

operating on every data element in the nodes of the tree,

transforming the tree into another data structure,

search for a node,

counting the nodes, and

finding the height of a tree.

Nodes may be traversed using different approaches or

orders. Two common approaches are:

Depth First, or

Breadth First.

Page 8: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

8

Depth-first Traversal (BFT)

Depth-first traversal can be done using simple recursion.Basically this involves recursively calling depth-first traversal oneach of the children.

The below tree would be traversed with the following order:

A, B, C, (B), (A), D, (A), E, F, (E), G, (E), (A)

A

B D E

C F G

Page 9: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

9

Breadth-first Traversal (DFT)

Breadth-first traversal involves visiting all the nodes at depth 0,then all the nodes at depth 1, all the nodes at depth 2, etc.

In the below example the order would be A, B, D, E, C, F, and G.

A

B D E

C F G

Page 10: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

10

Breadth-first Traversal (DFT)

Implementing a BFT is a little more complex than DFT, and willgenerally use more memory. An implementation will normallyinvolve maintaining a queue of nodes to visit. When a node isvisited the node's children are added to the end of the queue. Thenext node to visit is taken from the front of the queue. The iscontinued until the queue is empty.

A

B D E

C F G

Page 11: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

11

Binary TreeA binary tree is a tree with each node having at most 2 children.We often call the children of an inner node the 'left' or 'right' child.

A

B

C

E

F G

Page 12: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

12

DFT of a Binary TreeDFT of a binary tree can be done recursively.

A tree can be traversed by:

visit the root,

traverse the left subtree,

traverse the right subtree,

This is pre-order traversal.

The order traversed is : A, B, C, E, F, G.

A

B

C

E

F G

Page 13: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

13

DFT of a Binary TreeThere is also in-order traversal:

traverse the left subtree,

visit the root,

traverse the right subtree.

The order traversed in the below tree is : C, B, A, F, E, G.

A

B

C

E

F G

Page 14: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

14

DFT of a Binary TreeThere is also post-order traversal.

traverse the left subtree,

traverse the right subtree,

visit the root,

The order traversed in the below tree is : C, B, F, G, E, A.

A

B

C

E

F G

Page 15: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

15

Binary Search TreeIf the data stored in the nodes (or a key which forms part of the

data) can be ordered then the nodes of the binary tree can beorganized as a binary search tree.

A Binary Search Tree can be used to store a set

of elements.

e.g. the set can be store via the below binarysearch tree:

5

3 13

6 14

Page 16: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

16

Binary Search TreeA Binary Search Tree can be an efficient way of storing a table.

So the table:

could be stored using the binary search tree:

Isaac, 6

Hugh,10

Eric,44

Matt,8

John,12

Page 17: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

17

Binary Search TreeA Binary Search Tree is a binary tree where either:

the root has no children, or

the following three conditions hold:

all the nodes in the left subtree are less than the root

node,

all the nodes in the right subtree are greater than the

root node,

both children are binary search trees.

5

3 13

6 14

5

3 13

4 14

Page 18: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

18

Binary Search Tree - Search

Elements can be searched for recursively:

if the element we are searching for is at the root then we havefound it,

if the element we are searching for is less than the root thensearch the left subtree,

else (the element must be greater than the root element) searchthe right subtree.

If the tree is balanced than this is O(lg n) where n is the numberof elements in the tree.

5

3 13

6 14

Page 19: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

19

Binary Search Tree - AddElements can be added to the binary search tree by recurring

down the tree (basically the same as the search method) and thenadded as a leaf node at the appropriate position.

This is also O(lg n) where n is the number of

elements in the tree. (assuming the tree is

balanced)5

3 13

6 14

Page 20: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

20

Binary Search Tree - Delete

If the element to be removed is a leaf node then it can just beremoved.

10

4

1 6

18

5 8

13 21

10

4 18

1 6 21

5 8

delete 13

Page 21: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

21

Binary Search Tree - Delete

If the element to be removed is an inner node with only one childthan it can be deleted and the child subtree of the deleted elementcan be moved into the place of the deleted element's subtree.

10

4

1 6

18

5 8

21

10

4

1 6

21

5 8

Delete 18

Page 22: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

22

Binary Search Tree - Delete

If the element to be removed is an inner node and it has twochildren then the minimum element from the right sub-tree can bemoved into the deleted elements place. (or the maximum of the leftsub-tree)

Delete is O(lg n) where n is the number of nodes in the tree.(assumes a balanced tree)

10

4

1 6

18

5 8

13 21

4 18

1 6

13

21

5

Delete 10

8

Page 23: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

23

Binary Search Tree - Balanced

The shape of the tree will depend on the order elements areadded. If elements are added in ascending (or descending) orderthan the binary search tree will look (and perform) like a linked list.

A tree with elements can be placed in a tree with height

. If a binary search tree is within some constant range awayfrom this optimum height then it would be considered balanced(note different algorithms will define "balanced" in different ways).

There is a number of approaches to efficiently maintain a"balanced" tree as elements are added. (AVL Tree's, Red-BlackTree's)

Page 24: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

24

AVL TreesAVL Trees are balanced binary search trees. They maintain the

property that for each node the height of the node's children differby at most one.

This is achieve by checking that the tree remains balances afterinsertion. And if the tree become unbalanced then nodes are"rotated" to both maintain the binary search tree properties andrestore the tree to a balanced state.

This helps maintain good performance for searching the tree(even in the worst case), without degrading the performance ofinsertion or deletion operations.

Page 25: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

25

B-TreesB-Tree are a generalization of binary search trees. However,

they are not binary trees. So rather than having a single key ineach node more than one key can be placed in each node. Thesekeys are ordered within the nodes and partition the key values ofthe subtrees. So if there are 4 keys in a node then there would be5 subtrees.

The number of keys within an inner node will be maintained inthe range to , so the number of subtrees a node will have

will range between and . All the leaf node arekept at the same depth. This makes for good balance.

B-trees (and their variants) are used extensively for storingtables on hard disks as, in comparison to a binary search tree,they reduce the depth to lookup an element. This in tern reducesthe number of disk blocks needed to be read.

Page 26: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

26

B-TreesThe below tree is a b-tree whcih stores the set {1,3,8,9,12,15,21}.

1 3

8

9 12

15

21

Looking up elements within a b-tree can be implemented usingusing a recursive approach that is similar to that of a binary searchtree. However, insertion and deletion are a little more tricky as oneneeds to maintain the constrains to help keep the tree balanced.

Page 27: Trees - Research School of Computer Science · AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for: representing items that have a tree structure (such

27

Example Exam QuestionsDefine a binary tree. What are some different approaches for

traversing a binary tree?

What is the differences and similarities between binary searchtrees and b-trees.

Say you are given an implementation of a binary search treewhich includes the classes and the implementation of looking upelements. Complete the implementation by adding the "delete"operation.

Write a method that would determine if a binary tree is a binarysearch tree.