1 / 126

Understanding Tree Data Structures and Representations

Learn about binary trees, tree properties, representations, traversals, and hierarchical data in this comprehensive guide.

lisar
Download Presentation

Understanding Tree Data Structures and Representations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit-3 objectives Introduction to trees. Representation of trees. Binary tree ADT. Properties of trees. Binary tree representations-array and linked representations. Binary tree traversals. Threaded binary tree. MAX priority Queue ADT-implementation. Max Heap- Definition, insertion into and deletion from a Max Heap. Graphs- Introduction, definition, terminalogy Graph ADT. Graph representations-Adjacency matrix, Adjacency lists Graph traversals-BFS,DFS

  2. TREE

  3. TREE

  4. Linear Lists And Trees • Linear lists are useful for serially ordered data. • (e0, e1, e2, …, en-1) • Days of week. • Months in a year. • Students in this class. • Trees are useful for hierarchically ordered data. • Employees of a corporation. • President, vice presidents, managers, and so on.

  5. Hierarchical Data And Trees • The element at the top of the hierarchy is the root. • Elements next in the hierarchy are the children of the root. • Elements next in the hierarchy are the grandchildren of the root, and so on. • Elements that have no children are leaves.

  6. President VP3 VP1 VP2 children of root root Manager Manager1 Manager2 Manager grand children of root Worker Bee great grand child of root Example Tree

  7. TREE Definition: • A tree is a finite of one or more nodes such that • There is specially designated node called root. • The remaining node are partitioned in to n>=0 disjoint sets T1,T2,T3….Tn, where each of these sets in a tree. • T1,T2,T3….Tn, are called the subtrees of the root. t

  8. TREE • A tree satisfies the following properties: • It has one designated node, called the root, that has no parent. • Every node, except the root, has exactly one parent. • A node may have zero or more children. • There is a unique directed path from the root to each node.

  9. TREE A C B D E F G H I Root: Only node with no parent Parent of x: The node directly above node x in the tree Child of x: A node directly below node x in the tree Siblings: Nodes with common parent. Path: A sequence of connected nodes. Ancestor of x: A node on the path from the root to x. Descendent of x: A node on a path from x to a leaf. Empty Tree: A tree with no nodes. Leaf or ExternalNode: A node with no children.

  10. TREE Level 0 Level 1 Level 2 Level 3 Level 4 A A B C D E F G I H J k The level of a node x: It isthedistance from the root to node x. Generally, the root has a zero distance from itself, the root is at level 0. The, children of the root are at level 1, their children are at level at 2, and so on. Height of the Tree: The maximum no. of nodes covered in a path starting from root node to a leaf node is called height of a tree. Depth: Length of the path to that node from the root. Degree/arity of node x: Number of children's of a node x.

  11. SUB TREES President VP3 VP1 VP2 root Manager Manager1 Manager2 Manager Worker Bee

  12. President Leaves VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

  13. President Parent, Grandparent, Siblings, Ancestors, Descendants VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

  14. Caution • Some texts start level numbers at 0 rather than at 1. • Root is at level 0. • Its children are at level 1. • The grand children of the root are at level 2. • And so on. • We shall number levels with the root at level 1.

  15. Level 1 President VP3 VP1 VP2 Level 2 Manager Manager1 Manager2 Manager Level 3 Worker Bee Level 4 height = depth = number of levels

  16. President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee 3 2 1 1 Node Degree = Number Of Children 0 0 1 0 0

  17. 3 President 2 1 1 VP3 VP1 VP2 0 0 1 0 Manager Manager1 Manager2 Manager 0 Worker Bee Tree Degree = Max Node Degree Degree of tree = 3.

  18. Trees Representation There are several ways to draw a tree that represented in figure

  19. Trees Representation There are three ways to represent a tree List representation Left child-Right sibling representation Representation as a Degree-Two tree List representation: The tree shown above can be written as the list ( A ( B ( E ( K , L ) ,F) ,C ( G ) ,D ( H ( M ) , I ,J ) ) ) The information in the root node comes first, followed by a list of sub tress of that node.

  20. List Representation

  21. List Representation Lemma: If T a k-ary tree(a tree of degree k) with n nodes, each having a fixed size , then n(k-1)+1 of the nk child fields are 0,n>=1 Proof: Each non-zero child field points to a node and there is exactly one pointer to each node other than root, The number of nonzero child fields in an n-node tree is exactly n-1. The total number of child fields in a k-ary tree with n nodes is nk. Hence the number of non zero fields is nk-(n-1)=n(k-1)+1

  22. Left Child-Right Sibling Representation The structure of the node used in left child- right sibling representation. DATA LEFT CHILD RIGHT SIBLING

  23. Left Child-Right Sibling Representation The structure of the node used in left child- right sibling representation for the tree given below is.

  24. Left Child-Right Sibling Representation The structure of left child- right sibling representation for the above figure is .

  25. Representation as a Degree-Two tree To obtain the degree-two tree representation of a tree we simply rotate the right sibling pointers in a left child- right sibling tree clockwise by 45 degrees We refer to the two children of a node as the left and right children. Notice that the right of the root node is empty Left child –right child trees are also known as binary trees.

  26. Representation as a Degree-Two tree

  27. TREE APPLICATIONS Trees are very important data structures in computing. They are suitable for: • Hierarchical structure representation, e.g., • File directory. • Organizational structure of an institution. • Class inheritance tree. • Problem representation, e.g., • Expression tree. • Decision tree. • Efficient algorithmic solutions, e.g., • Search trees. • Efficient priority queues via heaps.

  28. BINARY TREE • In a binary tree, each node has at most two sub trees. • A binary tree(T) is a finite set of nodes such that:  T is empty tree (called empty binary tree)  T contains a specially designed node called the root of T, and remaining nodes of T forms two disjoint binary trees T1 and T2 which are called left sub treeand right sub treerespectively. Note: A binary tree is a tree in which no nodes can have more than two children.

  29. BINARY TREE ADT ADT Binary_ Tree (BinTree) is Objects: a finite set of nodes either empty or consisting of a root node, left Binary _ Tree, and right Binary _Tree. Functions: For all bt, bt1, bt2belongs to BinTree, item belongs to Binary _Tree BintreeCreate() ::== creates an empty binary tree Boolean Isempty(bt) ::== if(bt==empty binary tree) Return TRUE else return FALSE BinTreeMakeBT(bt1,item,bt2) ::==return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item

  30. BINARY TREE ADT BinTreeLchild(bt)::== if(IsEmpty(bt))return error else return the left subtree of bt BinTreeRchild(bt) ::== if(IsEmpty(bt))return error else return the right subtree of bt BinTree Data(bt) ::== if(IsEmpty(bt))return error else return the data in the root node of bt

  31. PROPERITIES OF BINARY TREES maximum number of elements minimum number of elements Binary Tree Properties A binary tree with n elements, n > 0, has exactly n-1 edges. A binary tree of height h, h >= 0, has at least h and at most 2h-1 elements or nodes in it. The height of a binary tree that contains n elements, n >= 0, is at least (log2(n+1)) and at most n. Minimum and Maximum number of elements for height 4

  32. PROPERITIES OF BINARY TREES P1: The maximum number of nodes on level I of a binary tree is 2i-1, i>=1 Proof by induction: Induction base :The root is at level 1 so i=1, 21-1 =20 =1 Induction Hypothesis: Let i>1 maximum no of nodes at level i-1 is 2i-2 Induction step: Since each node in a binary tree has maximum degree of 2, the maximum number of nodes on level I is two times the maximum number of nodes on level i-1 or 2i-1

  33. PROPERITIES OF BINARY TREES P2: The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1 K k ∑ (maximum number of node on level i)=∑ 2i-1 =2k -1 i=1 i=1 P3: [The relation between number of leaf nodes and degree-2 nodes]: For any nonempty binary tree T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2 the n0=n2+1. Let n1 be the number no of nodes of degree one, and n the total number of nodes. Since all nodes in T are at most of degree two , we have n=n0+n1+n2----------------------------------------------------------(1) If we count the no of branches, the total nodes n=B+1 And B=n1+2n2 so n=n1+2n2+1…………………………………(2) Subtract (2) from (1) so n0=n2+1

  34. PROPERITIES OF BINARY TREES P4: A full binary tree of depth k is a binary tree of depth k having 2k-1 nodes, k>=0 P5: A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. Differences between a tree and a binary tree Trees 1) Tree never be empty. 2) A node may have any no of nodes/children. Binary tree 1) Binary tree may be empty. 2) A node may have at most 2 children or 0 or1 children.

  35. BINARY TREE FULL BINARY TREE • A full binary tree is a tree in which every node other than the leaves has two children. • Note: All leaves are at same level and all other nodes each have two children. • A full binary tree of height h has exactly 2h-1 nodes. 1 Level 0- 1node Level 1- 2nodes 2 3 5 6 7 Level 2- 4nodes 4 14 15 10 12 13 Level 3-8nodes 8 9 11

  36. BINARY TREE COMPLETE BINARY TREE • A complete binary tree is a binary tree in which every level is completely filled except possibly the last level. • In the unfilled level, the nodes are attached starting from the left-most position. 1 Level 0- 1node 2 3 Level 1- 2 nodes 4 5 6 7 Level 2- 4 nodes 8 9 Level 3- 2 nodes

  37. BINARY TREE REPRESENTATIONS Binary trees can represented in 2 ways Array representation Linked representation • Sequential Representation or Array representation  Tree nodes are stored in a linear data structure like array.  Root node is stored at index ‘0’  If a node is at a location ‘i’, then its left child is located at 2 * i + 1 and right child is located at 2 * i + 2 • The space required by a binary tree of height h is 2h-1.

  38. BINARY TREE REPRESENTATIONS Array representation

  39. BINARY TREE Advantages of array/sequential/static representation • Any node can be accessed from any other node by calculating the index and this is efficient from execution point of view. • There is no or less overhead of maintaining pointers. Disadvantages of Static Representation • The major disadvantage with this type of representation is wastage of memory. Example: In the skewed tree, half of the array is unutilized. • Allows only static representation. There is no possible way to enhance the size of the tree. • Inserting a new node and deleting a node from it are inefficient with this representation because these require considerable data movement up and down the array which demand excessive processing time.

  40. BINARY TREE Representation of Binary Tree using Linked List • The most popular way to present a binary tree. • Each element is represented by a node that has two link fields (leftChild and rightChild) plus an element field . • The space required by an n node binary tree is n *sizeof a node. struct node { /* a node in the tree structure */ struct node *lchild; int data ; struct node *rchild; }; The pointer lchild stores the address of left child node. The pointer rchild stores the address of right child node. If child is not available , NULL is stored. A pointer variable root represents the root of the tree. lchild data rchild

  41. BINARY TREE Representation of Binary Tree using Linked List

  42. BINARY TREE Advantages of linked representation • This representation is superior to the array representation as there is no wastage of memory. • There is no need to have prior knowledge of depth of the tree. Using dynamic memory allocation concept one can create as much memory (node) as required. • Insertion and deletion which are the most common operations can be done without moving the other nodes. Disadvantages of linked representation • This representation does not provide direct access to a node. • It needs additional space in each node for storing the left and right subtrees

  43. BINARY TREE struct BST { int data; struct BST *left,*right; }node; struct BST* root=NULL,*temp,*cur; void create() { char c[10]; temp=root; cur=(struct BST*)malloc(sizeof(struct BST)); printf("\n enter data:\n"); scanf("%d",&cur->data); cur->left=NULL; cur->right=NULL; if(temp==NULL) root=cur; else

  44. BINARY TREE { while(temp!=NULL) { printf("\n enter u want to insert left or right child: "); scanf("%s",c); if(c[0]=='l') { f(temp->left==NULL) { temp->left=cur; return; } else temp=temp->left; } else { if(temp->right==NULL) { temp->right=cur; return; } else temp=temp->right; }//else }//while }//else }//create

  45. BINARY TREE TRAVERSALS Traversal : visiting each node at least once Traversal can be done in two ways 1.Recursive traversal 2.Iterative traversal • There are six recursive and non recursive techniques for binary tree traversal. 1. Preorder Traversal 2. Inorder Traversal 3. Postorder Traversal 4. Converse Preorder Traversal 5. Converse Inorder Traversal 6. Converse Postorder Traversal

  46. BINARY TREE TRAVERSAL Algorithm preOrder (root) Traverse a binary tree in root-left-right Pre Condition: root is the entry node of a tree or subtree Post Condition: each node has been processed in order 1. if(root is not null) 1. process(root) 2. preOrder(leftsubtree) 3. preOrder(rightsubtree) 2. end if end preOrder

  47. BINARY TREE TRAVERSAL Algorithm ConversepreOrder (root) Traverse a binary tree in root-right-left Pre Condition: root is the entry node of a tree or subtree Post Condition: each node has been processed in order 1. if(root is not null) 1. process(root) 2. ConversepreOrder(rightsubtree) 3. ConversepreOrder(leftsubtree) 2. end if end ConversepreOrder

  48. BINARY TREE TRAVERSAL Algorithm inOrder (root) Traverse a binary tree in left-root-right Pre Condition: root is the entry node of a tree or subtree Post Condition: each node has been processed in order 1. if(root is not null) 1. inOrder(leftsubtree) 2. process(root) 3. inOrder(rightsubtree) 2. end if end inOrder

  49. BINARY TREE TRAVERSAL Algorithm ConverseinOrder(root) Traverse a binary tree in left-root-right Pre Condition: root is the entry node of a tree or subtree Post Condition: each node has been processed in order 1. if(root is not null) 1. ConverseinOrder(rightsubtree) 2. process(root) 3. ConverseinOrder(leftsubtree) 2. end if end ConverseinOrder

  50. BINARY TREE TRAVERSAL Algorithm postOrder (root) Traverse a binary tree in left-right-root Pre Condition: root is the entry node of a tree or subtree Post Condition: each node has been processed in order 1. if(root is not null) 1. postOrder(leftsubtree) 2. postOrder(rightsubtree) 3. process(root) 2. end if end postOrder

More Related