Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.

Similar presentations


Presentation on theme: "Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with."— Presentation transcript:

1 Chapter 05 Trees (Part II)

2

3 Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with n nodes in given, then for any node with index i, 1 ≦ i ≦ n,  Parent(i) is at if i > 1. If i=1, node i is the root.  LeftChild(i) is at 2i if 2i ≦ n.  If 2i > n, node i has no left child.  RightChild(i) is at 2i+1 if 2i+1 ≦ n.  If 2i+1 > n, node i has no right child.

4 Example ▫Node 1:  Parent(1): 0, indicating node 1 is the root.  LeftChild(1): 2i = 2.  RightChild(2): 2i+1 = 3. ▫Node 5:  Parent(5):  LeftChild(5):  RightChild(5): ▫Node 4:  Parent(4):  LeftChile(4):  RightChild(4): A BC DE 1 Complete binary tree FG 2 3 456 7 HI 89 0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 91011

5 Array Representation Advantages: ▫Suppose n is the number of nodes. The search time can be bounded to O(log 2 n). Disadvantages: ▫The utilization of memory space is not flexible enough.

6 Linked Representation class TreeNode { friend class Tree; private: int data; TreeNode *leftChild; TreeNode *rightChild; }; class Tree { public: //Tree operations private: TreeNode *root; }; class TreeNode { friend class Tree; private: int data; TreeNode *leftChild; TreeNode *rightChild; }; class Tree { public: //Tree operations private: TreeNode *root; }; data leftChildrightChild leftChild rightChild data

7

8 Introduction Goal: to visit each node in a tree. ▫In each node, 6 possible ways can be used to move forwarding: ▫LVR, LRV, VLR, VRL, RVL, and RLV.  L: moving left.  V: visiting the node.  R: moving right. ▫Different traversal approaches result in different order of output.

9 Introduction Conventionally, we traverse left before right. ▫LVR (Inorder traversal) ▫LRV (Postorder traversal) ▫VLR (Preorder traversal) V V LR 2 1 3 V V LR 1 2 3 V V LR 1 3 2

10 5.3.2 Inorder Traversal void Tree::Inorder() { Inorder(root); } void Tree::Inorder(TreeNode *currentNode) { if (currentNode) { Inorder(currentNode->leftChild); Visit(currentNode); Inorder(currentNode->rightChild); } void Tree::Inorder() { Inorder(root); } void Tree::Inorder(TreeNode *currentNode) { if (currentNode) { Inorder(currentNode->leftChild); Visit(currentNode); Inorder(currentNode->rightChild); }

11 Example Consider using a binary tree to represent the following expression. (((A / B) * C) * D) + E + *E *D / AB C + * * / AA A / / BB B * * Visiting order: CC C * * DD D + EE +E

12 5.3.2 Preorder Traversal Approach: VLR + *E *D / AB C + * * / AA A / / BB B * * Visiting order: CC C * * DD D + EE +E void Tree::Inorder(TreeNode *currentNode) { if (currentNode) { Visit(currentNode); Inorder(currentNode->leftChild); Inorder(currentNode->rightChild); } void Tree::Inorder(TreeNode *currentNode) { if (currentNode) { Visit(currentNode); Inorder(currentNode->leftChild); Inorder(currentNode->rightChild); }

13 5.3.2 Postorder Traversal Approach: LRV + *E *D / AB C + * * / AA A / / BB B * * Visiting order: CC C * * DD D + EE +E void Tree::Inorder(TreeNode *currentNode) { if (currentNode) { Inorder(currentNode->leftChild); Inorder(currentNode->rightChild); Visit(currentNode); } void Tree::Inorder(TreeNode *currentNode) { if (currentNode) { Inorder(currentNode->leftChild); Inorder(currentNode->rightChild); Visit(currentNode); }

14 5.3.5 Iterative Inorder Traversal By using stack. void Tree::NonrecInorder() { Stack S; TreeNode *currentNode = root; while (1) { while (currentNode) { S.Push(currentNode); currentNode = currentNode->leftChild; } if (S.IsEmpty()) return; currentNode = S.Pop(); Visit(currentNode); currentNode = currentNode->rightChild; } void Tree::NonrecInorder() { Stack S; TreeNode *currentNode = root; while (1) { while (currentNode) { S.Push(currentNode); currentNode = currentNode->leftChild; } if (S.IsEmpty()) return; currentNode = S.Pop(); Visit(currentNode); currentNode = currentNode->rightChild; } 1.When do we push into the stack? 2.When do we pop the stack? 1.When do we push into the stack? 2.When do we pop the stack?

15 5.3.5 Iterative Inorder Traversal By using stack. + *E *D / AB C + * * / AA A / / BB B Visiting order: + * * / A B currentNode: +**/ABNULL Stack

16 5.3.6 Level-Order Traversal Visiting order in level-order traversal: + *E *D / AB C 1 23 45 67 89

17 5.3.6 Level-Order Traversal By using queue. void Tree::LevelOrder() { Queue Q; TreeNode *currentNode = root; while (currentNode) { Visit(currentNode); if (currentNode->leftChild) Q.Push(currentNode->leftChild); if (currentNode->rightChild) Q.Push(currentNode->rightChild); if (Q.IsEmpty()) return; currentNode = Q.Pop(); } void Tree::LevelOrder() { Queue Q; TreeNode *currentNode = root; while (currentNode) { Visit(currentNode); if (currentNode->leftChild) Q.Push(currentNode->leftChild); if (currentNode->rightChild) Q.Push(currentNode->rightChild); if (Q.IsEmpty()) return; currentNode = Q.Pop(); }

18

19 5.6.1 Priority Queue The element to be deleted is the one with highest (or lowest) priority. Consider one insertion and one deletion for an ordered and a non-ordered list: ▫Using a heap, both insertion and deletion can be performed in O(logn) time InsertionDeletion Ordered ListO(n)O(n)O(1) Non-ordered ListO(1)O(n)O(n) where n is the number of elements in the list.

20 5.6.2 Definition of a Max Heap A max tree is a tree which the data in each node is no smaller than those in its children. A max heap is a max tree which is a complete binary tree at once. 9 35 124 8 65 14 7 6

21 The ADT of MaxHeap A heap can be implemented using array class MaxHeap { private: int *heap; //element array int heapSize; //number of elements in heap int capacity; //size of the array heap };

22 The Constructor for MaxHeap Note: heap[0] is dummy. MaxHeap::MaxHeap(int theCapacity = 10) { if (theCapacity < 1) throw “Capacity must be >= 1.”; capacity = theCapacity; heapSize = 0; heap = new int [capacity + 1]; } 1 0 23 4567 dummy The root is at heap[1].

23 5.6.3 Insertion into a Max Heap Example: Insert 5 into the heap 20 152 1410 1 23 4567 currentNode: Inititally: heapSize+1 6 5 2 2 5 3 20

24 5.6.3 Insertion into a Max Heap void MaxHeap::Push(int e) { if (heapSize+1 == capacity) Resize(); int currentNode = heapSize + 1; while (currentNode != 1 && heap[currentNode/2] < e) { heap[currentNode] = heap[currentNode / 2]; currentNode /= 2; } heap[currentNode] = e; }

25 Analysis of Push() Time complexity: ▫The insertion begins at a leaf and moves up toward the root. ▫the height of a complete binary tree with n elements is, the while loop is iterated O(logn) times. ▫Each iteration of this loop takes O(1) time. ▫Hence, the time complexity of O(logn).

26 5.6.4 Deletion from a Max Heap Example: delete 5 from the heap 20 155 14102 1 23 4567 currentNode: The element to be popped: 1 20 2 15 2 2 14 2 4

27 5.6.4 Deletion from a Max Heap int MaxHeap::Pop(int e) { if (IsEmpty()) throw “Heap is empty.”; int lastE = heap[heapSize – -]; int currentNode = 1; //root int Max = heap[currentNode]; int child = 2; //a child of currentNode while (child <= heapSize) { if (child < heapSize && heap[child] < heap[child+1]) child++; if (lastE >= heap[child]) break ; heap[currentNode] = heap[child]; currentNode = child; child *= 2; } heap[currentNode] = lastE; return Max; }

28 Analysis of Pop() Time complexity: ▫Suppose the number of elements in the heap is n. ▫Since the height of a heap is, the while loop is iterated O(logn) times. ▫Each iteration of this loop takes O(1) time. ▫Hence, the time complexity of pop() is O(logn).

29 Part I – Searching and Insertion

30 5.7.1 Defintion A binary search tree has the following properties: 1.The keys in all nodes are distinct. 2.The key in any node is larger than all keys in its left subtree. 3.The key in any node is smaller than all keys in its right subtree. 4.The left and right subtrees are also binary trees.

31 Examples Examples of binary search tree 20 15 25 121626 30 5 40 2 60 70 65 80 By using which traversal approach can we output the keys in increasing order?

32 5.7.2 Searching a Binary Search Tree Point: searching a binary tree in a recursive way. bool BST::Search(int k) { return Get(root, k); } bool BST::Search (TreeNode *p, int k) { if (!p) return false; if (p->key == k) return true; if (k key) return Search (p->leftChild, k); if (k > p->key) return Search (p->rightChild, k); } bool BST::Search(int k) { return Get(root, k); } bool BST::Search (TreeNode *p, int k) { if (!p) return false; if (p->key == k) return true; if (k key) return Search (p->leftChild, k); if (k > p->key) return Search (p->rightChild, k); }

33 5.7.2 Searching a Binary Search Tree Searching a binary tree in an iterative way. bool BST::IterativeSearch (int k) { TreeNode *currentNode = root; while (currentNode) { if (k == currentNode->data) return true; if (k data) currentNode = currentNode->leftChild; if (k > currentNode->data) currentNode = currentNode->rightChild; } return false; } bool BST::IterativeSearch (int k) { TreeNode *currentNode = root; while (currentNode) { if (k == currentNode->data) return true; if (k data) currentNode = currentNode->leftChild; if (k > currentNode->data) currentNode = currentNode->rightChild; } return false; }

34 Comparison Suppose h denote the depth of the tree. Time complexity RecursionO(h)O(h) IterationO(h)O(h) Space complexity RecursionO(h)O(h) IterationO(1)

35 5.7.3 Insertion into a Binary Search Tree Point: to find the position to insert first. If there is a duplicate key, then the insertion will not be performed. Consider to insert 35 into the tree: 30 5 40 216 80 30 5 40 216 80 35

36 5.7.3 Insertion into a Binary Search Tree bool BST::Insert (int k) { TreeNode *p = root, **pp = NULL; while (p) { pp = p; if (k == p->data) return false; if (k data) p = p->leftChild; if (k > p->data) p = p->rightChild; } p = new TreeNode(k); if (root) { if (k key) pp->leftChild = p; else (k > pp->key) pp->RightChild = p; } else root = p; return false; } bool BST::Insert (int k) { TreeNode *p = root, **pp = NULL; while (p) { pp = p; if (k == p->data) return false; if (k data) p = p->leftChild; if (k > p->data) p = p->rightChild; } p = new TreeNode(k); if (root) { if (k key) pp->leftChild = p; else (k > pp->key) pp->RightChild = p; } else root = p; return false; }


Download ppt "Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with."

Similar presentations


Ads by Google