Trees Data Structure

Post on 13-Jan-2015

468 views 1 download

description

 

Transcript of Trees Data Structure

Trees

Family Tree

Taiem

Auf

Husais Sehm Murrah Jamha Adi

Ka'ab Amir Hirs

Luayy

Ghalib Muharib

Quresh

Taiem

Zahra

Mugheera

Mutlib Abdus Shams Naufil

Asad Nuzlah

Haris Zubair Abu Talib

Muhammad(PBUH)

Abdullah Musa'ab Abu Lahab Maqoom Hajl Abbas Mugheera Hamza Zarrar

Abdul Mutlib Aba' Saifi

Hashim Abu Amr Abu Ubaida

Abd Munaf Abdud Dar Abdul Uza

Qusayy

Kilab Makhzoom

Trees

A connected, acyclic, undirected graph T = (V, E).

| E | = | V | - 1

Minimally connected --- T is disconnected if any edge is removed.Maximally acyclic --- T contains a cycle if an edge is added.

Tree - Definition

A tree is a finite set of one or more nodes such that:

1. There is a specially designated node called the root.

Recursive Definition

2. The remaining nodes are partitioned in n 0 disjoint sets T1, T2, …, Tn, where each of these sets is a tree.

3. T1, T2, …, Tn are called the sub-trees of the root.

Taiem

Auf

Husais Sehm Murrah Jamha Adi

Ka'ab Amir Hirs

Luayy

Ghalib Muharib

Quresh

Family Treeroot

Sub-trees

Taiem

Zahra

Mugheera

Mutlib Abdus Shams Naufil

Asad Nuzlah

Haris Zubair Abu Talib

Muhammad(PBUH)

Abdullah Musa'ab Abu Lahab Maqoom Hajl Abbas Mugheera Hamza Zarrar

Abdul Mutlib Aba' Saifi

Hashim Abu Amr Abu Ubaida

Abd Munaf Abdud Dar Abdul Uza

Qusayy

Qulaab Makhzoom Banu Hashim

Subtree

a

b d

c

root

A node and all of its descendents.

e f

i j

g h

c

k

A

F KB

C H D L X

Tree

C H D

I

L

N

X

PG MQ

A

F KB

C H D L X

A

F KB

C D XH L

Node Types

I N PG MQ

Root (no parent)

Intermediate nodes (has a parent and at least one child)

I N PG MQ

Leaf nodes (0 children)

Rooted Tree

a

b d

c

root, ancestor

parent

node

internal node(not a leaf)

e f

i j

g h

c

k

node(self)

child child

descendent

leaf(no children)

e, i, k, g, hare leaves

sibling

A

F KB

C H D L X

A

C

F

XH L

KB

D

Degree of a NodeNumber of Children

C H D

I

L

N

X

PG MQ

C

Degree 3

X

Degree 1

H

I

L

N PG MQ

Degree 0

D

Degree 2

A

F KB

C H D L X

A

F KB

C H D L X

Level of a NodeDistance from the root

I N PG MQ

Level 0

Level 1

Level 2

I N PG MQ

Level 3

Height of the Tree = Maximum Level + 1

Height and Depth

7

3 10 4

height = 4 depth 0

depth 1

8 12

16 5

211

9

depth 2

depth 3

depth 4

node height = 2

Binary Trees

Each node has at most two children.

A set of nodes T is a binary tree if

Left child: the child node on the left.Right child: the child node on the right.

ra) it is empty, or b) it consists of three disjoint subsets:

1) a root node2) a left binary subtree3) a right binary subtree

a

d

b cf

e

left subtreeright subtree

Examples of Binary Trees

A

KB

A

B

C H L X

PMQ

C

Q

M

Properties of Binary Trees

• The maximum number of nodes on level iof a binary tree is 2i

• The maximum number of nodes in a binary • The maximum number of nodes in a binary tree of height k is 2k – 1

Height of Binary Tree

nodes Depth/level

1=20 0

2=21 1

4=22 3

ni=0 x i=Xn+1-1/X-1

4=22 3

8=23 4

………… ……..

2h H

Totals Nodes n= hk=0 2 K n= 2h+1–1 /2-1

n = 2h+1-1 lg(n)= lg2h+1-1 lg(n)= (h+1)lg22 -1

lgn=h+1-1 h= lgn

Full Binary TreeA binary tree of height k having 2k – 1 nodes is called a full binary tree

1

32

54 76

8 9 10 11 12 13 14 15

Complete Binary TreeA binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right, is called a complete binary tree

1

32

54 76

8 9 10 11 12

Full and Complete Binary Trees

Full binary tree: Complete binary tree: Each node is either a leaf

or has degree exactly 2.

All leaves have the samedepth and all internal nodes have degree 2.

7

3

8 12

10

128

103

73

11 2

Binary Tree Structure

a

b c

a rightleft

rightleft b left rightc

fe

c rightleft b

g

NULL

left right

left right

right

g

e

c

left

left right

f

Binary TreeNo node has a degree > 2

struct TreeNode {int data;TreeNode *left, *right; // left subtree and right subtree

};

Class BinaryTree {Class BinaryTree {private:

TreeNode * root;public:

BinaryTree() { root = NULL; }void add (int data);void remove (int data);void InOrder(); // In order traversal~ BinaryTree();

};

Inorder Traversal

1. Traverse the left subtree.2. Visit the node.3. Traverse the right subtree.

avisit time d a

b c

d e

Traversal order: bdaec

(n)

Running time:

visit time d

size k

size n-k-1

Binary Tree TraversalIn order Traversal (LNR)

void BinaryTree::InOrder() // work horse function{

InOrder(root);}

void BinaryTree::InOrder(TreeNode *t)void BinaryTree::InOrder(TreeNode *t){

if (t) {InOrder(t->left);visit(t);InOrder(t->right);

}}

void BinaryTree::visit (TreeNode *t) { cout << t->data; }

A

KB

A

BBB

A

KKK

A

Binary Tree TraversalIn Order Traversal (LNR)

C H L X

PMQ

C

QQ

Q

Q

C

C

MM

M

M

C

B

HH

H

H

A

LL

L

L

K

XX

X

PP

P

P

X if (t) {

InOrder(t->left);

visit(t);

InOrder(t->right);

}

Preorder Traversal

1. Visit the node.2. Traverse the left subtree.3. Traverse the right subtree.

aa

b c

d e

Traversal order: abdce

Binary Tree TraversalPre Order Traversal (NLR)

void BinaryTree::PreOrder(){

PreOrder(root);}

void BinaryTree::PreOrder(TreeNode *t) // work horse function{{

if (t) {visit(t);PreOrder(t->left);PreOrder(t->right);

}}

void BinaryTree::visit (TreeNode *t) { cout << t->data; }

A

KB

A

BBB

A

KKK

A

Binary Tree TraversalPre Order Traversal (NLR)

C H L X

PMQ

C

QQQ

C

MMM

C HHH LLL XX

PPP

X if (t) {

visit(t);

PreOrder(t->left);

PreOrder(t->right);

}A B C Q M H K L X P

Binary Tree TraversalPost Order Traversal (LRN)

void BinaryTree::PostOrder() // work horse function{

PostOrder(root);}

void BinaryTree::PostOrder(TreeNode *t)void BinaryTree::PostOrder(TreeNode *t){

if (t) {PostOrder(t->left);PostOrder(t->right);visit(t);

}}

void BinaryTree::visit (TreeNode *t) { cout << t->data; }

Postorder Traversal

1. Traverse the left subtree.2. Traverse the right subtree.3. Visit the node.

aa

b c

d e

Traversal order: dbeca

A

KB

A

BBB

A

KKK

A

Binary Tree TraversalPost Order Traversal (LRN)

C H L X

PMQ

C

QQ

Q

Q

C

C

MM

M

M

C

B

HH

H

H

A

LL

L

L

K

XX

X

PP

P

P

X if (t) {

PostOrder(t->left);

PostOrder(t->right);

visit(t);

}

Binary Tree Traversal

A

KB

C H L XC H L X

PMQ

NLR – visit when at the left of the Node ABCQMHKLXP

LNR – visit when under the Node QCM BHALKXP

LRN – visit when at the right of the Node QMC HBLPXKA

-

/+

A * D *

FCB E

Expression Tree

FCB E

LNR: A+B*C-D/E*F

NLR: -+A*BC/D*EF

LRN: ABC*+DEF*/-

BinaryTree ::~ BinaryTree();

Delete both the left child and right child

Which Algorithm?

before deleting itself

LRN

Another Traversal Example

15

6 18

3

2 413

7

9

2017

Preorder: Inorder: Postorder:

15, 6, 3, 2, 4, 7, 13, 9, 18, 17, 202, 3, 4, 6, 7, 9, 13, 15, 17, 18, 202, 4, 3, 9, 13, 7, 6, 17, 20, 18, 15