CS 1371 Introduction to Computing for Engineers - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

CS 1371 Introduction to Computing for Engineers

Description:

From a given person in the family tree, we will be able to easily access ... Well, the records of that portion of your family tree were destroyed in a tragic fire... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 34
Provided by: david3049
Category:

less

Transcript and Presenter's Notes

Title: CS 1371 Introduction to Computing for Engineers


1
CS 1371Introduction to Computing for Engineers
  • Trees and BSTs

2
Administrivia
  • Schedule for the remainder of the class is on Web
    Work
  • NO recitations this week, but there is during
    DEAD week.
  • Homework 12 is out and due tomorrow Tuesday, Nov
    29th, at 10am, six hour grace period.
  • No help desk tonight instead.
  • Exam is Wed, Nov 30th
  • Review sessions Mon and Tues, Nov 28th and 29th
  • Lab 4 due Friday, Dec 2 midnight
  • Reading
  • Smith, Chapter 27
  • It's Monday

3
Background
  • There is a spectrum of complexity for Dynamic
    Data Structures
  • Lists
  • At most, one child
  • No cycles
  • Binary Trees
  • At most, two children
  • No cycles
  • N-ary Trees
  • Any number of children
  • Graphs
  • Any number of children
  • Cycles permitted

4
Trees
  • Hierarchical data structure
  • Nodes with 0 to many children
  • One special node designated root
  • Acyclic
  • Recursive definition

5
Binary Trees
  • Hierarchical data structure
  • Nodes with 0 to 2 children
  • One special node designated root
  • Acyclic
  • Recursive definition

6
Recursive Definition?
  • A binary tree is either
  • A null reference
  • A node with two children each of which is a
    binary tree
  • This recursive definition is why many tree
    algorithms are very simple when stated
    recursively
  • Easier case A LinkedList is either null or a
    Node whose Next is a Node.

7
The Family Tree
There are many different ways to represent a
family tree. The style we will show is an
ancestor family tree because only two parents
binary tree. From a given person in the family
tree, we will be able to easily access
information about the persons ancestors, but not
their descendants. Which means.
8
In this particular family tree, we create a new
node in the tree for a child. We record
certain information about the parent (name, date
of birth (dob), date of death (dod)) as well as
who the parents are (mother, father).
9
But if we stop to think about it, arent the
father and mother both children themselves?
So wouldnt they too have properties like name,
dob, dod and father and mother?
So we need a data definition that contains two
references one to the father and one to the
mother
10
Lets Develop a Data Definition
A child is a TreeNode containing
name, dob, dod, father, mother where father
and mother are child structures name is a
symbol and dob and dod are numbers
Looks good, right?
11
That Almost Works but
That data definition seems to workbut there are
problems. Lets say you have quite an extensive
family history going all the way back to your
great-great-great-great-great grandfather
Billy-Bob Jones born in the year 1810 to Well,
the records of that portion of your family tree
were destroyed in a tragic fire You dont
actually know who your great6 grandfather or
grandmother actually is
12
So We have to Reflect that Fact
Your father and mother values could be
non-existent. They could be null. So we change
our data definition a little
  • A FamilyNode is either
  • null or
  • A TreeNode containing
  • name, dob, dod, father, mother
  • where father mother are FamilyNode , name is a
    symbol, and dob and dod are numbers

13
So What have We Made?
A data structure that references itself twice If
we wanted to think about it visually
14
Lets Start with Information About You
null
null
15
We Add More About Our Family
null
null
null
null
16
And more
null
null
null
null
null
null
null
null
null
null
null
null
17
Trees in Generalthe Terminology
  • A tree consists of nodes.
  • Each node contains data as well as
    references/links/pointers to one or more other
    nodes. Any such reference might be null.
  • The first or top node in a tree is called the
    root node.
  • A parent node points (references/links) to child
    nodes.
  • Any node that has two null children references is
    called a leaf node.

18
The Best Type of Tree a Binary Tree
Probably the most famous type of tree in
Computer Science is related to the Binary
Tree. A binary tree is a tree structure in which
each node has AT MOST two child nodes linked
directly off the parent node. The ancestral tree
was a binary tree.
19
Root
Leaf
Leaf
null
null
null
null
null
20
Parent
Child
null
null
null
null
null
21
What About code?
Is this much more complex than what weve done
before? Lets see what a typical ADT and
function would look like for a binary tree
22
A tree is like a List only with a root
23
Observations
  • We made some choices
  • to indicate unknown parents
  • we choose null
  • to indicate that the person is still alive (no
    date of death)
  • we choose dod -1
  • can create an new node for each person
  • Hint helps to draw a picture first and only
    think about one node at a time

answer
left answer
right answer
24
Things we want to do in trees
  • Traverse trees like print everything think
    recursively. Or count.
  • BSTs "Binary Search Trees"
  • Need to compare stuff (homework!!)
  • Insert things into trees easy
  • Delete (remove) things a little harder

25
Counting
  • function num Ancestors(bt)
  • bt is a binary tree with data 'root'
  • num AncestorsR(bt.root) tree's root is a
    family node
  • function newcount AncestorsR(BTnode)
  • recursive in-order processing
  • if isempty(BTnode)
  • newcount 0
  • else
  • newcount AncestorsR(getLeft(BTnode)) 1
  • AncestorsR(getRight(BTnode)) depth first
  • end

26
Traversal Order Can Matter
  • How would you do it?
  • Think recursively!!! Easiest is called DEPTH
    FIRST SEARCH.
  • Can do L D R or D L R or .
  • First way is "in order", second way is
    "pre-order" used to build trees from the root.
  • E.g. the char mehtod In order
  • If I'm a node, my string is
  • left string
  • me
  • right string
  • Simple example non balanced tree

27
Generating a string in order
  • function str inOrderString(bt)
  • _at_BinaryTree\inOrderString
  • str inOrderString(bt)
  • str RinOrderString(bt.root)
  • function str RinOrderString(here)
  • recursive in-order processing
  • if isempty(here)
  • str ''
  • else
  • str RinOrderString(getLeft(here)) ' ' ...
  • char(here) ' ' ...
  • RinOrderString(getRight(here))
  • end

28
Searching Binary Trees
  • Is there a 42 in the tree?
  • Think recursively!!!
  • Answer yes if
  • If I'm 42
  • or
  • There is a 42 in the left
  • or
  • There is a 42 in the right
  • Otherwise answer no
  • That's it!!! but I had to check every node!

29
Unordered binary trees.
null
null
null
null
By itself, a binary tree has no inherent order.
There is no ordered relationship between the data
in the parent node and the data in the child
nodes.
30
Binary Search Trees
  • Binary Tree
  • Left subtree contains key values less than node
  • Right subtree contains key value greater than
    node
  • Numbers or anything "comparable"
  • No duplicates

Normally handled specially
31
Searching a BST
42
41
32
Binary Search Trees
  • Performance
  • Search O(log N)
  • Insert O(log N)
  • Traverse O(N)
  • Performance O()'s assume tree is relatively
    balanced

This is why we do this!!!
33
Comparing Objects in trees
  • In Tree Nodes, and thus Trees, "data" can be any
    object
  • But in BST, need to compare (
  • So, the data can't be any Object, but must be a
    class that supports a method CompareTo or GE or
    something like that used in your code.
Write a Comment
User Comments (0)
About PowerShow.com