Upload
barnard-george
View
220
Download
1
Tags:
Embed Size (px)
Citation preview
Binary Trees 2
Topics
Trees Binary Trees
Definitions (full, complete, etc.) Expression Trees Traversals Implementation
Binary Trees 3
Tree StructuresP r e side n t - C E O
P r o duc t io nM a n a ge r
Sa le sM a n a ge r
Sh ip p in gSup e r v iso r
P e r so n n e lM a n a ge r
W a r e h o useSup e r v iso r
P ur c h a sin gSup e r v iso r
H IER A R C H IC A L TR EE S TR U C TU R E
Binary Trees 4
What is a Tree
In computer science, a tree is an abstract model of a hierarchical structure
A tree consists of nodes with a parent-child relation
Applications: Organization charts File systems Programming
environments
Computers”R”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
Binary Trees 5
HERE ARE SOME BINARY TREES WE
WILL BE STUDYING IN THE NEXT FEW
LECTURES: BINARY TREE BINARY SEARCH EXPRESSION HEAP TREE TREE AVL RED-BLACK TREE TREE
Binary Trees 7
USING BOTANICAL TERMINOLOGY:
A LEAF IS AN ITEM WHOSE LEFT
AND RIGHT SUBTREES ARE EMPTY.
A BRANCH IS A LINE DRAWN FROM
AN ITEM TO ITS LEFT OR RIGHT
SUBTREE.
Binary Trees 9
+
– /
X Y Z *
A B
THIS IS AN EXPRESSION TREE: EACH
LEAF IS AN OPERAND, AND EACH NON-
LEAF IS A BINARY OPERATOR.
Binary Trees 10
Tree Structures+
e
/
ba
*
dc
-
B IN ARY EXPRES S IO N T REE FO R " a* b + (c -d ) / e "
Binary Trees 12
50
40 70
9140 IS THE LEFT CHILD OF 50
50 IS THE PARENT OF 40
50 IS THE PARENT OF 70
40 AND 70 ARE SIBLINGS
WHAT IS 91 TO 50?
WHAT IS 91 TO 40?
Binary Trees 16
A PATH IN A TREE IS A SEQUENCE OF
ITEMS IN WHICH EACH ITEM EXCEPT
THE LAST IS THE PARENT OF THE
NEXT ITEM IN THE SEQUENCE.
Binary Trees 19
Binary Tree Definition
A binary tree T is a finite set of nodes with one of the following properties: (a) T is a tree if the set of nodes is empty.
(An empty tree is a tree.) (b) The set consists of a root, R, and exactly two
distinct binary trees, the left subtree TL and the right subtreeTR. The nodes in T consist
of node R and all the nodes in TL and TR.
Binary Trees 20
THOSE TWO SUBTREES ARE WRITTEN
AS leftTree(t) AND rightTree(t).
FUNCTIONAL NOTATION IS USED
INSTEAD OF OBJECT NOTATION –
SUCH AS t.leftTree( ).
Binary Trees 21
Binary Tree A binary tree is a tree with the
following properties: Each internal node has two
children The children of a node are an
ordered pair We call the children of an internal
node left child and right child Alternative recursive definition: a
binary tree is either a tree consisting of a single
node, or a tree whose root has an
ordered pair of children, each of which is a binary tree
Applications: arithmetic expressions decision processes searching
A
B C
F GD E
H I
Binary Trees 22
Examples of Binary Trees Expression tree
Non-leaf (internal) nodes contain operators Leaf nodes contain operands
Huffman tree Represents Huffman codes for characters appearing in
a file or stream Huffman code may use different numbers of bits to
encode different characters ASCII or Unicode uses a fixed number of bits for each
character
Binary Trees 23
Examples of Huffman Tree
Code for b = 100000Code for w = 110001
Code for s = 0011Code for e = 010
Binary Trees 24
NOTE THAT THERE IS NO BinaryTree CLASS IN THE STL. IT WOULD NOT BE FLEXIBLE ENOUGH FOR THE BINARY-TREE-BASED CLASSES ALREADY IN THE STANDARD TEMPLATE LIBRARY (set, map, AND priorty_queue).
Binary Trees 25
HOW CAN WE DEFINE
leaves(t),
THE NUMBER OF LEAVES IN THE BINARY TREE t?
RECURSIVELY!
Binary Trees 26
SIMPLEST CASE: WHEN t IS EMPTY
OTHER SIMPLE CASE: WHEN t HAS
ONLY 1 ITEM
OTHERWISE, EXPRESS THE NUMBER
OF LEAVES IN t IN TERMS OF THE
NUMBER OF LEAVES IN leftTree(t) AND
rightTree(t).
Binary Trees 27
if t is empty leaves(t) = 0else if t consists of a root item only leaves(t) = 1else leaves(t) = leaves(leftTree(t)) +
leaves(rightTree(t))
Binary Trees 29
IN A BINARY TREE t, height(t) IS THENUMBER OF BRANCHES FROM THEROOT TO THE FARTHEST LEAF.
50
30 80
62 90
84
DETERMINE height(t)
Binary Trees 33
height(t) = ?
To distinguish the height of an empty tree from the height of a single-item tree, What should the height of an empty tree be?
Binary Trees 34
ONE MORE EXAMPLE: WHAT IS THE
HEIGHT OF THE TREE IF THE
HEIGHT OF THE LEFT SUBTREE IS
4 AND THE HEIGHT OF THE RIGHT
SUBTREE IS 10?
Binary Trees 36
depth(x), THE DEPTH OF AN ITEM x IS THE
NUMBER OF BRANCHES FROM THE ROOT ITEM
TO x. IF x IS THE ROOT ITEM depth(x) = 0 ELSE depth(x) = level(x), THE LEVEL OF x, IS THE SAME
AS THE DEPTH OF x.
Binary Trees 37
Tree Node Level and Path Length
A
HG
FE
DCB
L e ve l: 0
L e ve l: 1
L e ve l: 2
L e ve l: 3
Binary Trees 39
WHAT IS depth(62)? level(90)? THE
HEIGHT OF THE SUBTREE ROOTED
AT 90? 50
30 80
62 90
84
Binary Trees 40
A BINARY TREE t IS A TWO-TREE IF
t IS EMPTY OR IF EACH NON-LEAF
IN t HAS TWO BRANCHES.
Binary Trees 43
RECURSIVELY SPEAKING:
A BINARY TREE t IS A TWO-TREE IF t
IS EMPTY OR IF leftTree(t) AND
rightTree(t) ARE EITHER BOTH EMPTY
OR BOTH NON-EMPTY TWO-TREES.
Binary Trees 44
A BINARY TREE t IS FULL IF t IS A
TWO-TREE AND ALL OF THE
LEAVES OF t HAVE THE SAME
DEPTH.
Binary Trees 46
RECURSIVELY SPEAKING:
A BINARY TREE t IS FULL IF t IS
EMPTY OR IF height(leftTree(t)) =
height(rightTree(t)) AND BOTH
leftTree(t) AND rightTree(t) ARE …?
Binary Trees 47
A BINARY TREE t IS COMPLETE IF t
IS FULL THROUGH A DEPTH OF
height(t) - 1, AND EACH LEAF
WHOSE DEPTH IS height(t) IS AS
FAR TO THE LEFT AS POSSIBLE.
Binary Trees 49
Tree Node Level and Path Length – Depth Discussion
A
ED
CB
GF
C o m p let e T ree (D ep t h 2 )F u ll w it h all p o s s ib le n o d es
Is this tree complete?
Is it full?
Binary Trees 51
Tree Node Level and Path Length – Depth DiscussionA
ED
CB
IH
N o n -C o m p let e T ree (D ep t h 3 )Lev el 2 is m is s in g n o d es
Is this tree complete? Is it full?
What is its height?
Binary Trees 52
Tree Node Level and Path Length – Depth Discussion
A
ED
CB
GF
KIH
N o n -C o m p let eT ree (D ep t h 3 )N o d es at lev el 3 d o n o t o ccu rp y left m o s t p o s it io n s
Is this tree complete? Is it full? What is its height?
Binary Trees 53
WITH EACH ITEM IN A COMPLETE
BINARY TREE, WE ASSOCIATE A NON-
NEGATIVE INTEGER AS FOLLOWS: A 0 B 1 C 2 D 3 E 4 R 5 S 6 F 7 G 8 L 9
Binary Trees 54
A B C D E R S F G L
THIS ASSOCIATION SUGGESTS THAT A COMPLETE BINARY TREE CAN BE STORED IN AN ARRAY:
Binary Trees 55
THEN THE RANDOM-ACCESS
PROPERTY OF ARRAYS ALLOWS
QUICK ACCESS OF PARENT FROM
CHILD AND CHILDREN FROM
PARENT.
Binary Trees 56
PARENT AT 0, CHILDREN AT 1, 2PARENT AT 1, CHILDREN AT 3, 4PARENT AT 2, CHILDREN AT 5, 6…PARENT AT i, CHILDREN AT ?
Children(i) = 2i + 1 and 2i + 2
Binary Trees 57
CHILD AT 1, PARENT AT 0CHILD AT 2, PARENT AT 0CHILD AT 3, PARENT AT 1CHILD AT 4, PARENT AT 1CHILD AT 5, PARENT AT 2CHILD AT 6, PARENT AT 2…CHILD AT i, PARENT AT ?
Parent(i) = (i – 1) / 2
Binary Trees 58
SO IT IS EFFICIENT TO STORE A
COMPLETE BINARY TREE IN AN
ARRAY.
CAN A COMPLETE BINARY BE
STORED IN A vector? YES, SAME IDEA
AS FOR AN ARRAY.
HOW ABOUT A list? NOT GOOD.
Binary Trees 59
A FULL TREE HAS HEIGHT THAT IS LOGARITHMIC IN n. THE HEIGHT OF A COMPLETE
BINARY TREE IS ALSO LOGARITHMIC
IN n. WHAT ABOUT THE HEIGHT OF A
CHAIN?
Binary Trees 60
Selected Samples of Binary Trees A
E
D
C
B
A
F
H
ED
CB
I
T ree ASiz e 9 D ep t h 3
T ree BSiz e 5 D ep t h 4
G
Binary Trees 61
Arithmetic Expression Tree Binary tree associated with an arithmetic
expression internal nodes: operators external nodes: operands
Example: arithmetic expression tree for the expression (2 (a 1) (3 b))
2
a 1
3 b
Binary Trees 62
Decision Tree Binary tree associated with a decision process
internal nodes: questions with yes/no answer external nodes: decisions
Example: dining decision
Want a fast meal?
How about coffee? On expense account?
Starbucks Spike’s Al Forno Café Paragon
Yes No
Yes No Yes No
Binary Trees 64
A TRAVERSAL OF A BINARY TREE IS
AN ALGORITHM THAT ACCESSES
EACH ITEM IN THE BINARY TREE.
Binary Trees 65
1. inOrder(t) {
if (t is not empty) { inOrder(leftTree(t)); access the root item of t; inOrder(rightTree(t)); } // if } // inOrder traversal LEFT – ROOT – RIGHT
Binary Trees 66
50
30 90
12 40 86 100
DETERMINE THE ORDER IN WHICH
THE ITEMS WOULD BE ACCESSED
DURING AN IN-ORDER TRAVERSAL.
ANSWER: 12, 30, 40, 50, 86, 90, 100
Binary Trees 67
Print Arithmetic Expressions Specialization of an inorder
traversal print operand or operator
when visiting node print “(“ before traversing left
subtree print “)“ after traversing right
subtree
Algorithm printExpression(v)if isInternal (v)
print(“(’’)inOrder (leftChild (v))
print(v.element ())if isInternal (v)
inOrder (rightChild (v))print (“)’’)
2
a 1
3 b((2 (a 1)) (3 b))
Binary Trees 68
2. postOrder (t) {
if (t is not empty) {
postOrder(leftTree(t));postOrder(rightTree(t));access the root item of t;
} // if} // postOrder traversal
LEFT – RIGHT – ROOT
Binary Trees 69
Postorder Traversal In a postorder traversal, a node
is visited after its descendants Application: compute space
used by files in a directory and its subdirectories
Algorithm postOrder(v)for each child w of v
postOrder (w)visit(v)
cs16/
homeworks/todo.txt
1Kprograms/
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
9
3
1
7
2 4 5 6
8
Binary Trees 70
DETERMINE THE ORDER IN WHICH
THE ITEMS WOULD BE ACCESSED
DURING A POST-ORDER TRAVERSAL.
HINT: AN OPERATOR IMMEDIATELY
FOLLOWS ITS OPERANDS. +
– /
X Y Z *
A B
Binary Trees 72
Evaluate Arithmetic Expressions Specialization of a
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 vreturn x y
2
5 1
3 2
Binary Trees 73
3. preOrder (t){
if (t is not empty){
access the root item of t;preOrder (leftTree (t));preOrder (rightTree (t));
} // if} // preOrder traversal
ROOT – LEFT – RIGHT
Binary Trees 74
Preorder Traversal A traversal visits the nodes of a
tree in a systematic manner In a preorder traversal, a node
is visited before its descendants
Application: print a structured document
Make Money Fast!
1. Motivations References2. Methods
2.1 StockFraud
2.2 PonziScheme
1.1 Greed 1.2 Avidity2.3 BankRobbery
1
2
3
5
4 6 7 8
9
Algorithm preOrder(v)visit(v)for each child w of v
preorder (w)
Binary Trees 75
DETERMINE THE ORDER IN WHICH
THE ITEMS WOULD BE ACCESSED
DURING A PRE-ORDER TRAVERSAL.
HINT: AN OPERATOR IMMEDIATELY
PRECEDES ITS OPERANDS. +
– /
X Y Z *
A B
Binary Trees 77
Binary Tree Traversals
Preorder: Visit root, traverse left, traverse right Inorder: Traverse left, visit root, traverse right Postorder: Traverse left, traverse right, visit root
Algorithm for Preorder Traversal
Algorithm for Inorder Traversal
Algorithm for Postorder Traversal
1. if the tree is empty 2. Return
else 3. Visit the root 4. Preorder traverse
the left subtree 5. Preorder traverse
the right subtree
1. if the tree is empty 2. Return else 3. Inorder traverse
the left subtree 4. Visit the root 5. Inorder traverse
the right subtree
1. if the tree is empty 2. Return else 3. Postorder traverse
the left subtree 4. Postorder traverse
the right subtree 5. Visit the root.
Binary Trees 78
Visualizing Tree Traversals Can visualize traversal by imagining a mouse that
walks along outside the tree If mouse keeps the tree on its left, it traces a route
called the Euler tour: Preorder: record node first time mouse is there Inorder: record after mouse traverses left subtree Postorder: record node last time mouse is there
Binary Trees 82
Traversals of Expression Trees Inorder traversal can insert parentheses where
they belong for infix form Postorder traversal results in postfix form Prefix traversal results in prefix form
Infix form (x + y) * ((a + b) / c)Postfix form: x y + a b + c / *Prefix form: * + x y / + a b c
Binary Trees 83
4. TO PERFORM A BREADTH-FIRST
TRAVERSAL (LEVEL ORDER TRAVERSAL)
OF A NON-EMPTY BINARY TREE,
FIRST ACCESS THE ROOT ITEM,
THEN THE CHILDREN OF THE ROOT ITEM,
FROM LEFT TO RIGHT, THEN THE GRAND-
CHILDREN OF THE ROOT ITEM, FROM
LEFT TO RIGHT, AND SO ON.
Binary Trees 84
PERFORM A LEVEL ORDER
TRAVERSAL OF THE FOLLOWING
BINARY TREE. A B C D E R S F G L
ANSWER: A, B, C, D, E, R, S, F, G, L
Binary Trees 85
Implementation of Level-Order What data structures would you need?
A
ED
CB
GF
KIH
N o n -C o m p let eT ree (D ep t h 3 )N o d es at lev el 3 d o n o t o ccu rp y left m o s t p o s it io n s
Binary Trees 86
PERFORM THE OTHER TRAVERSALS OF THAT TREE: inOrder (LEFT-ROOT-RIGHT) postOrder (LEFT-RIGHT-ROOT) preOrder (ROOT-LEFT-RIGHT) A B C D E R S F G L
Binary Trees 88
Binary Tree Nodes
Abs tract T re e M ode l
A
E F
H
D
CB
G
l e ft A ri gh t
T re e N ode M ode l
l e ft B ri gh t
l e ft E ri gh t
l e ft G ri gh t
l e ft D ri gh t
l e ft C ri gh t
l e ft H ri gh t
l e ft F ri gh t
Binary Trees 89
The BTNode Class Like linked list, a node has data + links to
successors Data is reference to object of type E Binary tree node has links for left and right
subtrees
Binary Trees 91
General Trees Implementations of Ordered Trees
Multiple links first_child and next_sibling links Correspondence with binary trees
Binary Trees 95
General (Non-Binary) Trees (2) A general tree can be represented using a binary
tree
Left link is to first childRight link is to next younger sibling
Binary Trees 9696
Summary Slide 1§- trees
- hierarchical structures that place elements in nodes along branches that originate from a root.
- Nodes in a tree are subdivided into levels in which the topmost level holds the root node.
§- Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure.
- Tree terminology with which you should be familiar:
parent | child | descendents | leaf node | interior node | subtree.
Binary Trees 97subtree
Tree Terminology
Root: node without parent (A) Internal node: node with at least one
child (A, B, C, F) External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H, D) Ancestors of a node: parent,
grandparent, grand-grandparent, etc. Depth of a node: number of
ancestors Height of a tree: maximum depth of
any node (3) Descendant of a node: child,
grandchild, grand-grandchild, etc.
A
B DC
G HE F
I J K
Subtree: tree consisting of a node and its descendants
Binary Trees 9898
Summary Slide 2§- Binary Trees
- Most effective as a storage structure if it has high density
§- ie: data are located on relatively short paths from the root.
§- A complete binary tree has the highest possible density
- an n-node complete binary tree has depth int(log2n).
- At the other extreme, a degenerate binary tree is equivalent to a linked list and exhibits O(n) access times.
Binary Trees 9999
Summary Slide 3§- Traversing Through a Tree
- There are six simple recursive algorithms for tree traversal.
- The most commonly used ones are:
1) inorder (LNR)
2)postorder (LRN)
3)preorder (NLR).
- Another technique is to move left to right from level to level.§- This algorithm is iterative, and its implementation
involves using a queue.