lect 21 zaheer abbas

19
Created by Zaheer Abbas Aghani 2K9 152

Upload: information-technology-center

Post on 19-May-2015

451 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Lect 21 Zaheer Abbas

Created by Zaheer Abbas Aghani 2K9 152

Page 2: Lect 21 Zaheer Abbas

Non-Linear Data Structure

TREE

2Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 3: Lect 21 Zaheer Abbas

Definition:Tree is a type of non-linear data structure that

is mainly used to represent data containing a hierarchical relationship between elements.

e.g. records, family tree and table of contents.

In tree every node has three fields, first represent the data while second & third keep the information of left & right child of node.

3Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 4: Lect 21 Zaheer Abbas

There are several ways to represent tree structure. We will use graph to represent tree in which every node will be represented by circle and a line from one node to other is called edge.

A

B

D E

C

F

Node

Edge

4Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 5: Lect 21 Zaheer Abbas

A tree can be consists of following Parts:

NodeRoot NodeParent NodeChild NodeLeaf NodeSub-tree

5Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 6: Lect 21 Zaheer Abbas

A node may contain a value or a condition or represent a separate data structure or a tree of its own. Each node in a tree has zero or more child nodes, which are below it in the tree.

nodes

6Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 7: Lect 21 Zaheer Abbas

The topmost node in a tree is called the root node. Being the topmost node, the root node will not

have parents. It is the node at which operations on the tree commonly begin.

root node

7Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 8: Lect 21 Zaheer Abbas

A node that has a child is called the parent node (or ancestor node, or superior ). Each node in tree has at most one parent except root node.

parent node

8Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 9: Lect 21 Zaheer Abbas

A node that has a parent node is called the child node. Each node in tree has is child node except leaf node.

parent node

Child node

9Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 10: Lect 21 Zaheer Abbas

A node with no children is called a leaf node. Most leaf nodes are at the bottom most level of tree. Since

they are at the bottom most level, they do not have any children.

They are also referred to as terminal node.

Leaf node

10Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 11: Lect 21 Zaheer Abbas

A subtree is a portion of a tree data structure that can be viewed as a complete tree in itself.

Any node in a tree , together with all the nodes below it, comprise a subtree of . The subtree corresponding to the root node is the entire tree;

the subtree corresponding to any other node is called is a proper subtree

Sub-tree

11Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 12: Lect 21 Zaheer Abbas

An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf node.

An intermediate node between the root and the leaf nodes is called an internal node.

Internal nodes

12Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 13: Lect 21 Zaheer Abbas

First we study a special kind of tree called Binary tree, which can easily maintained in the computer.

Binary tree can be define as a finite set of elements, called nodes.A binary tree can be either empty or Each node has at most two children.

Example:- A

B

D E

C

F13

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)

Page 14: Lect 21 Zaheer Abbas

Each node in binary tree is assigned a level number.

The level of each node in binary tree is defined as:Root of tree is defined as level 0Level number of other nodes is 1 more than

level number of its father node or parent node.Example:

A

CB

LEVEL 0

LEVEL 1

14Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 15: Lect 21 Zaheer Abbas

The depth or height of tree is one more than the largest level number of tree.

For example if the largest level number of tree is 2 than its depth is calculate by 2+1=3.

Example:LEVEL 0

LEVEL 1

A

B C

In this example the largest level number is 1 therefore depth is 1+1=2

Depth=2

15Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 16: Lect 21 Zaheer Abbas

Strictly Binary Tree: If every internal node (non terminal node) has its non empty left & right children then it is called strictly binary tree.

Here every internal node A, B, E has two non empty left and right child hence it is strictly binary tree.

A

B C

DE

F G

16Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 17: Lect 21 Zaheer Abbas

Complete Binary Tree: A complete binary tree is a binary tree where all internal nodes have both children except the last node.

In this tree the last node () has only one child.

3

1

2

4 5 6

17Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)

Page 18: Lect 21 Zaheer Abbas

Extended Binary Tree: A binary tree is called extended binary tree if every node of tree has zero or two children. It is also said to be 2-tree.

In binary tree, most of the nodes have one child then we can add one more child and can extend it. So it can be converted to extended binary tree.

Extended binary treeBinary

Tree18

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)

Page 19: Lect 21 Zaheer Abbas

Binary Search Tree: A binary search tree is a binary tree in which each node has value greater than every node of left sub-tree and less than every node of right sub-tree.

50

45

22 48

75

85

19Prepared by: Shumaila Bashir

Sheikh(Lecturer, ITC)