Tree Definition - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Tree Definition

Description:

Tree Node Level and Path Len. Binary Tree Definition. Selected Samples / Binary Trees ... Alice. Abigail. Agnes. Angela. Alice. Angela. Alice. 4. Main Index ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 50
Provided by: alan324
Category:
Tags: agnes | definition | tree

less

Transcript and Presenter's Notes

Title: Tree Definition


1
Chapter 10 Binary Trees
Tree Definition Tree Structures Tree Node Level
and Path Len. Binary Tree Definition Selected
Samples / Binary Trees Binary Tree Nodes Binary
Search Trees Locating Data in a Tree Removing a
Binary Tree Node Using Binary Search Trees -
Removing Duplicates Update Operations Removing an
Item From a Binary Tree Summary Slides
2
Tree Definition
  • Sequence containers
  • Arrays, vectors, list, etc
  • Access items by position
  • Associate containers
  • Access items by value/key
  • Example Employee (SNN, Name, Gender, Age,
    Salary)
  • A tree T is a hierarchical structure that places
    elements in nodes along branches that originate
    from a root.
  • (a) T is a tree if the set of nodes is empty.
    (An empty tree is a tree.)
  • (b) Each node (except the root) in T has
    exactly one predecessor, and can have
    multiple successors.
  • A tree is a nonlinear structure. A tree is a
    natural representation for information that is
    organized in a hierarchical fashion

3
Tree Definition
  • The idea of a tree can be found frequently in
    everyday life
  • sports events are organized using trees
  • nodes represent the contestants
  • the winner of each pairing advances to the next
    level
  • the winner of the last paring is declared the
    winner of the tournament

Alice
Alice
Abigail
Alice
Agnes
Angela
Angela
4
Why Trees?
  • Trees combine the advantages of two other
    structures
  • an ordered array and
  • a linked list

5
Ordered array
  • An ordered array
  • quick in terms of static operations
  • quick to search an ordered array for a particular
    value, using a binary search O(log2 N)
  • quick to iterate through an ordered array,
    visiting each object in sorted order
  • slow in terms of dynamic operations
  • insert a new object
  • find where the object will go
  • move all the objects with greater keys up one
    space in the array to make room for it (O(N))
  • delete an object
  • same multi-move operation

6
Linked lists
  • A linked list
  • quick in terms of dynamic operations
  • insertions and deletions
  • changing a few references O(1)
  • slow in terms of static operations
  • finding a specified element in a linked list
  • average N/2 objects O(N)
  • how about an ordered linked list?

7
Trees
  • Trees provide
  • quick insertion and deletion of a linked list,
    and
  • quick searching of an ordered array
  • A general tree
  • each node can have any number of children
  • A particular kind of tree
  • a binary tree
  • a binary search tree

8
Terms (1/2)
  • node
  • edge
  • root
  • the node that does not have a parent
  • leaf node
  • a node that does not have children
  • interior node
  • a node that has a parent and children

9
Terms (2/2)
  • path
  • there is a single unique path along arcs/edges
    from the root to any particular node
  • levels
  • the level of a particular node refers to how many
    generations the node is from the root
  • the root is level 0
  • height
  • the largest number of arcs traversed in moving
    from root to any leaf is known as the height of
    the tree
  • the longest path
  • the highest level
  • size
  • the number of nodes a tree has
  • density
  • a measure of the size of a tree relative to the
    depth of the tree
  • advantages of a tree with higher density

10
Tree Structures
11
Tree Node Level and Path Length
12
Binary Tree Definition
  • A binary tree T is a finite set of nodes with one
    of the following properties
  • (a) T is a tree if the set of nodes is empty.
    (An empty tree is a tree.)
  • (b) The set consists of a root, R, and exactly
    two distinct binary trees, the left
    subtree TL and the right subtree TR.
    The nodes in T consist of node R and all
    the nodes in TL and TR.
  • (c) Recursive structures

13
Tree Node Level and Path Length Depth Discussion
14
Tree Node Level and Path Length Depth Discussion
15
Tree Node Level and Path Length Depth Discussion
16
Tree Node Level and Path Length Depth Discussion
17
Height vs. number of nodes
20
height 0
21
height 1
22
height 2
A complete binary tree with height n contains
nodes N 20 21 22 2h (2(h1)
1) /(2 1) 2(h1) - 1
18
Selected Samples of Binary Trees
19
Some Types of Binary Trees
  • Expression tree
  • Each node contains an operator or an operand
  • Huffman tree
  • Represents Huffman codes for characters that
    might appear in a text file
  • Huffman code uses different numbers of bits to
    encode letters as opposed to ASCII or Unicode
  • Binary search trees
  • All elements in the left subtree precede those in
    the right subtree

20
Some Types of Binary Trees (continued)
21
Fullness and Completeness
  • Trees grow from the top down
  • Each new value is inserted in a new leaf node
  • A binary tree is full if every node has two
    children except for the leaves

22
Binary Tree Nodes
23
  • template lttypename Tgt
  • class tnode
  • public
  • // tnode is a class implementation structure.
    making the
  • // data public simplifies building class
    functions
  • T nodeValue
  • tnodeltTgt left, right
  • // default constructor. data not initialized
  • tnode()
  • // initialize the data members
  • tnode (const T item, tnodeltTgt lptr NULL,
  • tnodeltTgt rptr NULL)
  • nodeValue(item), left(lptr), right(rptr)

24
Binary Tree Scan
  • A binary tree T is a nonlinear structure, so a
    linear scan is impossible.
  • Without organization, we may skip some nodes, and
    access some nodes more than once.
  • T is a recursive structure, so we can use
    recursive algorithm to scan the tree, visiting
    each node exactly once.
  • Depending on the order, there are 3 scan types
  • Preorder scan (NLR)
  • Inorder scan (LNR)
  • Postorder scan (LRN)

25
Binary Tree Scan
  • Inorder scan
  • Traverse the left subtree (go left).
  • Visit the node.
  • Traverse the right subtree (go right).

26
Binary Search Tree
  • A binary search tree T orders the elements by
    using the relational operator lt . The ordering
    compares each node with the value of elements in
    its subtree
  • For each node, the data values in the left
    subtree are less than the value of the node, and
    the data values in the right subtree are greater
    than the value of the node.
  • No duplicate values by default.
  • Binary search trees are important
  • they occur often in algorithms
  • they can be efficiently manipulated
  • they are the simplest tree structure

27
Binary Search Trees
28
Current Node Action -LOCATING DATA IN A
TREE- Root 50 Compare item 37 and
50 37 lt 50, move to the left
subtree Node 30 Compare item 37 and
30 37 gt 30, move to the right
subtree Node 35 Compare item 37 and
35 37 gt 35, move to the right
subtree Node 37 Compare item 37 and 37.
Item found.
29
Removing a Binary Search Tree Node
30
(No Transcript)
31
(No Transcript)
32
(No Transcript)
33
(No Transcript)
34
Using Binary Search Trees Application Removing
Duplicates
35
Update Operations 1st of 3 steps
Insert an item with the value of 32 1)- The
function begins at the root node and compares
item 32 with the root value 25. Since 32 gt 25,
we traverse the right subtree and look at node
35.
36
Update Operations 2nd of 3 steps
2)- Considering 35 to be the root of its own
subtree, we compare item 32 with 35 and
traverse the left subtree of 35.
37
Update Operations 3rd of 3 steps
1)- Create a leaf node with data value 32. Insert
the new node as the left child of node
35. newNode getSTNode(item,NULL,NULL,parent)
parent-gtleft newNode
38
Removing an Item From a Binary Tree
39
Removing an Item From a Binary Tree
40
Removing an Item From a Binary Tree
41
Removing an Item From a Binary Tree
42
Removing an Item From a Binary Tree
43
Removing an Item From a Binary Tree
44
Removing an Item From a Binary Tree
45
Summary Slide 1
- trees - hierarchical structures that place
elements in nodes along branches that
originate from a root. - Nodes in a tree are
subdivided into levels in which the topmost
level holds the root node. - Any node in a
tree may have multiple successors at the
next level. Hence a tree is a non-linear
structure. - Tree terminology with which you
should be familiar parent child
ancestors descendents leaf node
interior node subtree successors edge
siblings level depth height size
degree density
46
Summary Slide 2
- Binary Trees - Most effective as a storage
structure if it has high density - data
are located on relatively short paths from the
root. - A complete binary tree has
the highest possible density - an n-node
complete binary tree has depth int(log2n).
47
Summary Slide 3
- Traversing Through a Tree - There are six
simple recursive algorithms for tree
traversal. - The most commonly used ones
are 1) inorder (LNR) 2) postorder
(LRN) 3) preorder (NLR). - Another technique
is to move left to right from level to
level. - This algorithm is iterative, and
its implementation involves using a queue.
48
Summary Slide 4
- A binary search tree stores data by value
instead of position - It is an example of an
associative container. - The simple
rules return lt go
left gt go right until finding a
NULL subtree make it easy to build a binary
search tree that does not allow duplicate
values.
49
Summary Slide 5
-The insertion algorithm can be used to define
the path to locate a data value in the
tree. -The removal of an item from a binary
search tree is more difficult and involves
finding a replacement node among the remaining
values. difference cases for removing an item
from a binary search tree
Write a Comment
User Comments (0)
About PowerShow.com