Title: Binary Trees
1Binary 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
2Binary 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
3Decision Tree
- Binary tree associated with a decision process
- internal nodes questions with yes/no answer
- external nodes decisions
4Arithmetic 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))
5Binary 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
6Binary Trees
- A binary tree T is Proper if
- All nodes have zero or two children.
Proper
Improper
7Binary 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.
8Properties 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
9BinaryTree 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)
10Tree 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()
11Inorder 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
12Print 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))
13Inorder 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())
14Evaluate 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
15Linked Structure for Binary Trees
- Binary Tree Node
- Element
- Parent node
- Left child node
- Right child node
Parent
Left
Right
Element
16An Array Structure for Binary Trees
Given node x left(x) 2x right(x)
2x1 parent(x) floor(x/2)
17An 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
18Array vs. Linked Tree Implementation
- 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?