Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.

Similar presentations


Presentation on theme: "Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees."— Presentation transcript:

1 Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf (ab_sarraf@aut.ac.ir) Trees

2 Dept. of Computer Eng. & IT, Amirkabir University of Technology Introduction A BC GH D EF KLM IJ Figure 5.2 : A sample tree level 1 2 3 4

3 Dept. of Computer Eng. & IT, Amirkabir University of Technology List Representation A 0 B F 0 CG 0 D IJ 0 E K L 0HM 0 Figure 5.3 : List representation of the tree of Figure 5.2

4 Dept. of Computer Eng. & IT, Amirkabir University of Technology Left child-right sibling representation node structure the left child field of each node points to its leftmost child (if any), and the right sibling field points to its closest right sibling (if any) data left childright sibling A B C G H D EF KLM I J

5 Dept. of Computer Eng. & IT, Amirkabir University of Technology Representation as a degree-two tree rotate the right-sibling pointers clockwise by 45 degrees the two children of a node are referred to as the left and right children A BB A B C G H D E F K L MI J Figure 5.7 : Left child-right child tree representation

6 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Trees (a) (b) A C F G B E D I H A B C E D Figure 5.10 : Skewed and complete binary trees level 1 2 3 4 5

7 Dept. of Computer Eng. & IT, Amirkabir University of Technology Properties of Binary Trees Lemma 5.2 [Maximum number of nodes] (1) The max number of nodes on level i of a binary tree is 2 i -1, i≥ 1 (2) The max number of nodes in a binary tree of depth k is 2 k -1, k≤ 1 A full binary tree of depth k a binary tree of depth k having 2 k -1 nodes, k≥ 0

8 Dept. of Computer Eng. & IT, Amirkabir University of Technology Figure 5.11 : Full binary tree of depth 4 with sequential node numbers 1 2 1011 6 3 45 8912 13 7 1415

9 Dept. of Computer Eng. & IT, Amirkabir University of Technology A binary tree with n nodes and depth k is complete its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k The height of a complete binary tree with n nodes is

10 Dept. of Computer Eng. & IT, Amirkabir University of Technology LeftChilddataRightChild LeftChild Figure 5.13 : Node representations data RightChild

11 Dept. of Computer Eng. & IT, Amirkabir University of Technology (b) A C F G B E D I H A BC 0F0D0G0 0H00I0 E0 root Figure 5.14 : Linked representation for the binary trees of Figure 5.10

12 Dept. of Computer Eng. & IT, Amirkabir University of Technology 5.3.1 Introduction Tree traversal visiting each node in the tree exactly once a full traversal produces a linear order for the nodes Order of node visit L : move left V : visit node R : move right possible combinations : LVR, LRV, VLR, VRL, RVL, RLV traverse left before right LVR : inorder LRV : postorder VLR : preorder

13 Dept. of Computer Eng. & IT, Amirkabir University of Technology Constructing the original tree from inorder/preorder Constructing the original tree from inorder/postorder Constructing the original tree from preorder/postorder

14 Dept. of Computer Eng. & IT, Amirkabir University of Technology 5.6 Heaps(Cont ’ ) 14 7 6 12 8 10 2 4 6 7 8 9 36 5 8320 50 30 25 11 21 Figure 5.24 : Max heaps Figure 5.25 : Min heaps

15 Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps 5.6.3 Insertion into Max Heap Examples 20 215 10 14 21 2015 10 142 20 15 10 142 Figure 5.26 : Insertion into a max heap (a) 5 (b) (d)(c)

16 Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont ’ ) Implementation need to move from child to parent heap is complete binary tree use formula-based representation Lemma 5.4 : parent(i) is at if i≠ 1 Complexity is O(log n)

17 Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont ’ ) template void MaxHeap ::Insert(const Element &x) // insert x into the max heap { if (n = = MaxSize) {HeapFull(); return;} n++; for (int i = n; 1; ) { if (i = = 1) break; // at root if (x.key <= heap[i/2].key) break; // move from parent to i heap[i] = heap[i/2]; i /= 2; } heap[i] = x; } Program 5.18 : Insertion into a max heap

18 Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont ’ ) Deletion from Max Heap Example 20 6 15 10 14 15 214 10 2 (a)(b) (c) Figure 5.27 : Deletion from a heap

19 Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont ’ ) template Element *MaxHeap ::DeleteMax(Element &x) // Delete from the max heap { if (!n) {HeapEmpty(); return 0;} x = heap[1]; Element k = heap[n]; n--; for (int i = 1, j = 2; j <= n; ) { if (j<n) if (heap[j].key < heap[j+1].key) j++; // j points to the larger child if (k.key >= heap[j].key) break; heap[i] = heap[j]; // move child up i = j; j *= 2; // move i and j down } heap[i] = k; return &x; } Program 5.19 : Deletion from a max heap

20 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees Definition binary tree which may be empty if not empty (1) every element has a distinct key (2) keys in left subtree < root key (3) keys in right subtree > root key (4) left and right subtrees are also binary search trees

21 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont ’ ) 20 25 22 15 10 12 30 405 2 60 70 8065 Figure 5.28 : Binary trees (a) (b) (c)

22 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont ’ ) Searching Binary Search Tree Recursive search by key value definition of binary search tree is recursive key(element) = x x=root key : element=root x<root key : search left subtree x>root key : search right subtree Complexity : O(h)

23 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont ’ ) Insertion into Binary Search Tree New element x search x in the tree success : x is in the tree fail : insert x at the point the search terminated 30 40 80 5 2 Figure 5.29 : Inserting into a binary search tree (a) Insert 80 (b) Insert 35 30 40 80 5 35 2

24 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont ’ ) template Boolean BST ::Insert(const Element &x) // Insert x into the binary search tree { // Search for x.key, q is parent of p BstNode *p = root; BstNode *q = 0; while(p) { q = p; if (x.key = = p->data.key) return FALSE; // x.key is already in tree if (x.key data.key) p = p->LeftChild; else p = p->RightChild; } // Perform insertion p = new BstNode ; p->LeftChild = p->RightChild = 0; p->data = x; if (!root) root = p; else if (x.key data.key) q->LeftChild = p; else q->RightChild = p; return TRUE; }

25 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont ’ ) Deletion from Binary Search Tree Leaf node corresponding child field of its parent is set to 0 the node is disposed Nonleaf node with one child the node is disposed child takes the place of the node Nonleaf node with two children node is replaced by either the largest node in its left subtree the smallest node in its right subtree delete the replacing node from the subtree

26 Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont ’ )


Download ppt "Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees."

Similar presentations


Ads by Google