Trees - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Trees

Description:

Binary tree: every node has at most 2 children (left child and right child) ... Suppose a binary tree has n nodes, and height h. The total numbers of nodes: h ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 44
Provided by: charle106
Category:
Tags: tree | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • Trees
  • Binary tree and Binary tree traversals
  • Expression trees
  • Infix, Postfix, Prefix notation
  • Binary Search Trees
  • Operations supported
  • Complexity

2
(Directed) Tree
3
Definition of Trees
  • /\ is a tree. It is called an empty tree.
  • If is a node and
  • are non-empty trees where k ? 1, then
  • is a tree.

4
Notation about Trees
  • root, subtrees
  • parent, child, sibling
  • leaves, internal nodes
  • edge
  • path, length
  • ancestor, descendant
  • depth, height
  • Root at depth 0,
  • One node tree height 0

5
Tree criteria
  • Always a single root
  • A single path to every node
  • Doesnt matter how we draw it

6
Trees ?
4
7
Game tree
  • 2 7
  • 1 3
  • 8 6 X

Start state for the 8-puzzle X marks the empty
square.
Can move the 3 tile into the empty square, or,
can move the 6 tile into it.
  • 2 7
  • 1 X
  • 8 6 3
  • 2 7
  • 1 3
  • 8 X 6

Goal of the game is to place all tiles in some
special order.
8
Binary Trees
  • Binary tree every node has at most 2 children
    (left child and right child)
  • N-ary tree no node has more than N children.
  • Advantages?
  • Disadvantages?
  • Equivalencies?

9
Some Properties of Binary Trees
  • Suppose a binary tree has n nodes, and height h.
  • The total numbers of nodes h1 ?n ?2h1-1
  • The height of the tree (from inequality above)
  • log(n1)-1 ? h ? (n-1)

10
Implementation of Binary Trees
11
Binary Trees
node
LL left link RL Right Link
T
(((ab)(c(d-3)))
12
Implementation of N-ary Trees
13
Implementation of Trees
Right link to siblings left link to successors
14
Some applications
  • Expressions ( (731218) (4/3) )
  • HTML or XML
  • Word documents

3
15
Traversing a tree
  • Traversing a tree is to visit every node in the
    tree

16
Binary Tree Traversal
  • Preorder
  • Root
  • Left
  • Right
  • In order
  • Left
  • Root
  • Right
  • Postorder
  • Left
  • Right
  • Root

Preorder (T) if TNIL then
return() Print(T-INFO) Preorder
(T-LL) Preorder (T-RL) Inorder (T) if
TNIL then return() Inorder (T-LL) Print
(T-INFO) Inorder (T-RL) Postorder (T)
if TNIL then return() Postorder
(T-LL) Postorder (T-RL) Print (T-INFO)
17
Preorder Traversal
Preorder (T) if TNIL then
return() Print(T-INFO) Preorder
(T-LL) Preorder (T-RL)
18
Used for copying a tree
  • As in copy constructor for tree class
  • Copy the root
  • Recursively copy left subtree
  • Recursively copy right subtree
  • Always have parent node to link new nodes to

19
Inorder Traversal
Inorder (T) if TNIL then return() Inorder
(T-LL) Print (T-INFO) Inorder (T-RL)
20
Postorder Traversal
( )
postorder
Postorder (T) if TNIL then
return() Postorder (T-LL) Postorder
(T-RL) Print (T-INFO)
21
Used to destruct tree
  • Return leaf nodes to heap via delete in a bottom
    up fashion
  • When children are returned to heap, then return
    the parent to heap
  • More difficult to do in preorder

22
Example of Traversals
Preorder a b c - d 3 prefix
notation Postorder a b c d 3 - Postfix
notation Inorder a b c d - 3 Infix
notation Prefix and postfix notation
unambiguous Infix notation ambiguous.
parenthesis necessary
23
Scene graph from graphics
  • Tree (graph) nodes are visited in preorder (top
    down)
  • Node might set light or place the camera
  • Node might set properties, such as color and
    shine
  • Node might define rotation or translation
  • Node might define object geometry to render

24
Expression Trees
63
  • Postfix
  • 1 2 3 4 5
  • Prefix
  • 1 2 3 4 5
  • Infix
  • 1 2 3 4 5 30
  • ( 1 ( 2 3 ) ) ( 4 5 ) 63


7
9


6
1

4
5
2
3
25
Postfix to Expression Tree
  • Similar to evaluating postfix expressions
  • Evaluating a postfix expression
  • Scan the input one symbol at a time
  • If it is a number
  • push it onto the stack
  • If it is an operator,
  • pop 2 numbers from the stack
  • apply the operator to the 2 numbers
  • push the result back onto the stack

26
Constructing an expression tree
  • 1 2 3 4 5 -
  • 1 2 3 4 5 1
  • 1 2 3 4 5 1 2
  • 1 2 3 4 5 1 2 3
  • 1 2 3 4 5 1 6

27
Binary Tree Reconstruction
  • For a given preorder sequence of binary tree, is
    the inorder sequence unique?
  • Given a preorder sequence of binary tree, we can
    construct a lot of BTs according to the
    corresponding inorder sequence.

2
1
3
2
1
3
28
Example
  • 1, 2, 3
  • Preorder sequence 123
  • Possible inorder sequences are
  • 123, 132, 213, 231, 321

29
Binary search trees
  • Store keys
  • all keys in left subtree keys in right subtree

30
Search Tree
LEFT sorted
list




31
Example
How do we find 8 in this tree?
32
Search Operations
  • Find( tree, key)
  • the pointer to the key (or null pointer)
  • FindMin( tree), FindMax( tree)
  • What is the running time?

33
Find, FindMin, FindMax
  • Find
  • Find Min
  • Find Max

34
How to insert?
  • What if I insert
  • 14
  • 99

Insert(X, var T) if(T nil) T
newnode(X) else if X
Insert(X, T.LL) if X T.item
Insert(X, T.RL)
35
Delete
  • 3 cases
  • What are they?
  • Which are easy?

36
Delete 2 children
2
Deletemin from Right child
37
Deletion
delete(var T,x) if T NIL then return x
does not exists in T else if (x INFO )
then delete (T-LL,x) else if (x T-INFO )
then delete (T-RL,x) else // T-INFO
x if (T - LL NIL) then T T- RL else
if (T - RL NIL) then T T- LL else
// T has left child, // max of left
child becomes the root
y deletemin (T-RL) T- INFO y
Q How to write delete with
deletemin?
38
Deletemin
int deletemin(var T) if T NIL then
return tree is already empty else if
(T - LL NIL) // T has only right child so T
is the min x T - INFO
T T - RL else //
T has a left child, follow left child x
deletemin (T-LL) return
(x) Question How to do deletemax?
39
Complexity
  • search, insertion, deletemax, deletion
  • average case O(log n) ---
  • But, only if random insertion!!!!
  • worst case O(n)

Question How to improve the performance?
40
Balancing Search Tree
  • AVL Tree
  • for every node in the tree, the height of the
    left and right subtree can differ by at most 1.
  • How to keep it balanced?
  • Rotation (single, or double)
  • average depth and worst case depth are O(logn)
  • Splay Tree
  • life time total complexity for m operations is
    O(mlogn), where n is the size of the tree
  • simpler to implement than AVL tree
  • B Tree
  • each node has between 2 and M children
  • Design for small max path
  • good for external storage (disks etc.) data
    structures

41
Testing balanced
  • boolean is_balanced (T, var height)
  • if TNIL then height -1 return true
  • else
  • left_b is_balanced (T-LL,
    left_height)
  • right_b is_balanced (T-RL,
    right_height)
  • height max (left_height,
    right_height) 1
  • if ( left_b and right_b and
    (abs(left_height -
    right_height)
  • then return(true)
  • else return (false)

What is the criteria?
2
42
Other things
int height (T) if TNIL then return
(-1) else return ( max (height(T-LL),
height(T-RL)) 1)
  • int count(T)
  • if TNIL then return (0)
  • else return (count(T-LL) count(T-RL) 1)

43
Good Trees and Bad Trees
h ?(log n)
h ?(n)
Write a Comment
User Comments (0)
About PowerShow.com