trees

54
General Tree Concepts Binary Trees Data Structures and Data Structures and Algorithms Algorithms

Upload: aamir2686

Post on 10-May-2017

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees

General Tree ConceptsBinary Trees

Data Structures and AlgorithmsData Structures and Algorithms

Page 2: Trees

2

Outline

2

Tree Data Structure– Examples– Definition– Terminology

Binary Tree– Definitions– Examples– Tree Traversal

Binary Search Trees

– Definition

Page 3: Trees

3 3

Linear Lists and Trees Linear lists (arrays, linked list)are useful for serially

ordered data– (e1,e2,e3,…,en)– Days of week– Months in a year– Students in a class

Trees are useful for hierarchically ordered data– Khalid’s descendants– Corporate structure– Government Subdivisions– Software structure

Page 4: Trees

4

Examples of trees directory structure family trees:

– all descendants of a particular person– all ancestors born after year 1800 of a particular person

evolutionary tress (also called phylogenetic trees) albegraic expressions

Page 5: Trees

5 5

Khalid’s Descendants

What are other examples of hierarchically ordered data?

Page 6: Trees

6 6

Property of a Tree A tree t is a finite nonempty set of nodes One of these elements is called the root Each node is connected by an edge to some other

node A tree is a connected graph

– There is a path to every node in the tree There are no cycles in the tree.

– It can be proved that a tree has one less edge than the number of nodes.

Usually depicted with the root at the top

Page 7: Trees

7

Is it a Tree?

yes!NO!

All the nodes are not connected

NO!There is a cycle

and an extra edge (5 nodes and 5 edges)

yes! (but not a binary tree)

yes! (it’s actually the same graph as the blue one) – but

usually we draw tree by its “levels”

Page 8: Trees

8 8

Subtrees

Page 9: Trees

9 9

Tree Terminology The element at the top of

the hierarchy is the root. Elements next in the

hierarchy are the children of the root.

Elements next in the hierarchy are the grandchildren of the root, and so on.

Elements at the lowest level of the hierarchy are the leaves.

Page 10: Trees

10 10

Other Definitions Leaves, Parent, Grandparent, Siblings,

Ancestors, Descendents

Leaves = {Hassan,Gafar,Sami,Mohmmed}

Parent(Musa) = KhalidGrandparent(Sami) = Musa

Siblings(Musa) = {Ahmed,Omer}

Ancestors(Hassan) = {Amed,Khalid}

Descendents(Musa)={Fahad,Sami}

Page 11: Trees

11 11

Levels and Height Root is at level 0 and its children are at level 1.

level 0

level 1

level 2

level 3

Page 12: Trees

12 12

Degree of a node Node degree is the number of children it has

Page 13: Trees

13 13

Tree Degree Tree degree is the maximum of node degrees

tree degree = 3

Page 14: Trees

14

Measuring Trees The height of a node v is the number of nodes on the longest

path from v to a leaf– The height of the tree is the height of the root, which is the number of

nodes on the longest path from the root to a leaf The depth of a node v is the number of nodes on the path

from the root to v– This is also referred to as the level of a node

Note that there are slightly different formulations of the height of a tree

– Where the height of a tree is said to be the length (the number of edge) on the longest path from node to a leaf

Page 15: Trees

15

Trees - Example

E

R

T

ELPM

EA

SA

root

Leaves or terminal nodes

Child (of root)

Depth of T: 2

Height of T: 1

Level

0

1

3

2

Page 16: Trees

16

Tree Terminology Example

16

A is the root node B is the parent of D and E C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The depth, level, or path length of E is 2 The height of the tree is 3 The degree of node B is 2

Page 17: Trees

17

C

Tree Terminology Example

A

B C D

GE FE F G

D

G

A

leaves:

C,E,F,G

path from A to D to G

subtree rooted at B

Page 18: Trees

18

Tree Representation

Class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; }

Page 19: Trees

19 19

Binary Trees A finite (possibly empty) collection of elements A nonempty binary tree has a root element and the

remaining elements (if any) are partitioned into two binary trees

They are called the left and right subtrees of the binary tree A

B C

GD E

left subtree of A

H I J

F

right subtree of C

right child of A

Page 20: Trees

20 20

Difference Between a Tree & a Binary Tree A binary tree may be empty; a tree cannot be empty. No node in a binary tree may have a degree more than 2,

whereas there is no limit on the degree of a node in a tree. The subtrees of a binary tree are ordered; those of a tree

are not ordered.

a

b c

a

c b

- different when viewed as a binary tree - same when viewed as a tree

Page 21: Trees

21 21

Binary Tree for Expressions

Page 22: Trees

22

Height of a Complete Binary Tree

L 0

L 1

L 2

L 3

At each level the number of the nodes is doubled. total number of nodes: 1 + 2 + 22 + 23 = 24 - 1 = 15

Page 23: Trees

23

Nodes and Levels in a Complete Binary Tree

Number of the nodes in a tree with M levels:

1 + 2 + 22 + …. 2M = 2 (M+1) - 1 = 2*2M - 1

Let N be the number of the nodes.

N = 2*2M - 1, 2*2M = N + 12M = (N+1)/2M = log( (N+1)/2 )

N nodes : log( (N+1)/2 ) = O(log(N)) levelsM levels: 2 (M+1) - 1 = O(2M ) nodes

Page 24: Trees

24 24

Binary Tree Properties

1. The drawing of every binary tree with n elements, n > 0, has exactly n-1 edges.

2. A binary tree of height h, h >= 0, has at least h and at most 2h-1 elements in it.

Page 25: Trees

25 25

Binary Tree Properties

3. The height of a binary tree that contains n elements, n >= 0, is at least (log2(n+1)) and at most n.

minimum number of elements maximum number of elements

Page 26: Trees

26 26

Full Binary Tree A full binary tree of height h has exactly 2h-1 nodes. Numbering the nodes in a full binary tree

– Number the nodes 1 through 2h-1– Number by levels from top to bottom– Within a level, number from left to right

Page 27: Trees

27 27

Node Number Property of Full Binary Tree

Parent of node i is node (i/2), unless i = 1 Node 1 is the root and has no parent

Page 28: Trees

28 28

Left child of node i is node 2i, unless 2i > n,where n is the total number of nodes.

If 2i > n, node i has no left child.

Node Number Property of Full Binary Tree

Page 29: Trees

29 29

Right child of node i is node 2i+1, unless 2i+1 > n,where n is the total number of nodes.

If 2i+1 > n, node i has no right child.

Node Number Property of Full Binary Tree

Page 30: Trees

30 30

Complete Binary Tree with N Nodes Start with a full binary tree that has at least n nodes Number the nodes as described earlier. The binary tree defined by the nodes numbered 1

through n is the n-node complete binary tree. A full binary tree is a special case of a complete

binary tree

Page 31: Trees

31 31

Example of Complete Binary Tree

Complete binary tree with 10 nodes. Same node number properties (as in full binary

tree) also hold here.

Page 32: Trees

32 32

Binary Tree Representation Array representation Linked representation

Page 33: Trees

33 33

Array Representation of Binary Tree The binary tree is represented in an array by

storing each element at the array position corresponding to the number assigned to it.

Page 34: Trees

34 34

Incomplete Binary Trees

Complete binary tree with some missing elements

Page 35: Trees

35 35

Example Binary Tree

Array index from 0Ex. To find node’s 2 left and right childLeft Child’s Index = 2 * Parent’s Index + 1Right Child’s Index = 2 * Parent’s Index + 2

To find a node’s parent use this formula: Parent’s Index = (Child’s Index – 1) / 2

Page 36: Trees

36 36

Linked Representation of Binary Tree The most popular way to present a binary tree Each element is represented by a node that has

two link fields (leftChild and rightChild) plus an element field

Each binary tree node is represented as an object whose data type is binaryTreeNode

The space required by an n node binary tree isn * sizeof(binaryTreeNode)

Page 37: Trees

37 37

Linked Representation of Binary Tree

Page 38: Trees

38 38

Node Class For Linked Binary TreeTreeNode classprivate class TreeNode { private String data; private TreeNode left; private TreeNode right;  public TreeNode(String element) { data = element; left = null; right = null; }}

Page 39: Trees

39 39

Common Binary Tree Operations Determine the height Determine the number of nodes Make a copy Determine if two binary trees are identical Display the binary tree Delete a tree If it is an expression tree, evaluate the expression If it is an expression tree, obtain the

parenthesized form of the expression

Page 40: Trees

40 40

Binary Tree Traversal A traversal of a tree is a systematic way of

accessing or "visiting" all the nodes in the tree. There are three basic traversal schemes:

preorder, postorder, inorder .In any of these traversals we always visit the left

subtree before the right

Page 41: Trees

41 41

Binary Tree Traversal Methods Preorder

The root of the subtree is processed first before going into the left then right subtree (root, left, right).

Inorder After the complete processing of the left subtree the root

is processed followed by the processing of the complete right subtree (left, root, right).

Postorder The root is processed only after the complete processing

of the left and right subtree (left, right, root). Level order

The tree is processed by levels. So first all nodes on level i are processed from left to right before the first node of level i+1 is visited

Page 42: Trees

42 42

Preorder Traversal public void preOrderPrint() { preOrderPrint(root); }  private void preOrderPrint(TreeNode tree) { if (tree != null) { System.out.print(tree.data + " "); // Visit the root preOrderPrint(tree.left);// Traverse the left subtree preOrderPrint(tree.right); // Traverse the right subtree } } 

Page 43: Trees

43 43

Preorder Example (visit = print)

H

D

B

A C E G I K M O

N

L

JF

H D B A C F E G L J I K N M O

N-L-R

Page 44: Trees

44 44

Preorder of Expression Tree

/ * + a b - c d + e fGives prefix form of expression.

Page 45: Trees

45 45

Inorder Traversal

public void inOrderPrint() { inOrderPrint(root); }  public void inOrderPrint(TreeNode t){ if (t != null) { inOrderPrint(t.left); System.out.print(t.data + " "); inOrderPrint(t.right); } }

Page 46: Trees

46 46

Inorder Example (visit = print)

L-N-R

H

D

B

A C E G I K M O

N

L

JF

A B C D E F G H I J K L M N O

Page 47: Trees

47 47

Inorder by Projection

Page 48: Trees

48 48

Inorder of Expression Tree

Gives infix form of expression, which is how we normally write math expressions. What about parentheses?

Page 49: Trees

49 49

Postorder Traversal

public void ostOrderPrint() { postOrderPrint(root); }  public void postOrderPrint(TreeNode t){ if (t != null) { postOrderPrint(t.left); postOrderPrint(t.right); System.out.print(t.data + " "); } }

Page 50: Trees

50 50

Postorder Example (visit = print)

L-R-NH

D

B

A C E G I K M O

N

L

JF

A C B E G F D I K J M O N L H

Page 51: Trees

51 51

Postorder of Expression Tree

a b + c d - * e f + /Gives postfix form of expression.

Page 52: Trees

52 52

Level Order Example (visit = print)

Add and delete nodes from a queueOutput: a b c d e f g h i j

Page 53: Trees

53 53

Space and Time Complexity The space complexity of each of the four

traversal algorithm is O(n) The time complexity of each of the four traversal

algorithm is O(n)

Page 54: Trees

54 54

Summary Tree, Binary Tree In order to process the elements of a tree, we consider accessing the

elements in certain order Tree traversal is a tree operation that involves "visiting” (or"

processing") all the nodes in a tree. Types of traversing

– Pre-order: Visit node first, pre-order all its subtrees from leftmost to rightmost.

– Inorder: Inorder the node in left subtree and then visit the root following by inorder traversal of all its right subtrees.

– Post-order: Post-order the node in left subtree and then post-order the right subtrees followed by visit to the node.