Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

In a max-heap the root is higher than or equal to the values in its ... node to move up for 'max' heap. Consider data set: 9/8/09. B. Ramamurthy. 25. Example ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 31
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:
Tags: trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • B.Ramamurthy
  • Chapter 10
  • CS114A,B

2
Introduction
  • Data organizations we studied are linear in that
    items are one after another.
  • Data organization can also be hierarchical
    whereby an item can have more than one immediate
    successor.
  • Thus ADT can be classified as linear and
    non-linear.
  • We will study non-linear ADTs tree, binary tree
    and binary search tree and graph in this
    discussion.

3
Topics for discussion
  • Terminology
  • Binary Tree -- Formal definition
  • Binary Search tree
  • Tree Traversal
  • Full binary tree
  • Complete binary tree
  • Balanced Tree
  • Summary

4
Terminology
  • Trees are used to represent relationships items
    in a tree are referred to as nodes and the lines
    connecting the nodes that express the
    hierarchical relationship are referred to as
    edges.
  • The edges in a tree are directed.
  • Trees are hierarchical which means that a
    parent-child relationship exist between the nodes
    in the tree.
  • Each node has at most one parent. The node with
    no parents is the root node.
  • The nodes that have no successors (no children
    nodes) are known as leaf nodes.

5
Basic Tree Anatomy
Level 0
Level 1
Level 2
6
Binary Tree formal definition
  • A binary tree is a set T of nodes such that
    either
  • T is empty, or
  • T is parameterized into three disjoint sets
  • A single node r, the root
  • Two possibly empty sets that are binary trees
    called left and right subtrees of r.
  • Example Binary trees to represent algebraic
    expressions.

7
Tree Types
  • Binary tree
  • Full binary tree
  • Complete binary tree
  • Binary Search Tree

8
Binary tree example
A - B / C
A
\
C
B
How do you traverse this tree?
9
Binary Search Tree
  • A binary search tree is a binary tree that is in
    a sense sorted according to the values in its
    nodes.
  • For each node n, a binary search tree satisfies
    the following three properties
  • 1. ns value is greater than all values in its
    left subtree TL.
  • 2. ns value is less than all its values in its
    right subtree TR.
  • 3. Both TL and TR are binary search tree.

10
Example Tree of names
Jane
Bob
Tom
Ellen
Alan
Wendy
Nancy
Where will you insert the name Randy? How about
Ian?
11
Hwk3 Q1
12
Hwk3 Q2
Due Date 4/9/99
13
Types of Trees
trees
dynamic
static
game trees
search trees
priority queues and heaps
graphs
Huffman coding tree
14
Complete Binary Tree
  • A complete binary tree is a binary tree
  • with leaves on either a single level or on two
    adjacent levels
  • such that the leaves on the bottommost level are
    placed as far left as possible
  • such that nodes appear in every possible position
    in each level above the bottom level.

15
Example
Complete Binary Tree
Incomplete Binary Tree
16
Example (contd.)
Complete Binary Tree
17
A Contiguous Representation for a complete binary
tree
1
Lets number the nodes starting from the
root, level by level, left to right
2
3
5
4
7
6
8
9
11
10
12
18
Finding nodes in Contiguous Representation
Let Aj be a node and n be the number of
nodes. Left child of Aj is A2j if 2j lt
n Right child of Aj is A2j1 id 2j1 lt
n Parent of Aj is Aj/2 if j gt 1 Root is
A1 If 2j gt n the Aj is a leaf. Lets look
at an example (from last years final exam)
19
Exercise
Given a complete binary tree with 2670 nodes and
with contiguous representation A1 to
A2670 what is the maximum level l of the
tree? How many nodes are in the level n where n lt
l ? Give an expression in terms of n. Derive the
expression for number of internal nodes of the
tree. How many leaf nodes are there in this tree?
Where is the parent of node A261
located? Where are the children of node at A261
located?
20
Exercise (contd.)
21
Heap A Formal Definition
  • Heap is a loosely ordered complete binary tree.
  • A heap is a complete binary tree with values
    stored in its nodes such that no child has a
    value greater than the value of its parent.
  • A heap is a complete binary tree
  • 1. That is empty or
  • 2a. Whose root contains a search key greater than
    or equal to both its children node.
  • 2b. Whose left subtree and right subtree are
    heaps.

22
Types of heaps
  • Heaps can be max heaps or min heaps. Above
    definition was for a max heap.
  • In a max-heap the root is higher than or equal to
    the values in its left and right child.
  • In a min-heap the root is smaller than or equal
    to the values in its left and right child.

23
ADT Heap
  • createHeap ( )
  • destroyHeap ( )
  • empty ( )
  • heapInsert (NewItem)
  • heapDelete ( RootNode)

24
Example
Consider data set
25
Example
1
26
Delete Root Item
  • In a max heap the root item is the largest and is
    the chosen one for deletion.
  • 1. After deletion of root, two disjoint heaps
    result.
  • 2. Place last node as a root, to form a
    semi-heap.
  • 3. Use trickle-down to form a heap.
  • Growth rate function 3log N 1 O(log N)
  • Consider a heap example discussed above.
  • Delete root item.

27
Insert An Item
  • 1. Insert a node as a last node.
  • 2. Trickle up (repeat for various levels) to form
    a heap.
  • Consider inserting 15 into the heap of the
    delete example.
  • Insert is also a O(log N) operation.

28
Insert Node Example
15
5
9
3
6
2
29
Priority Queue
  • Priority Queue implemented as a heap. Priority
    queue inserts are done according to some criteria
    known as priority
  • Insert location are according to priority and
    delete are to the head of queue.
  • PQueue constructor
  • PQueueInsert
  • PQueueDelete
  • Data
  • Heap PQ

30
Heap Sort
  • Heapsort
  • 1. Represent elements as a max-heap.
  • 2. Delete item at root, as it is the largest.
  • 3. Heap, repeat step 2, until one item is left.
  • O(Nlog N) in the worst case!
Write a Comment
User Comments (0)
About PowerShow.com