1.45k likes | 1.47k Views
Dive into the detailed overview of tree structures, including binary and AVL trees, as well as priority queues like heaps and leftist trees in the CSE233 course from POSTECH. Explore various representations, terminology, and practical implementations.
E N D
CSE233 Course Review CSE, POSTECH
What we studied in the 1st half Week 1 : Overview of C++, Program performance Week 2 : Array-based and linked representations of Lists Week 3 : Arrays and Matrices Week 4 : Performance Measurement Week 5 : Stacks Week 6 : Queues Week 7 : Skip lists and Hashing Week 8 : Review and Midterm Exam
What we studied in the 2nd half Week 9 : Binary and other trees Week 10 : Priority queues, Heaps, Leftist trees Week 11 : Tournament trees and Bin packing Week 12 : Binary search trees Week 13 : AVL trees Week 14 : Graphs Week 15 : Graph Search Methods Week 16 : Review and Final exam
Tree Terminology • A treet is a finite nonempty set of elements • The element at the top is called the root • The remaining elements, if any, are partitioned into trees, which are called the subtrees of t. • 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 at the lowest level of the hierarchy are the leaves.
Leaves, Parent, Grandparent, Siblings, Ancestors, Descendents Leaves = {Mike,AI,Sue,Chris} Parent(Mary) = Joe Grandparent(Sue) = Mary Siblings(Mary) = {Ann,John} Ancestors(Mike) = {Ann,Joe} Descendents(Mary)={Mark,Sue}
level 1 level 2 level 3 level 4 Levels and Height • Root is at level 1 and its children are at level 2. • Height = depth = number of levels
Node Degree • Node degree is the number of children it has
Tree Degree • Tree degree is the maximum of node degrees tree degree = 3
Binary Tree • A finite (possibly empty) collection of elements • A nonempty binary tree has a root element and the remaining elements (if any) are partitioned into two binary trees • They are called the left and right subtrees of the binary tree
- different when viewed as a binary tree a a - same when viewed as a tree b c c b Difference Between a Tree & a Binary Tree • A binary tree may be empty; a tree cannot be empty. • No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. • The subtrees of a binary tree are ordered; those of a tree are not ordered.
Binary Tree for Mathematical Expressions Figure 11.5 Expression Trees
Full Binary Tree • A full binary tree of height h has exactly 2h-1 nodes. • Numbering the nodes in a full binary tree • Number the nodes 1 through 2h-1 • Number by levels from top to bottom • Within a level, number from left to right
Complete Binary Tree with N Nodes • Start with a full binary tree that has at least nnodes • Number the nodes as described earlier. • The binary tree defined by the nodes numbered 1 through n is the n-node complete binary tree. • A full binary tree is a special case of a complete binary tree
Example of Complete Binary Tree • Complete binary tree with 10 nodes.
Fig. 11.8 Incomplete binary trees Incomplete Binary Trees • Complete binary tree with some missing elements
Binary Tree Representation • Array representation • Linked representation
Array Representation of Binary Tree • The binary tree is represented in an array by storing each element at the array position corresponding to the number assigned to it.
Right-Skewed Binary Tree • An n node binary tree needs an array whose length is between n+1 and 2n. • Right-skewed binary tree wastes the most space • What about left-skewed binary tree?
Linked Representation of Binary Tree • The most popular way to present a binary tree • Each element is represented by a node that has two link fields (leftChild and rightChild) and an element field
Priority Queues • A priority queue is a collection of zero or more elements each element has a priority or value • Unlike the FIFO queues, the order of deletion from a priority queue (e.g., who gets served next) is determined by the element priority • Elements are deleted by increasing or decreasing order of priority rather than by the order in which they arrived in the queue
Priority Queues • Operations performed on priority queues 1) Find an element, 2) insert a new element, 3) delete an element, etc. • In a Min priority queue, find/delete operation finds/deletes the element with minimum priority • In a Max priority queue, find/delete operation finds/deletes the element with maximum priority • Two or more elements can have the same priority
Implementation of Priority Queues • Implemented using heaps and leftist trees • Heap is a complete binary tree that is efficiently stored using the array-based representation • Leftist tree is a linked data structure suitable for the implementation of a priority queue
Max (Min) Tree • A max tree (min tree) is a tree in which the value in each node is greater (less)than or equal to those in its children (if any) • Nodes of a max or min tree may have more than two children (i.e., may not be binary tree)
Heaps - Definitions • A max heap (min heap) is a max (min) tree that is also a complete binary tree
Array Representation of Heap • A heap is efficiently represented as an array.
9 8 7 6 7 2 6 5 1 20 Insertion into a Max Heap • New element is 20 • Are we finished?
9 8 7 6 20 2 6 5 1 7 Insertion into a Max Heap • Exchange the positions with 7 • Are we finished?
9 20 7 6 8 2 6 5 1 7 Insertion into a Max Heap • Exchange the positions with 8 • Are we finished?
20 9 7 6 8 2 6 5 1 7 Insertion into a Max Heap • Exchange the positions with 9 • Are we finished?
20 15 7 6 9 2 6 5 1 7 8 Deletion from a Max Heap • Max element is in the root • What happens when we • delete an element?
15 7 6 9 2 6 5 1 7 8 Deletion from a Max Heap • After the max element is removed. • Are we finished?
15 7 6 9 2 6 5 1 7 8 Deletion from a Max Heap • Heap with 10 nodes. • Reinsert 8 into the heap.
8 15 7 6 9 2 6 5 1 7 Deletion from a Max Heap • Reinsert 8 into the heap. • Are we finished?
15 8 7 6 9 2 6 5 1 7 Deletion from a Max Heap • Exchange the position with 15 • Are we finished?
15 9 7 6 8 2 6 5 1 7 Deletion from a Max Heap • Exchange the position with 9 • Are we finished?
Max Heap Initialization • Heap initialization means to construct a heap by adjusting the tree if necessary • Example: input array = [-,1,2,3,4,5,6,7,8,9,10,11]
Max Heap Initialization - Start at rightmost array position that has a child.