Binary Trees - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Binary Trees

Description:

A binary tree (Tleft) called the left subtree of T ... All levels of the binary tree, except the last, contain as many ... The height of the tree is log(n 1)-1 ... – PowerPoint PPT presentation

Number of Views:210
Avg rating:3.0/5.0
Slides: 18
Provided by: comp156
Category:
Tags: binary | tree | trees

less

Transcript and Presenter's Notes

Title: Binary Trees


1
Binary Trees
  • A binary tree is a tree with the following
    properties
  • Each internal node has at most two children
  • The children of a node are ordered
  • Left Child
  • Right Child
  • Applications
  • arithmetic expressions
  • decision processes
  • Searching (Binary Search Trees)
  • Heap implementation of a Priority Queue

2
Binary Trees(Recursive Definition)
  • A binary tree is either empty or consists of
  • A node r, called the root of tree T
  • A binary tree (Tleft) called the left subtree of
    T
  • A binary tree (Tright) called the right subtree
    of T

3
Decision Tree
  • Binary tree associated with a decision process
  • internal nodes questions with yes/no answer
  • external nodes decisions

4
Arithmetic Expression Tree
  • Binary tree associated with an arithmetic
    expression
  • external nodes operands
  • Value is that of its variable or constant
  • internal nodes operators
  • Value is defined by applying its operation to the
    value of its children
  • Example arithmetic expression tree for the
    expression
  • (2 ? (a - 1) (3 ? b))

5
Binary Trees
  • A binary tree T of height h is Full if
  • All of the leaves are at level h and every parent
    (internal node) has exactly two children.

Full
Not Full
6
Binary Trees
  • A binary tree T is Proper if
  • All nodes have zero or two children.

Proper
Improper
7
Binary Trees
  • A binary tree T is Complete if
  • All levels of the binary tree, except the last,
    contain as many nodes as possible, and the nodes
    on the last level are filled from left to right.

8
Properties of Full Binary Trees
  • The number of nodes at depth d is 2d
  • The height of the tree is log(n1)-1
  • Number of external nodes equals number of
    internal plus 1 (ne ni 1)
  • Additional properties in section 7.3.3

Depth 0
Depth 1
Depth 2
Depth 3
9
BinaryTree ADT
  • The BinaryTree ADT extends the Tree ADT, i.e., it
    inherits all the methods of the Tree ADT
  • Accessor methods
  • left(v)
  • right(v)
  • Query methods
  • hasLeft(v)
  • hasRight(v)
  • Update
  • addRoot(e)
  • insertLeft(v, e)
  • insertRigth(v, e)
  • attach(v, T1, T2)
  • remove(v)

10
Tree ADT
  • Accessor methods
  • root()
  • parent(v)
  • children(v)
  • Query methods
  • isInternal(v)
  • isExternal(v)
  • isRoot(v)
  • Update method
  • replace (v, e)
  • Genereral methods
  • size()
  • isEmpty()
  • elements()
  • nodes()

11
Inorder Traversal
  • In an inorder traversal a node is visited after
    its left subtree and before its right subtree

Algorithm inOrder(v) if hasLeft (v) inOrder (left
(v)) visit(v) if hasRight (v) inOrder (right (v))
6
2
8
1
7
9
4
3
5
Demo
12
Print Arithmetic Expressions
  • Inorder traversal
  • print operand or operator when visiting node
  • print ( before traversing left subtree
  • print ) after traversing right subtree

Algorithm printExpression(v) if
hasLeft(v) print(() inOrder (left(v)) print(v.e
lement()) if hasRight(v) inOrder(right(v)) print(
))
((2 ? (a - 1)) (3 ? b))
13
Inorder Traversals Compared
Arithmetic Expressions
General
Algorithm inOrder(v) if hasLeft(v) inOrder(left(v)
) visit(v) if hasRight(v) inOrder(right(v))
Algorithm printExpression(v) if hasLeft
(v) print(() printExpression(left(v)) print(v.e
lement()) if hasRight(v) printExpression(right(v))
print())
14
Evaluate Arithmetic Expressions
  • Postorder traversal
  • recursive method returning the value of a subtree
  • when visiting an internal node, combine the
    values of the subtrees

Algorithm evalExpr(v) if isExternal(v) return
v.element() else x ? evalExpr(leftChild(v)) y ?
evalExpr(rightChild(v)) ? ? operator stored at
v return x ? y
15
Linked Structure for Binary Trees
  • Binary Tree Node
  • Element
  • Parent node
  • Left child node
  • Right child node

Parent
Left
Right
Element
16
An Array Structure for Binary Trees
Given node x left(x) 2x right(x)
2x1 parent(x) floor(x/2)
17
An Array Structure for Binary Trees(Example)
1
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15
a b d h
l n o
18
Array vs. Linked Tree Implementation
  • Storage requirements
  • Worst case array of size 2n required to store a
    tree of size n
  • Run time of basic binary tree operations
  • Basically the same, but what about inserting an
    element?
Write a Comment
User Comments (0)
About PowerShow.com