lecture about tree
TRANSCRIPT
![Page 1: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/1.jpg)
1
FOA
Lecture 10Trees
![Page 2: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/2.jpg)
2
TreesImposes a Hierarchical structure, on a collection of items.
Applications can be: Organization charts. Organization of information in database systems. Representation of syntactic structure of source
programs in compilers.
![Page 3: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/3.jpg)
3
TreesPakistan BOI Organizational chart
![Page 4: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/4.jpg)
4
TreesRoyal Genealogies
Roman Emperors: Julian-Claudian House (27BC-68AD)Roman Emperors: Julian-Claudian House (27BC-68AD) France
CHRONOLOGY The Merovingian Kings (481-752) Pepin and Charlemagne (640-987)
Monarchs of France (987-1883) Capet to Valois (987-1328)
House of Valois (1328-1589) the French Bourbons 1589-1883)
![Page 5: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/5.jpg)
5
Trees
Organization of information in database systems.
CUSTOMERCUSTOMER NUMBERCUSTOMER NAMECUSTOMER CITYCUSTOMER POSTCUSTOMER STCUSTOMER ADDRCUSTOMER PHONECUSTOMER FAX
ORDERORDER NUMBERORDER DATESTATUS
ORDER ITEM BACKORDEREDQUANTITY
ITEMITEM NUMBERQUANTITYDESCRIPTION
ORDER ITEM SHIPPEDQUANTITYSHIP DATE
![Page 6: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/6.jpg)
6
Trees: Basic TerminologyA Tree is a collection of elements called nodes.
One of the node is distinguished as a root, along with a relation (“parenthood”) that places a hierarchical structure on the nodes.
A node can be of any type. e.g.; A Letter, a String, or a Number.
![Page 7: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/7.jpg)
7
TreesA Tree can be defined recursively.
A single node by itself is a tree. This node is also the root of the tree.
Suppose n is a node and T1,T2,…Tk are trees with roots n1,n2,…nk, respectively.
Construct a new tree by making n the parent of nodes n1,n2,…nk.
![Page 8: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/8.jpg)
8
Trees
T1,T2,…Tk the subtrees.
n1,n2,…nk the children of node n.
Null Tree: A Tree with no nodes is called a “Null Tree”.
![Page 9: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/9.jpg)
9
Table of Contents: An Example of Trees
BookC1
s1.1s1.2
C2s2.1
s2.1.1s2.1.2
s2.2s2.3
C3
Book
C1 C2 C3
s1.1 s1.2 s2.1 s2.2 s2.3
s2.1.1 s2.1.2
![Page 10: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/10.jpg)
10
The root node, called “Book”, has three subtrees.
The root of each subtree corresponds to a chapter, C1, C2, C3
The downward line represents: “Book” as the parent of C1, C2, C3 and these are the children of “Book”
Table of Contents: An Example of Trees
![Page 11: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/11.jpg)
11
The subtree, with root C3 is a tree of single node.
The subtree with root C2 has three subtrees, corresponding to the subsections s2.1, s2.2 and s2.3.
Table of Contents: An Example of Trees
![Page 12: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/12.jpg)
12
Trees: Some Definitions Path: If n1,n2,…nk is a sequence of nodes in a tree
such that ni is a parent of ni+1 for i <= I < k, then this sequence is called a Path from n1 node to nk.
Length of Path: One less than the number of nodes in the path.
There is a path of length zero from every node to itself.
![Page 13: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/13.jpg)
13
Zero length pathBook
C1 C2 C3
s1.1 s1.2 s2.1 s2.2 s2.3
s2.1.1 s2.1.2
Trees: Some Definitions
Path from C2 to s2.1.1Path length is 2
![Page 14: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/14.jpg)
14
Trees: Some Definitions If there is a path from node a to node b, then a is an
ancestor of b and b is a descendant of a.
Any node is both an ancestor and descendant of itself.
An ancestor or descendant of a node, other than the node itself, is called a proper ancestor or proper descendant of the node.
![Page 15: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/15.jpg)
15
Book
C1 C2 C3
s1.1 s1.2 s2.1 s2.2 s2.3
s2.1.1 s2.1.2
Trees: Some Definitions
C2 ancestor of s2.1s2.1 ancestor of s2.1.1
![Page 16: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/16.jpg)
16
Trees: Some Definitions The root is the only node with no proper ancestor.
A node with no proper descendant is called a leaf.
A subtree of a node is a node, together with all its descendants.
The height of a node in a tree is the length of a longest path from that node to the leaf.
![Page 17: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/17.jpg)
17
Book
C1 C2 C3
s1.1 s1.2 s2.1 s2.2 s2.3
s2.1.1 s2.1.2
Trees: Some DefinitionsNo proper ancestor
No proper descendent
Sub tree
Height
![Page 18: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/18.jpg)
18
Trees: Some Definitions The height of a tree is the height of the
root.
The depth of a node is the length of the path from the root to that node.
![Page 19: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/19.jpg)
19
Book
C1 C2 C3
s1.1 s1.2 s2.1 s2.2 s2.3
s2.1.1 s2.1.2
Trees: Height of a Tree
![Page 20: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/20.jpg)
20
The Order of NodesThe children of a node are usually ordered from left-to-right.
b c
a
c b
a
Two distinct (ordered) trees
![Page 21: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/21.jpg)
21
The Order of NodesComparison on bases of ordering:
“If a and b are siblings, and a is to the left of b, then all the descendants of a are to the left of all the descendent of b.”
![Page 22: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/22.jpg)
22
The Order of Nodes
1
2 3 4
5 6 7
8 9 10
Node 8 is to the right of node 2.
Node 8 is the left of nodes 9,6,10,4 and 7.
Neither left nor right of its ancestor 1,3 and 5.
![Page 23: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/23.jpg)
23
The Order of NodesThree most important ordering are: Preorder
Postorder
InorderThese ordering are defined recursively.
![Page 24: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/24.jpg)
24
The Order of Nodes If a tree T is null, then the empty list is
the preorder, inorder and postorder listing of T.
If T consists a single node, then that node by itself is the preorder, inorder and postorder listing of T.
![Page 25: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/25.jpg)
25
The Order of NodesLet T be a tree with root n and subtrees
T1,T2,…Tk
n
T1 T2 Tk…
![Page 26: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/26.jpg)
26
The Order of NodesThe preorder listing of the nodes of T is the root n of T followed by the nodes of T1 in preorder, then the nodes of T2 in preorder, and so on, up to the nodes of Tk in preorder.
n
T1 T2 Tk…
![Page 27: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/27.jpg)
27
The Order of NodesThe inorder listing of the nodes of T is the nodes of T1 in inorder, followed by the node n, followed by the nodes of T2,…, Tk, each group of nodes in inorder.
n
T1 T2 Tk…
![Page 28: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/28.jpg)
28
The Order of NodesThe postorder listing of the nodes of T is the nodes of T1 in postorder, then the nodes of T2 in postorder, and so on, up to Tk, all followed by node n.
n
T1 T2 Tk…
![Page 29: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/29.jpg)
29
1
2 3 4
5 6 7
8 9 10
The Order of NodesWalk around the outside of the tree, start at the root, move counterclockwise and stay as close to the tree as is possible.
![Page 30: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/30.jpg)
30
The Order of Nodes For preorder, list the nodes the first time
you pass it. For postorder, list a nodes the last time
you pass it, as you move up to the parent.
For inorder, list a leaf the first time you pass it, but list an interior node the second time you pass it.
![Page 31: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/31.jpg)
31
The Order of Nodes
The ordering of the interior nodes and their relationship to the leaves vary among the three orderings.
![Page 32: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/32.jpg)
32
Labeled and Expressions TreesA label is a value stored at the node.
n4 n5
n2
n6 n7
n3
n1*
+ +
a ab c
A labeled tree representing expression (a + b) * ( a + c)
LabelName ofThe node
![Page 33: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/33.jpg)
33
Labeled and Expressions Trees1. Every leaf is labeled by an operand and
consists of that operand alone.2. Every interior node n is labeled by an
operator. If n represents the binary operator Θ, the left child represent the expression E1 and the right child represent the Expression E2, then n represents (E1) Θ (E2).
![Page 34: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/34.jpg)
34
Labeled and Expressions Trees The Preorder listing of the Expression tree produces
a prefix expression. In a prefix expression the operator precedes its left
and right operand. The prefix operation for a single operand a is a itself. The prefix expression for (E1) Θ (E2), is Θ P1 P2,
where P1 and P2 are the prefix expressions for E1 and E2.
No parentheses are necessary.
![Page 35: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/35.jpg)
35
Labeled and Expressions Trees
n4 n5
n2
n6 n7
n3
n1
+ +
a ab c
The preorder listing of the labels is *+ab+ac *
The prefix expression for n2 is +ab, is the shortest legal prefix of *+ab+ac.
![Page 36: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/36.jpg)
36
Labeled and Expressions Trees The Postorder listing of the Expression tree
produces a postfix (or Polish)expression. In a postfix expression the operator follows
its left and right operand. The postfix expression for (E1) Θ (E2), is P1
P2 Θ, where P1 and P2 are the postfix expressions for E1 and E2.
No parentheses are necessary.
![Page 37: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/37.jpg)
37
Labeled and Expressions Trees
n4 n5
n2
n6 n7
n3
n1
+ +
a ab c
The postorder listing of the labels is ab+ac+* *
The postfix expression for n2 is ab+, is the shortest legal postfix of ab+ac+*.
![Page 38: LECTURE ABOUT TREE](https://reader035.vdocuments.mx/reader035/viewer/2022081604/5875fc261a28ab4a508b4c1f/html5/thumbnails/38.jpg)
38
Labeled and Expressions Trees
n4 n5
n2
n6 n7
n3
n1
+ +
a ab c
The inorder traversal gives the infix expression, but without any parentheses.
a+b*a+c