Download presentation
Presentation is loading. Please wait.
Published bySuryadi Darmadi Modified over 6 years ago
1
Trees Construction Paths Height/Depth Ordered Trees Traversals
Tree Operations 4/8/2019 CS 303 – Trees Lecture 7
2
ADT Tree - Terminology Tree The null tree, L, is a tree
A single node, n, is a tree If n is a node and T1,...,Tk are trees then is a tree n = Root T1,...,Tk = subtrees n1,...,nk = children of n n = parent of n1,...,nk n n1 n2 nk 4/8/2019 CS 303 – Trees Lecture 7
3
Paths If n1,n2,...nk is a sequence of nodes in a tree, such that ni is the parent of ni+1, i <= i < k, then n1,n2,...,nk is a path from n1 to nk of length k-1 If there exists a path a,...,b then: a is an ancestor of b b is a descendant of a There is a path of length 0 from a to a. a is both an ancestor and a descendant of a! If a is an ancestor/descendant of b and a <> b, then a is a proper ancestor descendant of b. 4/8/2019 CS 303 – Trees Lecture 7
4
Height/Depth The depth of a node n is the length of the (unique!) path from the root to n. So, Depth(root) = 0. The height of a node n is the length of the longest path from n to a leaf node. [A leaf node is one with no descendants]. a 1 b label each node with height and depth c d 2 e f 3 4 5 4/8/2019 CS 303 – Trees Lecture 7
5
<> = Order Ordered (the default) – children form a list. a a b d
Unordered (if specified) – children form a set a a = c b d b d c 4/8/2019 CS 303 – Trees Lecture 7
6
TTLO For siblings a, b we can define “a is To The Left Of b” (a TTLO b) For non-siblings, a, b: If a’, b’ are siblings, and a’ is an ancestor of a, and b is an ancestor of b, and a’ TTLO b’ then a TTLO b a b c d e f g h i k j work out x TTLO y for various x & y 4/8/2019 CS 303 – Trees Lecture 7
7
Relationships Must have exactly 1 of: a == b
a is a Proper Ancestor of b a is a Proper Descendant of b a TTLO b b TTLO a Exercise: Prove it! 4/8/2019 CS 303 – Trees Lecture 7
8
Traversals Suppose we “walk” the tree and generate a sequence of nodes. For leaves, we want a TTLO b a ...,a,...,b,... What about internal nodes (and root)? Three choices: Pre-order: n, (t1), (t2), ..., (tk) In-order: (t1), n, (t2), ..., (tk) Post-order: (t1), (t2), ..., (tk), n 4/8/2019 CS 303 – Trees Lecture 7
9
Traversal Example Pre-Order (Prefix): *+ab+ac
In-Order (Infix): (a+b)*(a+c) Post-Order(Postfix): ab+ac+* Prefix and Postfix also known as “(reverse) Polish” after Jan Lukasiewicz (a Polish logician) If a TTLO b, which one (pre-, in-, post-order) guarantees that ..., a, ..., b, ...? Prove it! 4/8/2019 CS 303 – Trees Lecture 7
10
Tree Operations (one choice)
p = Parent(T,n) lc = LeftmostChild(T,n) rs = RightSibling(T,n) l = Label(T,n) T = Create(n, T1, ..., Tk) r = Root(T) 4/8/2019 CS 303 – Trees Lecture 7
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.