tree and binary tree

32
Tree Data Structure

Upload: zaid-shabbir

Post on 11-Aug-2015

236 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Tree and binary tree

Tree Data Structure

Page 2: Tree and binary tree

What is a Tree Data Structure● In computer science a tree is a abstract

mode of hierarchical structure. ● A Tree consist of nodes with a parent-child

relationship.● Tree data structure applications

– Organization charts

– File systems

– Programing environment

Page 3: Tree and binary tree

Tree Terminology● Root node which has no parent.● Internal node which at least one child.● Leaf node which has no child.● Ancestors nodes means parent, grand-

parent, grand-grand-parent etc.● Dept of a node means no of ancestors.● Height of a tree mean maximum dept of any

node.● Decent nodes means child, child-of-child etc.

Page 4: Tree and binary tree

Tree Terminology...

Height of Tree3

Levels

00

1

0

2

0

3

Page 5: Tree and binary tree

Binary Tree VS Not Binary Tree

5

3 7

4 14

9

23

18 27

16 41

32

15

5

3 7

4 14

9

23

18 27

16 41

32

15

Binary Tree Not a Binary Tree

Page 6: Tree and binary tree

Binary Tree ...

With One Child Binary Tree

Invalid binary tree

Binary tree

Page 7: Tree and binary tree

Strictly Binary Tree● Strictly BT either has 0 or 2 subtrees● A strictly BT with n leaves always contains

'2n-1' nodes

Page 8: Tree and binary tree

Full Complete BT

Page 9: Tree and binary tree

Expression Tree● Strictly BT used for expression trees

(A + (B-C))*((D-E) / (F+G-H))

*

+

A -

CB

/

- -

D

F

E + H

G

Page 10: Tree and binary tree

Tree Traversals● There are many ways to traverse BT, Lets

define operations needed to traverse a tree.– P → Process the Root of the binary tree.

– L → Traverse the Left SubTree of Root.

– R → Traverse the Right SubTree of Root.

● These operations performed in different order generate 6 different ways to traverse a binary tree: PLR, PRL, LPR, RPL, LRP and RLP

Page 11: Tree and binary tree

● Preorder traversal also called depth first order

1. P → Process the Root of the binary tree

2. L → Traverse the Left SubTree of Root in Preorder

3. R → Traverse the Right SubTree of Root in Preorder

Void preorder (BinaryTreeNode<T2>* root) {

If (root) {

cout<<root-> Data<< ' ';

Preorder(root->Left);

Preorder(root->Rift);

}

}

Expression:(A+B)*(C-D)

Preorder:*+AB-CD

*

+

A B

-

C D

Recursive Preorder Traversal (PLR)

Page 12: Tree and binary tree

Recursive Inorder Traversal (LPR)

Expression:(A+B)*(C-D)

Preorder:A+B*C-D

*

+

A B

-

C D

● Inorder traversal also called symmetric first order

– L → Traverse the Left SubTree of Root in Inorder

– P → Process the Root of the binary tree

– R → Traverse the Right SubTree of Root in Inorder

Void Inorder (BinaryTreeNode<T2>* root) {

If (root) {

Inorder(root->Left);

cout<<root-> Data<< ' ';

Inorder(root->Rift);

}

}

Page 13: Tree and binary tree

Expression:(A+B)*(C-D)

Preorder:AB+CD-*

*

+

A B

-

C D

– L → Traverse the Left SubTree of Root in Postorder

– R → Traverse the Right SubTree of Root in Postorder

– P → Process the Root of the binary tree

Void Postorder (BinaryTreeNode<T2>* root) {

If (root) {

Postorder(root->Left);

Postorder(root->Rift);

cout<<root-> Data<< ' ';

}

}

Recursive Postorder Traversal (LRP)

Page 14: Tree and binary tree

● Inorder ( ) {

– Create stack to store pointers to binary tree nodes

– Create pointer that point to current node under consideration

– Initialize current node pointer to point towards root node

– While (current node pointer has not processed whole tree) {

● While( current node pointer is not pointing towards empty tree) {– Push current node pointer on stack– Move current node towards left subtree

● }

● If stack is not empty {– do {

● Set current node pointer =Pop pointer from the stack● Process the current node (e.g. display through cout)

– } while curNode has empty right subtree and stack is not empty– Move current node towards right subtree

● }

– }

Iterative Inorder Traversal Using Stack

Page 15: Tree and binary tree

Iterative Inorder Traversal Using Stack

Operations Stack Inorder Expression Current Pointer

Start, Move to root Empty Empty *

Push * to stack, Move to + * Empty +

Push + to stack, Move to A * + Empty A

A is leaf, Process A, Pop +, Move to + * A +

Process +, Move to B * A + B

B is leaf, Process B, Pop *, Move to * Empty A + B *

Process *, Move to - Empty A + B * -

Push – to stack, Move to C - A + B * C

C is leaf, Process C, Pop -, Move to - Empty A + B * C -

Process -, Move to D Empty A + B * C - D

D is leaf, Process D, stack empty: Finish

Expression:(A+B)*(C-D)

Inorder: A+B*C-D

+

A B

-

C D

*

Page 16: Tree and binary tree

Iterative Inorder Traversal Using Stack● Void InOrder(BinaryTreeNode<T2>* root) {

– Stack< BinaryTreeNode <T2>* > stack;

– BinaryTreeNode<T2>* curNode =root;

– while(curNode !=NULL ) {● While(curNode !=NULL ){

– stack.Push (curNode);– CurNode = curNode ->Left;

● }● If (!stack.IsEmpty( ) ){

– Do {● curNode = stack.pop();● cout <<curNode->Data <<' ';

– } while( curNode-> Right ==NULL && !stack.IsEmpty( ) );– curNode = curNode->Right;

● } } }

Page 17: Tree and binary tree

Binary Search Tree (BST)A binary search tree is either empty or in which every node contains a key and satisfied the conditions.

1. The key in the left child of a node (if it exist) is less then the key in its parent node.

2.The key in the right child of a node (if it exist) is greater then the key in its parent node.

3.The left and right subtrees of the root are again binary search trees.

4.No two entries in a binary search tree may have a equal keys.

Page 18: Tree and binary tree

Complexity of BST

Data Structure Sorted Array Linked List BST

Searching Element O ( log2 N ) O ( N ) O ( log2 N )

Inserting Element O ( N ) O ( 1 ) O ( log2 N )

Deleting Element O ( N ) O ( 1 ) O ( log2 N )

Page 19: Tree and binary tree

Binary Search Tree ADT● 15, 5, 23, 3, 7, 2, 4, 14, 9, 18, 27, 16, 41, 32

15

5

3

2

7

4 14

9

23

18 27

16 41

32

Page 20: Tree and binary tree

Search value Algorithm in BST● Find (Any value In BST) {

Create a pointer to BST node: pNode

Initialize pNode = Root

While (Value at pNode ! = Key && pNode!=NULL) {If ( Value at pNode > Key )

pNode =pNode - > Left

elsepNode = pNode - > Right

}

Return pNode;

}

Page 21: Tree and binary tree

Searching Item from BSTFind 14:

Start at Root 15

14 < 15, Go Left

14 > 5, Go Right

14 > 7, Go Right

Found

15

5

3

2

7

4 14

9

23

18 27

16 41

32

Page 22: Tree and binary tree

Inserting Item into BST Algorithm● Insert (Key value in BST) {

– Create pointers to a BST node: pNode, pParent, newNode

– Initialize: pNode with Root, pParent with NULL, newNode Data = key value and its Left/Right pointer =NULL

– If ( Root ==NULL)

● Root = newNode

– Else if (Root !=NULL ) {

● pNode =Root

● pParent =NULL

● While (pNode !=NULL && pNode - > Data ! =value ) {– pParent =pNode– If (pNode - > Data > value )

● pNode = pNode - > Left– else

● pNode = pNode - > Right

● }

Page 23: Tree and binary tree

Inserting Item into BST Algorithm●

● If (pNode ! = NULL )– Display message value already into BST … Failed

● Else if (pNode == NULL ) {

if (pParent - > Data > value )● Pparent - > Left =newNode

– Else● Pparent - > Right = newNode

● }

– }

● }

Page 24: Tree and binary tree

Inserting Item into BST AlgorithmInsert 25:

Start at Root 15

25 > 15, Go Right

25 > 23, Go Right

25 < 27 , Go Left

Empty BST

Insert 25 Here

15

5

3

2

7

4 14

9

23

18 27

16 41

32

25

Page 25: Tree and binary tree

Inorder Predecessor● Inorder

predecessor of some value in the BST is the maximum value in its left subtree

● Inorder predecessor can never have the right child

● Algo: Predecessor( value ) {

– Create pointer to BST Node : pNode

– pNode = Find (value)

– If (pNode ==NULL)

● Return NULL;

– Else If(pNode → Left == NULL)

● Return NULL;

– Else

● Return Maximum(pNode-> Left->Data);

– }

Page 26: Tree and binary tree

Inorder Successor● Inorder successor

of some value in the BST is the minimum value in its right subtree.

● Inorder successor can never have the left child

● Algo: Successor( value ) {

– Create pointer to BST Node : pNode

– pNode = Find (value)

– If (pNode ==NULL)

● Return NULL;

– Else If(pNode → Right == NULL)

● Return NULL;

– Else

● Return Minimum(pNode-> Right->Data);

– }

Page 27: Tree and binary tree

15

5

3 7

4 14

9

23

18 27

16 41

32

25

Inorder PredecessorStart at Root 15Find maximum in left subtree (14)

Inorder SuccessorStart at Root 15Minimum in Right subtree 16

Inorder predecessor

Inorder Successor

Inorder Predecessor and Successor

Page 28: Tree and binary tree

Delete Node from BST● Three methods can use to delete a node

from BST– Node has both right and left BST empty then

just replace node with NULL

– Node has only right BST

– Node has only left BST

– Node has both right and left BST● Find the Inorder successor of value. Attach left

subtree of value as left subtree of its successor. Now value has only right subtree

● Find the Inorder Predecessor of value. Attach right subtree of value as right subtree of its predecessor. Now value has only left subtree

Page 29: Tree and binary tree

Delete Node from BST ...

15

5

3 7

4 14

9

Node has both right and left BST empty then just replace node with NULL

15

5

3 7

4 14

Page 30: Tree and binary tree

Delete Node from BST ...

15

5

3 7

4 14

9

Node has only right BST

15

5

3

4

14

9

Page 31: Tree and binary tree

Delete Node from BST ...

15

5

3 7

4 14

9

Node has only Left BST

15

5

3

4

7

9

Page 32: Tree and binary tree

Delete Node from BST ...

15

5

3 7

4 14

9

Node has bot Left & Right BST

Inorder predecessor

Inorder Successor

15

4

3 7

14

9

15

7

14

9

3

4

Replace with Inorder predecessor

Replace with Inorder successor