trees

39
Trees Binary Trees Extended Binary Trees

Upload: tanaya

Post on 05-Jan-2016

44 views

Category:

Documents


1 download

DESCRIPTION

Trees. Binary Trees Extended Binary Trees. Tree. A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree can have only One Root There Must be One and Only One Path from the Root to Any Other Node. Nodes. Tree. Non - Tree. Edges. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Trees

Trees

Binary Trees

Extended Binary Trees

Page 2: Trees

Tree• A Tree consist of Nodes connected by Edges.• Node is represented by Circle• Edges as Lines or Arrows• A Tree can have only One Root• There Must be One and Only One Path from the

Root to Any Other Node

Tree

Edges

Nodes

Non - Tree

Page 3: Trees

Terminology• Path: Nodes connected by Edges form a Path• Root: Top node of the Tree• Parent: The Above Node of any Node• Child: Downward Node of any Node• Leaf: Node with No Child Node• Level: How many Generation the Node is

from the Root• Key: The value used to find any Node in a

tree

Page 4: Trees

A

B C

J

GD E

KH

F

A is the Root

E is the right child of B

D is the left child of B

B is the parent of D and E The dashed line

is a path

F is the Root of a subtree

Level 0

Level 1

Level 2

Level 3

J and K are siblings

H, E, J, K and G are leaf nodes

Page 5: Trees

BINARY TREES• Binary Tree ‘T’ is define as a Finite set of

Elements called Nodes• T is Empty – Null or Empty Tree• T has a Root Node ‘R’ • T1 and T2 will be the Left and Right Subtrees• If T1 and T2 are Nonempty, their Roots will called

Left and Right Successors of R• Any Node can have 0, 1 or 2 Successors.• Node with No Successors are called Terminal

Nodes

Page 6: Trees

BINARY TREE REPRESENTATION

2

7 5

4

92 6

85

A Binary Tree of Size 9 and Height 3, with a Root 2

Page 7: Trees

Definitions• An Arrow (Edge) is Refers to the link from the Parent to the Child.

Sequence of Edges forms a Path. • The Root Node of a Tree is the Node with No Parents • A Leaf or Terminal Node has No Children • The Depth of a Node N is the Length of the Path from the Root to the

Node. The set of all Nodes at a Given Depth is sometimes called a Level of the Tree. The Root Node is at Depth Zero.

• The Height of a Tree is the Depth of its Furthest Leaf• A Tree with only a Root node has a Height of Zero • Siblings are Nodes that Share the same Parent Node • The size of a Node is the Number of Descendants it has Including Itself • Root is the only Node in the Tree with in-Degree = 0

Page 8: Trees

A

1,3 and 4 are Similar. 1 and 3 are Copies – 2 and 4Are Neither Similar Nor Copy.

B

C D

E

F

G H

A

B

C D

E

F

G H

1 3

2 4

Page 9: Trees

Algebraic Expression E = (a – b) / ((c * d) + e)

/

- +

c

ea b

d

*

Page 10: Trees

Formula: Node K, 2*K, 2*K+1 and K/2

1

2 3

8

74 5

9

6

Depth dn: n Nodes, Dn = log2 n +1

Page 11: Trees

Representation in Memory• By a Single Array (Formula Discussed)

• By Link List– Root Location of the Root– INFO[K] Data of the Node N– LEFT[K] Left Child of Node N– RIGHT[K] Right Child of Node N

Root

A

C

E

B

D

Page 12: Trees

Traversing Binary Trees• Preorder:

– Process the Root R– Traverse the Left Subtree of R– Traverse the Right Subtree of R

• Inorder:– Traverse the Left Subtree of R– Process the Root R– Traverse the Right Subtree of R

• Postorder:– Traverse the Left Subtree of R– Traverse the Right Subtree of R– Process the Root R

Page 13: Trees

Preorder Inorder Postorder

A

B C

H

GD E

J

F

Preorder : A B D E C F H J GInorder : D B E A H F J C GPostorder : D E B H J F G C A

Page 14: Trees

E = [a + (b – c)] * [(d – e) / (f + g – h)]

Preorder : * + a – b c / - d e - + f g h Postorder : a b c - + d e – f g + h - / *

*

+ /

-

b

a -

c d e

-

+

f g

h

Page 15: Trees

Preorder TraversingInitially push NULL onto Stack and Set PTR:= Root

(a) Process the Left Path, Push Right Child in Stack. Ends when Node has No Left Child

(b) [Backtracking] Pop and Assign PTR to the TOP Element, If PTR ≠ NULL then Return to Step (a) otherwise Exit.

Initially push NULL onto Stack (Stack= 0)Set PTR = A Root of TProcess A; Push C in Stack (Stack=0,C)Process B; No Right ChildProcess D; Push H in Stack (Stack=0,C,H)Process G; No Left, Right ChildPop H from StackSet PTR := H (Stack=0,C) Return to Step (a)Process H; Push K in Stack (Stack=0,C,K) Pop K from StackContinues……….Output: A, B, D, G, H, K, C, E, F

A

B C

HG

D E

K

F

Page 16: Trees

PREORDER (INFO, LEFT, RIGHT, ROOT) - Algorithm

STACK is used to Temporarily hold the Addresses of Nodes1. [Push NULL onto STACK and Initialize PTR]

Set TOP : = 1, STACK[1] : = NULL and PTR : = ROOT2. Repeat Step 3 to 5 while PTR ≠ NULL 3. Apply PROCESS to INFO[PTR]4. [Right Child ?]

If Right Child ≠ NULL then [Push on STACK]Set TOP := TOP + 1 and STACK[TOP] := RIGHT[PTR]

[End of If Structure]5. [Left Child ?]

If LEFT [PTR] ≠ NULL then:Set PTR := LEFT [PTR]

Else [Pop from STACK]Set PTR := STACK [TOP] and TOP := TOP - 1 [End of If Structure]

[End of Step 2 loop]6. Exit.

Page 17: Trees

Postorder TraversingInitially push NULL onto Stack and Set PTR:= Root

(a) Process the Left Path, Push all Nodes N in Stack; and If N has a Right Child R(N) Push –R(N)

(b) [Backtracking] Pop and Process Positive Nodes. Assign PTR to the TOP Element, If PTR = -VE, Set PTR +VE then Return to Step (a) otherwise Exit.

Initially push NULL onto Stack (Stack= 0)Set PTR = A Root of T, Push A, -C, B, D, -H, G, K Stack = 0, A, -C, B, D, -H, G, K Pop and Process K then G, Only Pop –H PTR = - H, Reset PTR = H and Push it in Stack Step (a)Stack = 0, A, -C, B, D, H, -M, LPTR = - M, Reset PTR = M Stack = 0, A, -C, B, D, H, MContinues……….Output: K, G, L, M, H, D, B, E, C, A

A

B C

HG

D

L MK

E

Page 18: Trees

POSTORDER (INFO, LEFT, RIGHT, ROOT) - Algorithm

1. [Push NULL onto STACK and Initialize PTR] Set TOP : = 1, STACK[1] : = NULL and PTR : = ROOT

2. [Push Left-most Path in Stack]Repeat Step 3 to 5 while PTR ≠ NULL

3. Set TOP := TOP + 1 and STACK[TOP] := PTR[Pushes PTR on Stack]

4. If Right [PTR] ≠ NULL then [Push on STACK] Set TOP := TOP + 1 and STACK[TOP] := - RIGHT[PTR] [End of If Structure]

5. Set PTR : = LEFT[PTR] [Updates Pointer PTR][End of Step 2 loop]

6. Set PTR := STACK[TOP] and TOP := TOP – 1 [Pops node from STACK]

• Repeat while PTR > 0 (a) Apply Process to INFO[PTR] (b) Set PTR := STACK[ TOP] and TOP := TOP – 1 [Pops node from STACK] [End of Loop]

• If PTR < 0 then (a) Set PTR := - PTR (b) Go to Step 2 [End of If Structure] • Exit

Page 19: Trees

Binary Search Tree

• Why Use Binary Search Tree– Combines the advantages of Ordered Array

and Linked List– Searching

• Equals Compared to Ordered Array• Fast Compared to Linked List

– Insert and Delete Item• Fast Compared to Ordered Array• Equals Compared to Linked List

Page 20: Trees

Binary Search Tree Properties• The value of Node ‘N’ is Greater then every

value in the Left Subtree• The value of Node ‘N’ is Less then every value

in the Right Subtree• Inorder Traversing Produce a Sorted List

38

14 56

70

828 23 45

18

Page 21: Trees

Searching and Insertiona) Compare ITEM with the Root Node ‘N’

i. If ITEM < N, Proceed to the Left Child of Nii. If ITEM > N, Proceed to the Right Child of N

b) Repeat (a) until one of the following occursi. ITEM foundii. ITEM not found, Insert ITEM

38

14 56

70

828 23 45

18

20

ITEM = 20 Inserted

Page 22: Trees

FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) - Algorithm

i. LOC = NULL and PAR = NULL :Tree is Emptyii. LOC ≠ NULL and PAR = NULL: ITEM is Root of Treeiii. LOC = NULL and PAR ≠ NULL: ITEM is not in Tree, Added using PAR1. [Tree Empty ?]

If Root = NULL; Set LOC := NULL and PAR := NULL. Return2. [ITEM at Root?]

If ITEM = INFO[Root]; Set LOC:=Root and PAR := NULL. Return3. [Initialize ITEM and SAVE]

If ITEM < INFO[Root] then Set PTR := LEFT[Root] and SAVE := RootElse Set PTR := RIGHT[Root] and SAVE := Root

[End of If Structure]4. Repeat Steps 5 and 6 while PTR ≠ NULL5. [ITEM Found?]

If ITEM < INFO[PTR] then Set LOC :=PTR and PTR:=SAVE. Return6. If ITEM < INFO [PTR] then Set SAVE:=PTR and PTR:= LEFT[PTR]

Else Set SAVE := PTR and PTR:= RIGHT[PTR][End of If Structure][End of Step 4 Loop]

i. [Search Unsuccessful] Set LOC := NULL and PAR := SAVEii. Exit

Page 23: Trees

INSBST(INFO, LEFT, RIGHT, ROOT, ITEM, LOC)

1. Call FIND (INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)2. If LOC ≠ NULL then Exit3. [Add ITEM to Tree]

Set New := Create NodeSet INFO[New] := ITEM

Set RIGHT[New] := NULL and LEFT[New] := NULL If PAR = NULL thenSet Root := NewElse if ITEM < INFO[PAR] then Set LEFT[PAR] := NEWElse Set RIGHT[PAR] := NEW[End of If Structure]

4. Exit

Page 24: Trees

Deletion in a Binary Tree

• Case 1: Node N has No Children– Replace the Pointer in Parent Node by NULL

• Case 2: Node N has Exactly One Child– Replace the Pointer in Parent Node by its Only

Child• Case 3: Node N has Two Children

– Find the Inorder Successor – Remove the Inorder Successor by using Case 1

or Case 2– Replace the Node N by the Inorder Successor

Page 25: Trees

Case 1: Deletion with No Child

10

5

3 7

10

5

3 7

Null

Before Deletion After Deletion

Page 26: Trees

Case 2: Deletion with One Child

Before Deletion After Deletion

80

52

67

48 71

63

To Be Deleted

80

52

67

48 63

Page 27: Trees

Case 3: Deletion with Two Children

Before Deletion After Deletion

To Be Deleted

50

25

40

15 35

30205

50

30

40

15 35

205

Successor of 25

Page 28: Trees

Case 3: Deletion with Successor Child

Before Deletion After Deletion

To Be Deleted

50

75

93

62 87

77

79Successor of 75

Successor Right Child

50

77

93

62 87

79

Page 29: Trees

Procedure: CASEA (INFO,LEFT,RIGHT,ROOT,LOC,PAR)Deletes N at LOC where N has No 2 Children, PAR gives the Parent of N or PAR = NULL Shows N is Root. CHILDShows the Only Child of N or CHILD = NULL

1. [Initialize CHILD]If LEFT[LOC] = NUL and RIGHT[LOC] = NULL then:

Set CHILD := NULLElse if LEFT[LOC] ≠, then: Set CHILD := LEFT[LOC]Else Set CHILD := RIGHT[LOC]. [End of If Structure]

2. If PAR ≠ NULL, then: If LOC = LEFT[PAR], then: Set LEFT[PAR] := CHILDElse Set RIGHT[PAR] := CHILD. [End of If Structure]

Else:Set ROOT : = CHILD

[End of If Structure]3. Return

Page 30: Trees

Procedure: CASEB (INFO,LEFT,RIGHT,ROOT,LOC,PAR)Deletes N at LOC where N has 2 Children, PAR gives the Parent of N or PAR = NULL Shows N is Root. SUC Shows the Inorder Successor and PARSUC Shows the Parent of Successor

1. [Find SUC and PARSUC](a) Set PTR := RIGHT[LOC] and SAVE := LOC.(b) Repeat while LEFT[PTR] ≠ NULL:

Set SAVE := PTR and PTR:=LEFT[PTR][End of Loop]

(c) Set SUC := PTR and PTR := SAVE2. [Delete Inorder Successor using Previous Procedure]3. [Replace Node N by its Inorder Successor]

(a) If PAR ≠ NULL, then: If LOC = LEFT[PAR], then: Set LEFT[PAR : =SUC Else: Set RIGHT[PAR] := SUC [End of If Structure] Else: Set ROOT := SUC [End of If Structure]

(b) Set LEFT [SUC] := LEFT[LOC] and RIGHT[SUC] := RIGHT[LOC]

4. Return

Page 31: Trees

Algorithm: DEL (INFO,LEFT,RIGHT,ROOT,ITEM)This Algorithm Deletes the Given ITEM of Information from the Binary Search Tree T.

1. [Find the Location of ITEM and its Parent]

Call FIND (INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)

2. [ITEM in Tree?]

If LOC = NULL then Write Item not in Tree and Exit.

3. [Delete Node Containing ITEM]

If RIGHT[LOC] ≠ NULL and LEFT[LOC] ≠ NULL, then:

Call CASEB (INFO,LEFT,RIGHT,ROOT,LOC,PAR).

Else

call CASEA (INFO,LEFT,RIGHT,ROOT,LOC,PAR).

[End of If Structure]

4. Exit.

Page 32: Trees

HEAP – HEAP SORT

• A Complete Binary Tree H can be maxheap or minheap If:– The Value of Node N is Greater then or Equal to

the Value of Each of its Children (MaxHeap)– The Value of Node N is Less then or Equal to

the Value of Each of its Children (MinHeap)

• H[ 1 ] is the root , H[ 2k ], H[ 2k + 1 ]

• Parent of Any Non root Node j: H[ j / 2 ]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

97 88 95 66 55 95 48 66 35 48 55 62 77 25 38 18 40 30 26 24

Page 33: Trees

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

97 88 95 66 55 95 48 66 35 48 55 62 77 25 38 18 40 30 26 24

97

88 95

55

5548

24

66

3566

26304018

48

3825

95

7762

Page 34: Trees

Insertion in a Heap• First Set the New ITEM on the Available

Location i.e. If ITEM = 70 its Location is H[21]

• Then Compare the ITEM with its Parents– If Parent is Greater Then ITEM Leave it– Else Swap Them

• 70 is the Right Child of H[10] = 48• Compare 70 with 48 and Interchange• Compare 70 with 55 and Interchange• Compare 70 with 88 and Leave

Page 35: Trees

New Heap: 44, 30, 50, 22, 60, 55, 77, 55

77

60

4544

55

3050

22

44

30

50

30 44

Page 36: Trees

INSHEAP( TREE, N, ITEM)H is a Heap with N items in which ITEM will be inserted.PTR will Find the Location of ITEM and PAR will be the Parent Location1. [Add New Node to H and Initialize PTR]

Set N := N + 1 and PTR := N2. [Find Location to Insert ITEM]

Repeat Steps 3 to 6 while PTR > 13. Set PAR = [PAR / 2] [Location of Parent Node]4. If ITEM <= TREE [PAR] then:

Set TREE[PAR] := ITEM and Return[End of If Structure]

5. Set TREE[PTR] := TREE[PAR] [Move Node Down]6. Set PTR := PAR [Update PTR] [End of Step 2 Loop]7. [Assign ITEM as the ROOT of H]

Set TREE[1] := ITEM8. Return.

Page 37: Trees

Deletion in a Heap• Deletion of the Root• Replace the Root with the Last Value L of the Heap• L will Sink to its Proper Position

95

70

6530

85

3355

15 221520

85

70

6530

55

3322

15 1520

Page 38: Trees

DELHEAP (TREE,N,ITEM)1. Set LAST := TREE[N] and N:= N-1 [remove the Last Node]

2. Set PTR := 1, LEFT := 2 and RIGHT := 3 [Initialize Pointers]

3. Repeat Step 4 to 7 while RIGHT < = N

4. If LAST >= TREE[LEFT] and LAST >= TREE[RIGHT] then:

Set TREE [PTR] := LAST and Return [End of If Structure]

5. If TREE[RIGHT] <= TREE[LEFT] then:

Set TREE[PTR] := TREE[LEFT] and PTR := LEFT

Else: Set TREE[PTR] := TREE[RIGHT] and PTR := RIGHT

[End of If Structure]

6. Set LEFT := 2 * PTR and RIGHT := LEFT + 1

[End of Step 4 Loop]

7. If LEFT = N and If LAST < TREE[LEFT], then Set PTR:= LEFT

8. Set TREE[PTR] := LAST

9. Return

Page 39: Trees

HEAPSORT (A, N)An array A with N Elements is given.

1. [Build a Heap H]

Repeat for J = 1 to N – 1:

Call INSHEAP(A, J, A[J+1])

[End of Loop]

2. [Sort A by repeatedly deleting the Root of H]

Repeat while N > 1:

a) Call DELHEAP(A, N, ITEM)

b) Set A[N +1] := ITEM

[End of Loop]

3. Exit.