Data Structures Trees By Dr. Mohammad Ali H. Eljinini
Trees Are collections of items arranged in a tree like data structure (none linear). Items are stored inside units called nodes. However: We can use arrays and linked structures to represent trees, as we will see later. So, let us start with some trees anatomy
Trees Anatomy Root Level 0 R Internal Node Level 1 S T Edge (or Branch) Level 2 X U V W Level 3 Y Z Leaf
Trees Anatomy Every Tree starts at the root. S and T are children of R. S is the parent of X. The root in on Level 0. In a Tree, there is exactly one path from R to any other node. The descendants of S are X, Y, and Z. The ancestors of node Y are X, S, and R.
Binary Trees A Binary Tree: is a tree in which each node has at mos two children. Example: R S T X U W Y Z V
Complete Binary Trees A Complete Binary Tree: is a Binary Tree in which each level must be completed (full), before we start a new level. Note: Items are left on the last level are filled left to right. Example: R S T Full X V U W Full Y Z Last level: start filling left to right!
Sequential BT Representation (array-based) H D K B F J L A C 0 1 2 3 4 5 6 7 8 9 Lets call the above array, Array A: 1. The Root is A[1] 2. Left Child of A[i] is at A[2i] 3. Right Child of A[i] is at A[2i+1] 4. The Parent of A[i] is A[i/2] Array A 1 H 2 3 D K 4 5 6 7 B F J L Example: Node B at A[4]: Parent of B: A[4/2] = A[2] has D LC of B : A[2*4] = A[8] has A RC of B : A[2*4+1] = A[9] has C 8 9 A C A Binary Tree represented in Array A So, we can represent a BT in an Array, but we have to determine where each item should be located in the array as shown above.
Linked BT Representation (Linked-based) R H D K B F public class TreeNode{ itemtype item; TreeNode LC, RC; // Methods of get and set } // as done previously LC item TreeNode RC TreeNode is a class contains: item and two references (LC and RC)
Linked BT Representation (Linked-based) But, how do we create the first node (Root)? TreeNode R; // a reference to TreeNode Create the node and set it s address in R: R = new TreeNode( ); R Store character H in Root Node: R.setItem( H ); H Store null (for now) in LC and RC: R.setLC(null); R.setRC(null);
Linked BT Representation (Linked-based) Now, let s insert another node to the left: TreeNode p; // another TreeNode reference Create the node and set it s address in p: p = new TreeNode(); R Fill in the new node: p H p.setitem( D ); p.setlc(null); p.setrc(null); D Attach the new node: R.setLC(p);
Applications of Trees: Heaps A Heap is: - A complete BT - and, the value in any parent is greater then the value in its LC and in RC. - That s it! Examples: 7 9 10?? 8 6 4?? 8 7 5 10 4 6 4 7 2 9 1 5 Not Complete Complete, but not heap! Heap
Applications of Heaps: 1. Priority Queues: The highest value comes out first! Note: Highest value is located in the root of the heap (always)!! Problem: If we remove the value in the root, we must reconstruct the heap again! Solution: Heapify! How? 1.Delete last leaf, and place its value in Root. 2.Exchange value of root with the largest value of its two children. 3.Exchange values down the path until the last level.
Applications of Heaps: 2. HeapSort: A sorting algorithm using a heap! How it works? 1.Insert values to be sorted in a heap. 2.Root has highest value: move it into array. 3.Heapify. 4.Repeat step 2 and 3 until all values are moved into the array.
Traversing Binary Trees: Expression Trees: A Binary Tree used to represent algebraic expressions formed with binary operators. Example: (b 2-4ac)/2a (b^2-4*a*c)/(2*a) - / * Note: 1. All operands are in leafs. 2. All operations are in internal nodes. ^ b 2 * 4 a * 2 a c
Traversing Binary Trees: Three ways to Travers Expression Trees: 1. Preorder: (output expression in prefix notation) - Visit Root - Traverse Left subtree in preorder - Traverse right subtree in preorder. 2. Inorder: (output expression in infix notation) - Traverse Left subtree in inorder - Visit Root - Traverse right subtree in inorder. 3. Postorder: (output expression in postfix notation) - Traverse Left subtree in Postorder - Traverse right subtree in Postorder. - Visit Root
Traversing Binary Trees: Implementation of Preorder traversal: public void pretraverse(treenode t) { if(t!=null) { Console.Write(t.getItem()); pretraverse(t.getlc()); // recursive traversal pretraverse(t.getrc()); // and now RC. } } pretraverse is a recursive method that will traverse the Left Child (LC) and Right Child (RC) in recursive manner.
Traversing Binary Trees: None-Recursive Preorder traversal (using Stack): Void pretraverse(treenode t) { Stack s; TreeNode n; s.init(); s.push(t); while(!s.empty()) { n = s.pop(); if(n!=null) { Console.WriteLine( N.getItem() ); s.push(n.getrc()); s.push(n.getlc()); } } }
Binary Search Trees (BST): A BST is: A Binary Tree where: Values in Left subtree < values in Node N < Values in right subtee. Example: 3, 2, 5, 9, 7, 1, 4, 10. Searching in BST: if key > current item move to RC else move to LC 1 2 3 4 5 9 7 10
Binary Search Trees (BST): TreeSort Algorithm: 1. Insert elements into BST 2. Perform inorder traversal Example: O M T C E P U Algorithm: 1. BST 2. Inorder (LNR) C E M O P T U C E M O P T U
Exercise: Trees 1. Construct expression tree whose PostOrder Traversal is: 2^2a*b*-b2^+ab-/ 2. Give Preorder, InOrder, and Postorder of the following expression tree X S R U T W Y Z V 3. Write a program to construct an expression tree, give a character string specifying its preorder traversal. 4. Write a function takes root and key, then search for the key in BST. 5. Write a function to construct a BST.