BST

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ind Kth element Add Count field to each node ! Count(): primitive that extracts value of Count(): count count from specified node ! Find_Kth(Node, k) if if ( (Empty(Left_Child(Node)) && && (Empty(Right_Child(Node))) return Info(Node) if (k <= Count(Node)) return Find_Kth(Left_Child(Node), k) return Find_Kth(Right_Child(Node), k – Count(Node)) 12 Add_Kth(Node, Info, k) if (Empty(Node)) return Create(Info, null, null) if ( (Empty(Left_Child(Node)) && (Empty(Right_Child(Node))) B1 ← Create(Info, null, null) if (k == 1) B2 ← Create(Info, B1, Node) else B2 ← Create(Info, Node, B1) Count(B2) ← 1 return B2 if (k <= Count(Node)) Left_Child(Node) ← Add_Kth(Left_Child(Node), Info, k) Count(Node) ← Count(Node) + 1 return Node else Right_Child(Node) ← Add_Kth(Right_Child(Node), Info, k – Count(Node)) 13 return Node Example of Add_Kth() ! Add_Kth(Root, Z, 4): 3 (Z, 4) " Right (Z, 1) " Left Create (Z, , ) Create (Z, B1, D) Count = 1 1 Z 1 A 1 B Z 1 D E C 14 Example of Add_Kth() ! Add_Kth(Root, Z, 4): 3 (Z, 4) " Right (Z, 1) " Left update Left update Count 1 1 A 1 B Z 2 1 D E C 15 Example of Add_Kth() ! Add_Kth(Root, Z, 4): 3 (Z, 4) " Right 1 A Done! 2 1 B 1 C Z E D 16 Notes Notes on Lists as BTs ! Time Complexity – Due to the same properties of BST, all three Due functions functions on average function in O(log2n) O(log ! Memory efficiency – Need to store n-1 additional nodes (non-leaf leaf node) node) – About half of memory is “wasted” 17...
View Full Document

This document was uploaded on 03/06/2014 for the course ECE 368 at Purdue University-West Lafayette.

Ask a homework question - tutors are online