binary tree

34
Trees Non-Linear Data Structure

Upload: ssankett-negi

Post on 17-Dec-2014

1.735 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Binary tree

Trees

Non-Linear Data Structure

Page 2: Binary tree

Non-Linear Data Structure

• These data structures donot have their elements in a sequence.

• Trees is an example.

• Trees are mainly used to represent data containing a hierarchical relationship between elements, ex : records, family trees and table of contents.

Page 3: Binary tree

Terminology of Trees

• The boxes on the tree are called nodes

• The nodes immediately below (to the left and right of) a given node are called its children

• The node immediately above a given node is called its parent

• The (unique) node without a parent is called the root

• A node with no children is called a leaf

• Two children of the same parent are said to be siblings

• A node can be an ancestor (e.g. grandparent) or a descendant

(e.g. great-grandchild)

Page 4: Binary tree

Some tree terminology

• root: node with no parent

– A non-empty tree has exactly one root

• leaf: node with no children

• siblings: two nodes with the same parent.

• path: a sequence of nodes n1, n2, … , nk such that ni is the parent of

ni+1 for 1 ≤ i < k

– in other words, a sequence of hops to get from one node to

another

– the length of a path is the number of edges in the path, or 1 less

than the number of nodes in it

Page 5: Binary tree

Depth and height

• depth or level: length of the path from root to the current node(depth of root = 0)

• height: length of the longest path from root to any leaf

– empty (null) tree's height: -1

– single-element tree's height: 0

– tree with a root with children: 1

Page 6: Binary tree

Tree example

Page 7: Binary tree

Trees

• Tree nodes contain two or more links

– All other data structures we have discussed

only contain one

• Binary trees

– All nodes contain two links

• None, one, or both of which may be NULL

– The root node is the first node in a tree.

– Each link in the root node refers to a child

– A node with no children is called a leaf node

Page 8: Binary tree

Binary Trees

• Special type of tree in which every node or vertex has either no children, one child or two children.

• Characteristics :• Every binary tree has a root pointer which

points to the start of the tree.

• A binary tree can be empty.

• It consists of a node called root, a left subtreeand right subtree both of which are binary trees themselves.

Page 9: Binary tree

Examples : Binary Trees

X X X X

Y YZ Z

A

B C

(1) (2) (3) (4)

Root of tree is node having info as X.

(1) Only node is root.

(2) Root has left child Y.

(3) Root X has right child Z.

(4) Root X has left child Y and right child Z which

is again a binary tree with its parent as Z and

left child of Z is A, which in turn is parent for

left child B and right child C.

Page 10: Binary tree

Properties of Binary Tree

• A tree with n nodes has exactly (n-1) edges or

branches.

• In a tree every node except the root has exactly

one parent (and the root node does not have a

parent).

• There is exactly one path connecting any two

nodes in a tree.

• The maximum number of nodes in a binary tree

of height K is 2K+1 -1 where K>=0.

Page 11: Binary tree

Representation of Binary Tree

• Array representation

– The root of the tree is

stored in position 0.

– The node in position p, is

the implicit father of nodes

2p+1 and 2p+2.

– Left child is at 2p+1 and

right at 2p+2.

X

Y Z

A

B C

Y AZ

0 2 3 4 5 6 71 8

B C

10 11 12

X

9

Page 12: Binary tree

Representation of Binary Tree

• Linked List• Every node will consists of information, and two

pointers left and right pointing to the left and right child nodes.

struct node{

int data;

struct node *left;

struct node *right;

};

• The topmost node or first node is pointed by a root pointer which will inform the start of the tree. Rest of the nodes are attached either to left if less than parent or right if more or equal to parent.

Page 13: Binary tree

• Diagram of a binary tree

B

A D

C

Page 14: Binary tree

Operations on Binary Tree

• Searching an existing node.

• Inserting a new node.

• Deleting an existing node.

• Traversing the tree.

• Preorder

• Inorder

• Postorder

Page 15: Binary tree

Search Process

• Initialize a search pointer as the root pointer.

• All the data is compared with the data stored in each

node of the tree starting from root node.

• If the data to be searched is equal to the data of the

node then print “successful” search.

• Else if the data is less than node’s data move the pointer

to left subtree. Else data is more than node’s data move

the pointer to right subtree.

• Keep moving in the tree until the data is found or search

pointer comes to NULL in which case it is an “unsuccessful” search.

Page 16: Binary tree

Example used for Search21

18

197

6 9

8 11

14

13

Root

Page 17: Binary tree

Example : Search

• Initialize temp as root pointer which is node having 21.

• Loop until temp!=NULL or ITEM found

• Compare item=9 with the root 21 of the tree, since 9<21, proceed temp to the left child of the tree.

• Compare ITEM=9 with 18 since 9<18 proceed temp to the left subtree of 18, which is 7.

• Compare ITEM=9 with 7 since 9>7 proceed temp to

right subtree of the node which holds a value 7.

• Now ITEM=9 is compared with the node which holds

value 9 and since 9 is found the searching process ens here.

Page 18: Binary tree

Insert Process

• For node insertion in a binary search tree, initially the data that is to be inserted is compared with the data of

the root data.

• If the data is found to be greater than or equal to the

data of the root node then the new node is inserted in

the right subtree of the root node, else left subtree.

• Now the parent of the right or left subtree is considered

and its data is compared with the data of the new node and the same procedure is repeated again until a NULL

is found which will indicate a space when the new node has to be attached. Thus finally the new node is made

the appropriate child of this current node.

Page 19: Binary tree

Example used for Insert38

14

238

Root

56

8245

18 70

20

Page 20: Binary tree

Example : Insert

• Suppose the ITEM=20 is the data part of the new node to be inserted in the tree and the root is pointing to the

start of the tree.

• Compare ITEM=20 with the root 38 of the tree. Since 20

< 38 proceed to the left child of 38, which is 14.

• Compare ITEM=20 with 14, since 20 > 14 proceed to the right child of 14, which is 23.

• Compare ITEM=20 with 23 since 20 < 23 proceed to the left child of 23 which is 18.

• Compare ITEM=20 with 18 since 20 > 18 and 18 does not have right child, 10 is inserted as the right child of 18.

Page 21: Binary tree

• Tree traversals:– Inorder traversal – prints the node values in ascending order

1. Traverse the left subtree with an inorder traversal

2. Process the value in the node (i.e., print the node value)

3. Traverse the right subtree with an inorder traversal

– Preorder traversal

1. Process the value in the node

2. Traverse the left subtree with a preorder traversal

3. Traverse the right subtree with a preorder traversal

– Postorder traversal

1. Traverse the left subtree with a postorder traversal

2. Traverse the right subtree with a postorder traversal

3. Process the value in the node

Page 22: Binary tree

Binary tree traversals

• three common binary tree traversal orderings(each one begins at the root):

– preorder traversal: the current node is processed, then the node's left subtree is traversed, then the node's right subtree is traversed (CURRENT-LEFT-RIGHT)

– in-order traversal: the node's left subtree is traversed, then the current node itself is processed, then the node's right subtree is traversed (LEFT-CURRENT-RIGHT)

– postorder traversal: the node's left subtree is traversed, then the node's right subtree is traversed, and lastly the current node is processed (LEFT-RIGHT-CURRENT)

Page 23: Binary tree

Binary tree preorder traversal

• order: C F T B R K G

– The trick: Walk around the outside of the tree and emit a node'svalue when you touch the left side of the node.

"C"

"G" "F"

root

"T"

"R" "B"

"K"

Page 24: Binary tree

Binary tree in-order traversal• order: B T R F K C G

– The trick: Walk around the outside of the tree and emit a node'svalue when you touch the bottom side of the node.

"C"

"G" "F"

root

"T"

"R" "B"

"K"

Page 25: Binary tree

Binary tree postorder traversal

• order: B R T K F G C

– The trick: Walk around the outside of the tree and emit a node'svalue when you touch the right side of the node

"C"

"G" "F"

root

"T"

"R" "B"

"K"

Page 26: Binary tree

Delete Process

• There are four possible conditions are to be taken into account :

i. No node in the tree holds the specified data.

ii. The node containing the data has no children.

iii. The node containing the data has exactly one child.

iv. The node containing data has two children.

• Condition (i) In this case we simply print the message that the data item is not present in the tree.

• Condition (ii) In this case since the node to be deleted has no children the memory occupied by this should be freed and either the left link or the right link of the parent of this node should be set to NULL. Which of these is to be set to NULL depends upon whether the node being deleted is a left child or right child of its parent.

Page 27: Binary tree

Condition (iii)• In this case since the node to be deleted has one child the solution is to

adjust the pointer of the parent of the node to be deleted such that after

deletion it points to the child of the node being deleted as in the diagram.

21

18

197

6 9

11

10

Root

24

26

25 30

21

18

197

6 11

10

24

26

25 30

Root

Page 28: Binary tree

Condition (iv)In this case the node to be deleted has two children. Consider node 9 before deletion. The inorder successor of the

node 9 is node 10. The data of this inorder successor should now be copied into the node to be deleted and a pointer

should be set up pointing to the inorder successor (node 10). This inorder successor would always have one or zero child. It should then be deleted using the same procedure as for deleting one child or a zero child node. Thus, the whole

logic of deleting a node with two children is to locate the inorder successor, copy its data and reduce the problem to a

simple deletion of a node with one or zero child. This is shown in the example.

21

18

197

6 9

11

10

Root

24

26

25 30

8

21

18

197

6 10

11

Root

24

26

25 30

8

Inorder successor of node 9

Page 29: Binary tree

Deletion

• Condition (i) Print message data not found.

• Condition (ii) Since no children so free the node

& make either parent->left=NULL or parent-

>right=NULL.

A A

B C

Parent Parent

left right

X X

In the example1,

parent->left==x so free(x) and

make parent->left=NULL.

In the example2,

parent->right==x so free(x) and

make parent->right=NULL.

Page 30: Binary tree

Deletion• Condition (iii) Adjust parent to point to the child of the deleted node.

I. Node deleted(X) has only left child :

1) If(parent->left==x) parent->left=x->left.

2) If(parent->right==x) parent->right=x->left.

A A

B

Parent Parent

left right

X XB

C C

leftright

Page 31: Binary tree

Deletion• Condition (iii) Adjust parent to point to the child of the deleted node.

II. Node deleted(X) has only right child :

1) If(parent->left==x) parent->left=x->right.

2) If(parent->right==x) parent->right=x->right.

A A

B

Parent Parent

left right

X XB

C C

rightright

Page 32: Binary tree

Deletion• Condition (iv) solution more complex as X has two children,• Find inorder successor of the node X. Inorder successor of any node will be first go to right of the

node and then from that node keep going left until NULL encountered, that will be the inordersuccessor.

• Copy data of inorder successor to node X’s data.• Place pointer at the inorder successor node. Now the inorder succesor will have zero or one child

only (so the complex problem is reduced to condition (iii)).• Delete the inorder successor node by using logic of condition (ii) if no children or condition (iiii) if one

right child.

A

B

Parent

left

X

D

rightleft

C

E

left

A

E

Parent

left

X

D

rightleft

C

E

leftHere inorder successor E has no children condition (ii)

Page 33: Binary tree

Deletion

A

B

Parent

left

X

D

rightleft

C

E

left

F

right

G H

Inorder

Successor

left right

A

E

Parent

left

X

D

rightleft

C

E

left

F

right

G H

Inorder

Successor

left right

A

E

Parent

left

D

rightleft

C

F

G H

left right

Here inorder successor E has one right child condition (iii)

If(parent->left==x) parent->left=x->right.

left

Page 34: Binary tree

Application of Tree

• Expression Tree

• Game Tree