Lect 21 Zaheer Abbas

  • View
    442

  • Download
    2

Embed Size (px)

Text of Lect 21 Zaheer Abbas

  • 1. Created byZaheer Abbas Aghani 2K9 152

2. Non-Linear Data Structure TREE Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 3.

  • 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.

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 4.

  • 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.

Node Edge Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) A B D E C F 5.

  • A tree can be consists of following Parts:
  • Node
  • Root Node
  • Parent Node
  • Child Node
  • Leaf Node
  • Sub-tree

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 6.

  • 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 morechild nodes , which are below it in the tree.

nodes Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 7.

  • The topmost node in a tree is called theroot 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 Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 8.

  • A node that has a child is called theparent node(orancestornode , orsuperior). Each node in tree has at most one parent except root node.

parent node Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 9.

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

parent node Child node Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 10.

  • 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 Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 11.

  • Asubtreeis 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 aproper subtree

Sub-tree Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 12.

  • Aninternal nodeorinner nodeis any node of a tree that has child nodes and is thus not a leaf node.
  • An intermediatenode between the root and the leaf nodes is called an internal node.

Internal nodes Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 13.

  • 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 eitherempty or Each node has at most two children.
  • Example:-

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) A B D E C F 14.

  • 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 0
    • Level number of other nodes is 1 more than level number of its father node or parent node.
    • Example:

A C B LEVEL0 LEVEL1 Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 15.

  • 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:

LEVEL0 LEVEL1 A B C

  • In this examplethe largest level number is 1 therefore depth is 1+1=2
  • Depth=2

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 16.

  • 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 D E F G Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 17.

  • 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 Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 18.

  • 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 tree Binary Tree Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 19.

  • 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.

Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 50 45 22 48 75 85