trees. 2 root leaf chapter 5 3 definition of tree n a tree is a finite set of one or more nodes such...

34
Trees

Upload: bertina-hodge

Post on 06-Jan-2018

215 views

Category:

Documents


3 download

DESCRIPTION

CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called the root. n The remaining nodes are partitioned into n>=0 disjoint sets T 1,..., T n, where each of these sets is a tree. n We call T 1,..., T n the subtrees of the root.

TRANSCRIPT

Page 1: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Trees

Page 2: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

2

Trees

Gill Tansey

Brunhilde

Tweed Zoe

Terry

Honey Bear

Crocus Primrose

Coyote

Nous Belle

Nugget

Brandy

DustyRoot

leaf

Page 3: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 3

Definition of Tree A tree is a finite set of one or more nodes

such that: There is a specially designated node called

the root. The remaining nodes are partitioned into n>=0

disjoint sets T1, ..., Tn, where each of these sets is a tree.

We call T1, ..., Tn the subtrees of the root.

Page 4: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

4

Level and Depth

K L

E F

B

G

C

M

H I J

D

A

Level

1

2

3

4

node (13)degree of a nodeleaf (terminal)nonterminalparentchildrensiblingdegree of a tree (3)ancestorlevel of a nodeheight of a tree (4)

3

2 1 3

2 0 0 1 0 0

0 0 0

1

2 2 2

3 3 3 3 3 3

4 4 4

Page 5: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 5

Terminology The degree of a node is the number of subtrees

of the node– The degree of A is 3; the degree of C is 1.

The node with degree 0 is a leaf or terminal node.

A node that has subtrees is the parent of the roots of the subtrees.

The roots of these subtrees are the children of the node.

Children of the same parent are siblings. The ancestors of a node are all the nodes

along the path from the root to the node.

Page 6: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

6

Representation of Trees

List Representation– ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )– The root comes first, followed by a list of sub-trees

data link 1 link 2 ... link n

How many link fields are needed in such a representation?

Page 7: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 7

Left Child - Right Sibling

A

B C D

E F G H I J

K L M

dataleft child right sibling

Page 8: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 8

Binary Trees A binary tree is a finite set of nodes that is

either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.

Any tree can be transformed into binary tree.– by left child-right sibling representation

The left subtree and the right subtree are distinguished.

Page 9: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

J

IM

HL

A

B

C

D

E

F G K

*Figure 5.6: Left child-right child tree representation of a tree

Page 10: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 10

Abstract Data Type Binary_Treestructure Binary_Tree(abbreviated BinTree) isobjects: a finite set of nodes either empty or

consisting of a root node, left Binary_Tree, and right Binary_Tree.

functions: for all bt, bt1, bt2 BinTree, item element Bintree Create()::= creates an empty binary tree

Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE

Page 11: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Class NodeBT{NodeBT bTKi, bTKa;

Object item;

public NodeBT(NodeBt btki, Object it, NodeBt btka){bTKi = btki; item = it; bTKa = btka;}

}Class BinaryTree{

NodeBt node;public create(){node = null;} // public BinaryTree(){node = null;}public isEmpty(BinaryTree bt){ return bt== null;}public Object inOrder(BinaryTree bt){}public Object preOrder(BinaryTree bt){}public Object posOrder(BinaryTree bt){}

}

11

Page 12: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Bpublic Object inOrder(BinaryTree bt){

inOrder(btKi);printData()intOrder(btKa);

}public Object preOrder(BinaryTree bt){

printData()preOrder(btKi)preOrder(btKa)

}public Object posOrder(BinaryTree bt){

posOrder(btKi)posOrder(btKa)printData()

}

preOrder(bt)print- ApreOrder(btKi)

print-BpreOder(btKi)

print-DpreOder(btKa)

print-EpreOrder(btKa)

print-C

12

Page 13: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 13

BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of btelement Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of btBintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt

Page 14: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 14

Samples of Trees

A

B

A

B

A

B C

GE

I

D

H

F

Complete Binary Tree

Skewed Binary Tree

E

C

D

1

2

3

45

Page 15: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 15

Maximum Number of Nodes in BT The maximum number of nodes on level i of a

binary tree is 2i-1, i>=1. The maximum nubmer of nodes in a binary tree

of depth k is 2k-1, k>=1.

i-1

Prove by induction.

2 2 11

1

i

i

kk

Page 16: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 16

Relations between Number ofLeaf Nodes and Nodes of Degree 2

For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0=n2+1

proof: Let n and B denote the total number of nodes & branches in T. Let n0, n1, n2 represent the nodes with no children, single child, and two children respectively. n= n0+n1+n2, B+1=n, B=n1+2n2 ==> n1+2n2+1= n, n1+2n2+1= n0+n1+n2 ==> n0=n2+1

Page 17: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 17

Full BT VS Complete BT A full binary tree of depth k is a binary tree of

depth k having 2 -1 nodes, k>=0. A binary tree with n nodes and depth k is

complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k.

k

A

B C

GE

I

D

H

F

A

B C

GE

K

D

J

F

IH ONML

Full binary tree of depth 4Complete binary tree

Page 18: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 18

Binary Tree Sequential Representations

If a complete binary tree with n nodes (depth =log n + 1) is represented sequentially, then forany node with index i, 1<=i<=n, we have:– parent(i) is at i/2 if i!=1. If i=1, i is at the root and

has no parent.– left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no

left child.– right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n,

then i has no right child.

Page 19: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

CHAPTER 5 19

Sequential Representation

AB--C------D--.E

[1][2][3][4][5][6][7][8][9].[16]

[1][2][3][4][5][6][7][8][9]

ABCDEFGHI

A

B

E

C

D

A

B C

GE

I

D

H

F

(1) waste space(2) insertion/deletion problem

Page 20: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

20

List Representation

Page 21: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

BST

21

Page 22: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Binary Search Trees22

Binary Search Trees A binary search tree is a

binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property:– Let u, v, and w be three

nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w)

External nodes do not store items

An inorder traversal of a binary search trees visits the keys in increasing order

6

92

41 8

Page 23: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Example Binary Searches Find ( root, 2 )

5

10

30

2 25 45

5

10

30

2

25

4510 > 2, left

5 > 2, left

2 = 2, found

5 > 2, left

2 = 2, found

root

Page 24: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Example Binary Searches Find (root, 25 )

5

10

30

2 25 45

5

10

30

2

25

4510 < 25, right

30 > 25, left

25 = 25, found

5 < 25, right

45 > 25, left

30 > 25, left

10 < 25, right

25 = 25, found

Page 25: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Binary Search Tree Construction

How to build & maintain binary trees?– Insertion– Deletion

Maintain key property (invariant)– Smaller values in left subtree– Larger values in right subtree

Page 26: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Binary Search Tree – Insertion

Algorithm1. Perform search for value X2. Search will end at node Y (if X not in tree)3. If X < Y, insert new leaf X as new left subtree for Y4. If X > Y, insert new leaf X as new right subtree for Y

Observations– O( log(n) ) operation for balanced tree– Insertions may unbalance tree

Page 27: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Example Insertion Insert ( 20 )

5

10

30

2 25 45

10 < 20, right

30 > 20, left

25 > 20, left

Insert 20 on left

20

Page 28: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Iterative Search of Binary TreeNode Find( Node n, int key) {

while (n != NULL) { if (n.data == key) // Found it

return n;if (n.data > key) // In left subtree n = n.left;else // In right subtree n = n.right;

} return null;

}Node n = Find( root, 5);

Page 29: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Recursive Search of Binary TreeNode Find( Node n, int key) {

if (n == NULL) // Not foundreturn( n );else if (n.data == key) // Found itreturn( n );else if (n.data > key) // In left subtreereturn Find( n.eft, key );else // In right subtreereturn Find( n.right, key );

}Node n = Find( root, 5);

Page 30: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Binary Search Tree – Deletion

Algorithm 1. Perform search for value X2. If X is a leaf, delete X3. Else // must delete internal node

a) Replace with largest value Y on left subtree OR smallest value Z on right subtreeb) Delete replacement value (Y or Z) from subtree

Observation– O( log(n) ) operation for balanced tree– Deletions may unbalance tree

Page 31: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Example Deletion (Leaf)

Delete ( 25 )

5

10

30

2 25 45

10 < 25, right

30 > 25, left

25 = 25, delete

5

10

30

2 45

Page 32: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Example Deletion (Internal Node) Delete ( 10 )

5

10

30

2 25 45

5

5

30

2 25 45

2

5

30

2 25 45

Replacing 10 with largest value in left

subtree

Replacing 5 with largest value in left

subtree

Deleting leaf

Page 33: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

Example Deletion (Internal Node) Delete ( 10 )

5

10

30

2 25 45

5

25

30

2 25 45

5

25

30

2 45

Replacing 10 with smallest value in right

subtree

Deleting leaf Resulting tree

Page 34: Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called

QUESTION??

34