Title: Tree Structures 3 slides
1Chapter 10 Binary Trees
Tree Structures (3 slides) Tree Node Level and
Path Len. (5 slides) Binary Tree
Definition Selected Samples / Binary Trees Binary
Tree Nodes Binary Search Trees Locating Data in a
Tree Removing a Binary Tree Node stree ADT (4
slides) Using Binary Search Trees - Removing
Duplicates Update Operations (3 slides) Removing
an Item From a Binary Tree (7 slides)
Summary Slides (5 slides)
2Motivation
- Arrays, vectors and lists sequence containers-
access items by position. - Computer Applications store data by value and not
position. - Programs reference data through use of key
associative containers.
3Tree Structures
Tree A non-linear, hierarchical structure in
which an element can have multiple successors,
and all elements originate from a root
4Tree Structures
Binary Tree A special type of tree that allows
an element to have atmost two successors.
5Tree Structures
A-gt root B,C,D -gtAs children
B-gtparent of E,F E,F -gt children of B
Link from parent to child is an edge
A leaf node is node without any children- E,I, J,
G, H
Each node in the tree is a root of its sub-tree,
which consists of all nodes and its sub-tree.
6Tree Node Level and Path Length
A-gtB-gtE-gtH path from A to H Path length 3
Node Level of E 2
7Binary 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.
8Tree Node Level and Path Length Depth Discussion
- A complete binary tree of depth n is a tree in
which - each level from 0 to n-1 has all possible nodes
- All leaf nodes at level n occupy the leftmost
position
9Tree Node Level and Path Length Depth Discussion
10Tree Node Level and Path Length Depth Discussion
11Tree Node Level and Path Length Depth Discussion
12Selected Samples of Binary Trees
13Binary Tree Nodes
14CLASS tnode
- // represents a node in a binary tree
- template lttypename Tgt
- class tnode
-
- public
- // tnode is a class implementation structure.
making the - // data public simplifies building class
functions - T nodeValue
- tnodeltTgt left, right
- // default constructor. data not initialized
- tnode()
-
- // initialize the data members
- tnode (const T item, tnodeltTgt lptr NULL,
- tnodeltTgt rptr NULL)
- nodeValue(item), left(lptr), right(rptr)
-
15Example
tnodeltintgt p, q //p is a leaf node with value
8 p new tnodeltintgt(8) //q is a node with
value 4 and p as a right child q new
tnodeltintgt(4,NULL, p)
16Binary Tree Scan Algorithms
- A binary tree is a recursive structure in which
each node is specified by its value and its left
and right sub-trees. - Three separate actions at each node
- Visiting the node and performing some task N
- Making a recursive descent to Left Sub-tree - L
- Making a recursive descent to Right Sub-tree- R
- A left or right descent into subtrees moves the
scan to the left or right child, which is the
root of the corresponding subtree. - Once we arrive at the root of the subtree, the
scan algorithm sets in place the repetition of
the same three actions. - The descent terminates when we reach an empty
tree(pointerNULL) - The order in which we perform the N,L, and R
actions determines the different recursive scan
algorithms.
17Inorder Scan
- L, N, R or R, N, L
- Traverse the Left sub-tree L
- Visit the node N
- Traverse the Right Subtree R
- After completing all of the operations at a node,
return to the parent node and pick up the
unfinished actions at that node
18inorderOutput()
- template lttypename Tgt
- void inorderOutput(tnodeltTgt t, const string
separator " ") -
- // the recursive scan terminates on a empty
subtree - if (t ! NULL)
-
- inorderOutput(t-gtleft, separator) //
descend left - cout ltlt t-gtnodeValue ltlt separator //
output the node - inorderOutput(t-gtright, separator) //
descend right -
19Inorder Scan
- L, R, N
- Traverse the Left sub-tree L
- Traverse the Right Subtree R
- Visit the node N
- The visit occurs after (post) the scan
completes recursive descents of both the
left-subtree and the right-subtree.
20postorderOutput()
- template lttypename Tgt
- void postorderOutput(tnodeltTgt t, const string
separator " ") -
- // the recursive scan terminates on a empty
subtree - if (t ! NULL)
-
- postorderOutput(t-gtleft, separator) //
descend left - postorderOutput(t-gtright, separator) //
descend right - cout ltlt t-gtnodeValue ltlt separator //
output the node -
21Iterative Level-Order Scan
- Some applications need to access elements by
levels, with root (Level 0) coming first, then
the children of root (Level 1) followed by next
generation (Level 2) and so forth - This type of scan uses queue as an intermediate
storage structure. - Pop a node from the queue
- Perform some action with the node
- Push its children into the queue
22template lttypename Tgt void levelorderOutput(tnodelt
Tgt t, const string separator " ") //
store siblings of each node in a queue so that
they are visited in order at the next level of
the tree queuelttnodeltTgt gt q tnodeltTgt p
// initialize the queue by inserting the root in
the queue q.push(t) // continue the
iterative process until the queue is empty
while(!q.empty()) p q.front() //
delete front node from queue and output the node
value q.pop() cout ltlt p-gtnodeValue
ltlt separator if(p-gtleft ! NULL) // if a
left child exists, insert it in the
queue q.push(p-gtleft) if(p-gtright !
NULL) // if a right child exists, insert next to
its sibling q.push(p-gtright)
23A Binary Search Tree Class
- A Binary Search Tree is an associative container
implemented as stree class. - The Binary Search Tree class supports both
constant and non-constant iterators. - The iterators allow for an inorder traversal of
the elements. - Due to the order of traversal these trees are
called ordered associative containers.
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28Using Binary Search Trees Application Removing
Duplicates
29Update Operations 1st of 3 steps
1)- The function begins at the root node and
compares item 32 with the root value 25. Since
32 gt 25, we traverse the right subtree and
look at node 35.
30Update Operations 2nd of 3 steps
2)- Considering 35 to be the root of its own
subtree, we compare item 32 with 35 and
traverse the left subtree of 35.
31Update Operations 3rd of 3 steps
1)- Create a leaf node with data value 32. Insert
the new node as the left child of node
35. newNode getSTNode(item,NULL,NULL,parent)
parent-gtleft newNode
32Removing an Item From a Binary Tree
33Removing an Item From a Binary Tree
34Removing an Item From a Binary Tree
35Removing an Item From a Binary Tree
36Removing an Item From a Binary Tree
37Removing an Item From a Binary Tree
38Removing an Item From a Binary Tree
39Summary 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.
40Summary 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.
41Summary 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.
42Summary Slide 4
- A binary search tree stores data by value
instead of position - It is an example of an
associative container. - The simple
rules return lt go
left gt go right until finding a
NULL subtree make it easy to build a binary
search tree that does not allow duplicate
values.
43Summary Slide 5
- The insertion algorithm can be used to define
the path to locate a data value in the
tree. - The removal of an item from a binary
search tree is more difficult and involves
finding a replacement node among the remaining
values.
44Access and Update operations
- Access Operation
- find()
- streeltTgtiterator find(const T item)
- Update Operations
- insert()
- void insert(const T item, bool success,
iterator iter) - A
- a