Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

DOM trees (browsers) cider. like. red. I. apple. and. wine. A tree is a set of nodes and a set of directed edges that connects pairs of nodes. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 16
Provided by: DonSl1
Category:
Tags: apple | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Lecture 21
  • Trees

2
Trees
  • A useful data structure for many applications

3
Applications
  • Compiler design
  • text processing (dictionary)
  • searching algorithms
  • DOM trees (browsers)

cider
like
red
I
apple
and
wine
4
Trees Ctd
  • A tree is a set of nodes and a set of directed
    edges that connects pairs of nodes.
  • A tree is a a Directed, Acyclic Graph (DAG) with
    the following properties
  • - one vertex is distinguished as the
    root no edges enter this vertex
  • - every other vertex has exactly one
    entering edge

5
Trees
  • thus, there exists a unique path from the root to
    any vertex
  • Every node can have up to two(in a binary tree)
    other nodes connected to it, which we call the
    children.
  • Left and right child

6
Terminology
  • parent, child, sibling -
  • immediate predecessor of a node is its parent
  • immediate successor is a child
  • ancestor, descendant -
  • if path from root to node a goes through node b,
    b is an ancestor of a
  • conversely, a is a descendant of b

7
Definitions
  • Path length
  • the number of edges that must be followed
  • leaf node
  • a node that has no children
  • Depth of a node
  • length of the path from root to the node
  • height of a tree
  • number of nodes in the longest path from root to
    a leaf node

8
More Definitions
  • internal node -
  • not a leaf
  • subtree at v -
  • treat v as if it were a root
  • height of vertex v -
  • length of longest path from v to a leaf
  • height of tree -
  • height of root
  • depth of v -
  • length of path from root to v
  • ordered tree -
  • children of each vertex are distinguished
    (ordered left to right)

9
Binary Search Trees
  • Binary Search Tree
  • An ordered tree in which the nodes can have 0, 1,
    2 children
  • Complete -
  • bottom row missing nodes only on right
  • Full, complete -
  • all rows full, no missing nodes on any level (max
    nodes for height)
  • nodes in full, complete tree of height k?
  • leaves?
  • Conversely, height of full, complete tree with n
    nodes?

10
Implementation
  • Consider the following definition of a node
  • class Node
  • type data
  • Node left
  • Node right
  • Node(type n)datan leftNULLrightNULL

data
left
right
11
Implementation
  • Node root
  • root new Node(10)
  • Node nextnode new Node(5)
  • // make this node the left child of the root
  • Root.left nextnode
  • // create another node and make it the right node
  • nextnode new Node(20)
  • Root.right nextnode
  • Now let us build a tree according to the
    following criteria.
  • Make the first number the root of the tree
  • For every subsequent number, use the following
    criteria to determine its place in the tree
  • if the node is bigger, go right
  • if the node is smaller, go left

12
Implementation
  • Open a file
  • read a num
  • Node root new Node(num)
  • while (!infile.eof())
  • int num infile.getInt()
  • Node nextnode new Node(num)
  • Node ptr root
  • Node prev
  • while (ptr ! null)
  • prev ptr
  • if (num gt ptr.data) ptr
    ptr.right
  • if (num lt ptr.data) ptr ptr.left
  • if (num gt prev.data) pre.right
    nextnode
  • else prev.left nextnode

13
Recursive Insert
  • void Insert(Node t, string val) // Note we pass
    our tree ptr in by ref
  • // since we
    need to modify our left right ptrs
  • if (t null) //
    within each node
  • tnew Node( val)
  • return
  • if (t.data val)
  • cout ltlt "Ignoring Dupe\n"
  • return
  • if ( val lt t.data )
  • Insert(t.left, val )
  • else
  • Insert(t.right, val )

14
Tree Traversal
  • Three methods of tree traversal
  • inorder
  • left, root, right
  • preorder
  • root, left, right
  • postorder
  • left, right, root

15
Inorder Traversal
  • void print(Node ptr)
  • if (ptr ! NULL)
  • print(ptr.left)
  • System.out.print(ptr.data)
  • print(ptr.right)
  • How does this work?

8
12
6
14
10
4
7
Write a Comment
User Comments (0)
About PowerShow.com