trees 2015, fall pusan national university ki-joune li

18
Trees 2015, Fall Pusan National University Ki-Joune Li

Upload: laureen-nelson

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees 2015, Fall Pusan National University Ki-Joune Li

Trees

2015, FallPusan National UniversityKi-Joune Li

Page 2: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

2

Tree

Definition Tree : Finite set of one or more nodes

One Root A set of n (0) children, each of which is a tree (subtree)

Hierarchical Structure Recursion

Recursive way of definition Divide and Conquer

a

b c

d e f

(a(b(d,e),c(f)))

Page 3: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

3

Some Terms

Root Leaf (or Terminal) Node Internal nodes

Child and Parent Sibling Degree of Tree

Maximum Branching Factor Level

Depth of Tree: Maximum Level

a

b c

d e f

Root

Leaf nodes

Internal nodes

Level 1

Level 2

Level 3

Page 4: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

4

Binary Tree

Binary Tree Tree whose maximum branch factor = 2 A finite set of nodes

empty or Root, left subtree, and right subtree

a

b c

d e f

Page 5: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

5

Some Properties of Binary Tree

A binary tree T of depth k Maximum number of nodes on level i : 2i-1 Maximum number of nodes in T : 2k – 1 n0 = n2 + 1,

where n0 is the number of leaf nodes andn2 is the number of nodes with two children

Full Binary Tree A Binary Tree of depth k with 2k – 1 nodes No empty subtrees except nodes on level k

Complete Binary Tree Nodes correspond to numberd from 1 to n

a

b

d e

c

f g

1

2

4 5

3

Page 6: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

6

Representation of Binary Tree : Array

If a binary tree T with n nodes is complete and i is the node number of the i-th node, LeftChild(i) = 2i if 2i n, otherwise no left child RightChild(i) = 2i + 1 if 2i +1 n, otherwise no left child

Only valid for complete binary graph

2/)( iiparent

1

2

4 5

3

root

0

1

2

3

4

5

Left child

Right child

Parent

Page 7: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

7

Representation of Binary Tree : Linked

Class BinaryTree { private: TreeNode *root; public: // Operations };

Class BinaryTree { private: TreeNode *root; public: // Operations };

Class TreeNode { friend class BinaryTree; private: DataType data; TreeNode *LeftChild; TreeNode *RightChild;};

Class TreeNode { friend class BinaryTree; private: DataType data; TreeNode *LeftChild; TreeNode *RightChild;};

RightChild

Data

Node

LeftChild

Page 8: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

8

Binary Tree Operations : Traversal

Traversal Prefix Infix Postfix Level Order

-

+

X *

*

X Y

X + Y * Z – X * Y

- + X * Y Z * X Y

X Y Z * + X Y * -

Y Z

X + Y * Z – X * Y

Page 9: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

9

Preorder Traversal

void BinaryTree::Preorder() { Preorder(root); }

void BinaryTree::Preorder() { Preorder(root); }

void BinaryTree::Preorder(TreeNode *node) { if(node!=NULL}{ cout << node->data; preorder(node->leftChild); preorder(node->rightChild); }}

+

A B

+ A B

Page 10: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

10

Level Order Traversal

Not Recursivea

b

d e

c

f g

Queuing

void BinaryTree::LevelOrder() { Queue *queue=new Queue; queue->add(root); TreeNode *p; while(queue->isEmpty()==NO) { p=queue->delete(); cout << p->data; queue->add(node->leftChild); queue->add(node->rightChild); }}

a

a b

Output

Queue c

b

d e

c

f g

Page 11: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

11

TreeNode* BinaryTree::Copy(TreeNode *node) { TreeNode* p=NULL; if(node!=NULL}{ p=new TreeNode; p->data=node->data; p->leftVhild=copy(node->leftChild); p->rightChild=copy(node->rightChild); } return p;}

Tree Copy

TreeNode* BinaryTree::Copy() { return Copy(root); }

TreeNode* BinaryTree::Copy() { return Copy(root); }

C

A B

C

A B

Equal: Same Way Evaluation of propositional calculus

Page 12: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

12

Binary Search Tree

Binary Search Tree A kind of Binary Tree For each node n in a binary search tree

Max(n.leftChild) ≤ n.data < Min(n.rightChild)

55

38

20 45

74

62 80

40 51

51 62

Page 13: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

13

Binary Search Tree: Search

Search by Recursion

Search without Recursion

TreeNode* BST::SearchBST(TreeNode *node,int key) { if(node==NULL) return NotFound; else if(key==node->data) return node; else if(key<node->data) return SearchBST(node->leftChild); else return SearchBST(node->rightChild);}

TreeNode* BST::SearchBST(int key) { TreeNode *t=root; while(t!=NULL) { if(key==t->data) return t; else if(key < t->data) t=t->leftChild; else /* key > t->data) t=t->rightChild); } return NULL;}

Page 14: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

14

Binary Search Tree: Insertion

Boolean BST::Insert(int key) { TreeNode *p=root; TreeNode *q=NULL; while(p!=NULL) { q=p; if(key==p->data) return FALSE; else if(key < p->data) p=p->leftChild; else /*key > p->data*/ p=p->rightChild; }

TreeNode *t=new TreeNode(key); if(root==NULL) root=t; else if(key < q->data) q->leftChild=t; else q->rightChild = t; return TRUE;}

55

38

20 45

74

62 80

40 51

+ 60

p

q

p

q

p

q

60

Page 15: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

15

Binary Search Tree: Deletion

Three cases

38

20 45

40 51

55

38

45

74

62

40 51 55Case 1: Delete a

Leaf NodeCase 2: Delete

a node with a child

55

38

45

74

62

40 51 55

Case 3: Delete a nodewith two children

51

Largest node

Page 16: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

16

Heap

Heap (Max Heap) Priority Queue

DeleteQueue: Delete Max Element from Queue A Complete Binary Tree

Data at a node is greater than the max of its two subtrees

Represented by array (or pointers)

74

45

34

55

1223

Page 17: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

17

Heap: Insertion

74

45

34

55

1223

+ 60

74

45

34

55

1223 60

74

45

34

55

1223 60

Heap[++k]=60

Heap[ k/2 ]=55

Heap[k]=60 O(log2 n)

Page 18: Trees 2015, Fall Pusan National University Ki-Joune Li

STEMPNU

18

Heap: Deletion

74

45

34

55

1223

45

34

55

1223

45

34

55

12

23

45

34

55

12

23

O(log2 n)