tree_new
TRANSCRIPT
-
7/30/2019 tree_new
1/46
TREES
General trees
Binary trees
Binary search trees AVL trees
Balanced and Threaded trees.
-
7/30/2019 tree_new
2/46
General Trees.Trees can be defined in two ways :
Recursive
Non- recursive A
B C D E
F G H I
K
J
One natural way to define a tree is
recursively
-
7/30/2019 tree_new
3/46
General trees-Definition A tree is a collection of nodes. The
collection can be empty; otherwise atree consists of a distinguish node r,called root, and zero or more non-
empty (sub)trees T1,T2,T3,.TK. .
A
B C D E
F G H I
K
J
Each of whose roots areconnected by a directed edge
from r.
-
7/30/2019 tree_new
4/46
General trees-Definition
A tree consists of set of nodes andset of edges that connected pair ofnodes.
A
B C D E
F G HI
K
J
-
7/30/2019 tree_new
5/46
Eg. A table of contents and
its tree representationBookC1
S1.1
S1.2C2
S2.1
S2.1.1
S2.1.2
S2.2
S2.3
C3
Book
C1 C2 C3
S2.1S1.2S1.1 S2.2 S2.3
S2.1.2S2.1.1
-
7/30/2019 tree_new
6/46
Degree
The number of sub tree
of a node is called its
degree.
Eg. Degree of book
3, C12,C30
Book
C1 C2 C3
S2.1S1.2 S2.2 S2.3
S2.1.2S2.1.1
S1.1
-
7/30/2019 tree_new
7/46
Terminal Nodes and Non
Terminal nodesNodes that have degree 0 is calledLeaf or Terminal node.Other nodescalled non-terminal nodes. Eg.Leaf
nodes :C3,S1.1,S1.2 etc.
Book
C1 C2 C3
S2.1S1.2 S2.2S2.3
S2.1.2S2.1.1
S1.1
-
7/30/2019 tree_new
8/46
Parent, Children & SiblingsBook is said to be the father (parent)of C1,C2,C3 and C1,C2,C3 are said
to be sons (children ) of book.Children of the same parent are said
to be siblings.Eg.C1,C2,C3 are siblings (Brothers)
Book
C1 C2 C3
S2.1S1.2 S2.2 S2.3
S2.1.2S2.1.1
S1.1
-
7/30/2019 tree_new
9/46
Length
Book
C1 C2 C3
S2.1S1.2 S2.2 S2.3
S2.1.2S2.1.1
The length of a path is oneless than the number of nodesin the path.(Eg path from book
to s1.1=3-1=2)
S1.1
-
7/30/2019 tree_new
10/46
Ancestor & Descendent
Book
C1 C2 C3
S2.1S1.2 S2.2 S2.3
S2.1.2S2.1.1
If there is a path from node a to node b, then a is an ancestor of b and b is
descendent of a.In above example, the ancestor of S2.1
are itself,C2 and book, while itdescendent are itself, S2.1.1 and S2.1.2.
S1.1
-
7/30/2019 tree_new
11/46
Height & DepthThe height of a node in a tree is the length of
a longest path from node to leaf.[ In above
example node C1 has height 1, node C2 hasheight 2.etc.
Depth : The depth of a tree is the maximumlevel of any leaf in the tree.[ In above example
depth=3]
Book
C1 C2 C3
S2.1S1.2 S2.2 S2.3
S2.1.2S2.1.1
S1.1
-
7/30/2019 tree_new
12/46
Tree -
Implementation : Keep the children of each node in a
linked list of tree nodes. Thus eachnode keeps two references : one toits leftmost child and other one for itsright sibling.
Left DataRight
-
7/30/2019 tree_new
13/46
Left child -Right sibling
representation of a treeA
B
C
D E
GF
-
7/30/2019 tree_new
14/46
A
B C D
F G
-
7/30/2019 tree_new
15/46
Data structure
definition
Class Treenode
{
Object element;
Treenode leftchild;
Treenode rightsibling;
}
-
7/30/2019 tree_new
16/46
An application :File system
There are many applications for
trees. A popular one is thedirectory structure in manycommon operating systems,including VAX/VMX,Unix and DOS.
-
7/30/2019 tree_new
17/46
Binary trees
A binary tree is a tree in which nonodes can have more than twochildren.
The recursive definition is that abinary tree is either empty orconsists of a root, a left tree, and aright tree.
The left and right trees maythemselves be empty; thus a nodewith one child could have a left orright child. We use the recursive
definition several times in the
-
7/30/2019 tree_new
18/46
One use of the binary tree is inthe expression tree, which iscentral data structure incompiler design.
- d
b c
+
a *
(a+((b-c)*d))
The leaves of an expression tree are
operands, such as constant, variable
names. The other nodes contain
operators.
Eg :
-
7/30/2019 tree_new
19/46
Tree traversal-iterateclasses.
The main tree traversal
techniques are:Pre-order traversal
In-order traversalPost-order traversal
-
7/30/2019 tree_new
20/46
Pre-order traversalTo traverse a non-emptybinary tree in pre-order (alsoknown as depth first order),we perform the followingoperations.Visit the root ( or print the root)Traverse the left in pre-order(Recursive)Traverse the right tree in pre-order
-
7/30/2019 tree_new
21/46
Pre-order traversal
Pre-order list 1,2,3,5,8,9,6,10,4,7
1
2 43
5 6
8 9 10
7
Visit the root ( or print the root)Traverse the left in pre-order (Recursive)Traverse the right tree in pre-order
(Recursive)
-
7/30/2019 tree_new
22/46
In-order traversal
1
243
5 6 7
8 9 10
Pre-order list 2,1,8,5,9,3,10,6,7,4
Traverse the left-subtree in in-order
Visit the rootTraverse the right-subtree in in-
order.
-
7/30/2019 tree_new
23/46
post-order
traversal1
2 43
5 6 7
89 10
Pre-order list 2,8,9,5,10,6,3,7,4,1
Traverse the left sub-tree in post-orderTraverse the right sub-tree in post-order
Visit the root
-
7/30/2019 tree_new
24/46
Properties of binary
trees.If a binary tree contains m nodes atlevel L, then it contains at most 2mnodes at level L+1.
A binary tree can contain at most 2L
nodes at L
At level 0 B-tree can contain at most 1=20 nodes
At level 1 B-tree can contain at most 2=21 nodes
At level 2 B-tree can contain at most 4=
22
-
7/30/2019 tree_new
25/46
Complete B-tree
A complete B-tree of depth d is theB-tree that contains exactly 2Lnodes at each level between 0 and
d ( or 2d
nodes at d)
Complete B-treeNot a Complete B-tree
-
7/30/2019 tree_new
26/46
The total number of nodes (Tn)
in a complete binary tree ofdepth d is 2d+1-1
Tn=20+21+22+2d..(1)
2Tn=21
+22
+ 2d+1
.(2) (2)-(1)
Tn=2d+1-1
-
7/30/2019 tree_new
27/46
Threaded Binary TreesGiven a binary tree with n nodes, the total number of links in the tree is 2n.
Each node (except the root) has exactly one incoming arc only n - 1 links point to nodes remaining n + 1 links are null.
One can use these null links to simplify some traversal processes.
A threaded binary search tree is a BST with unused links employed to
point to other tree nodes. Traversals (as well as other operations, such as backtracking)
made more efficient.
A BST can be threaded with respect to inorder, preorder or postorder
successors.
-
7/30/2019 tree_new
28/46
Threaded Binary TreesGiven the following BST, thread it to facilitate inorder traversal:
The first node visited in an inorder traversal is the leftmost leaf, node A.
Since A has no right child, the next node visited in this traversal is its
parent, node B.
Use the right pointer of node A as a thread to parent B to make backtracking
easy.
H
E
B
K
F J L
A D M
C
IG
-
7/30/2019 tree_new
29/46
Threaded Binary Trees
The thread from A to B is shown as the arrow in above diagram.
H
E
B
K
F J L
A D M
C
IG
-
7/30/2019 tree_new
30/46
Threaded Binary TreesThe next node visited is C, and since its right pointer is null, it also can be
used as a thread to its parent D:
H
E
B
K
F J L
A D M
C
IG
-
7/30/2019 tree_new
31/46
Threaded Binary TreesNode D has a null right pointer which can be replaced with a pointer to Ds
inorder successor, node E:
H
E
B
K
F J L
A D M
C
IG
-
7/30/2019 tree_new
32/46
Threaded Binary TreesThe next node visited with a null right pointer is G. Replace the null pointer
with the address of Gs inorder successor: H
H
E
B
K
F J L
A D M
C
IG
-
7/30/2019 tree_new
33/46
Threaded Binary TreesFinally, we replace:
first, the null right pointer of I with a pointer to its parentand then, likewise, the null right pointer of J with a pointer to its parent
H
E
B
K
F J L
A D M
C
IG
-
7/30/2019 tree_new
34/46
Threaded Tree Example
Amir Kamil 8/8/02 34
8
753
11
13
1
6
9
-
7/30/2019 tree_new
35/46
Threaded Tree Traversal
We start at the leftmost node in thetree, print it, and follow its rightthread
If we follow a thread to the right, weoutput the node and continue to itsright
If we follow a link to the right, we goto the leftmost node, print it, andcontinue
Amir Kamil 8/8/02 35
-
7/30/2019 tree_new
36/46
Threaded Tree Traversal
Amir Kamil 8/8/02 36
8
75
3
11
13
1
6
9Start at leftmost node, print it
Output1
-
7/30/2019 tree_new
37/46
Threaded Tree Traversal
Amir Kamil 8/8/02 37
8
75
3
11
13
1
6
9Follow thread to right, print node
Output1
3
-
7/30/2019 tree_new
38/46
Threaded Tree Traversal
Amir Kamil 8/8/02 38
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
-
7/30/2019 tree_new
39/46
Threaded Tree Traversal
Amir Kamil 8/8/02 39
8
75
3
11
13
1
6
9Follow thread to right, print node
Output1
3
5
6
-
7/30/2019 tree_new
40/46
Threaded Tree Traversal
Amir Kamil 8/8/02 40
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
6
7
-
7/30/2019 tree_new
41/46
Threaded Tree Traversal
Amir Kamil 8/8/02 41
8
75
3
11
13
1
6
9Follow thread to right, print node
Output1
3
5
6
7
8
-
7/30/2019 tree_new
42/46
Threaded Tree Traversal
Amir Kamil 8/8/02 42
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
6
7
8
9
-
7/30/2019 tree_new
43/46
Threaded Tree Traversal
Amir Kamil 8/8/02 43
8
75
3
11
13
1
6
9Follow thread to right, print node
Output1
3
5
6
7
8
9
11
-
7/30/2019 tree_new
44/46
Threaded Tree Traversal
Amir Kamil 8/8/02 44
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
6
7
8
9
11
13
-
7/30/2019 tree_new
45/46
Threaded Tree Modification
Were still wasting pointers, sincehalf of our leafs pointers are still null
We can add threads to the previousnode in an inorder traversal as well,which we can use to traverse thetree backwards or even to do
postorder traversals
Amir Kamil 8/8/02 45
-
7/30/2019 tree_new
46/46
Threaded Tree Modification
8
75
3
11
13
1
6
9