Title: Recursive Definition of Tree Structures not so precise
1Recursive Definition of Tree Structures (not so
precise)
Ch 10 Trees
No rule is violated but we dont like it
- An empty collection is a tree
- A single node is a tree
- A node being linked to a finite number of trees
forms a tree.
23
Is this definition good enough ?
d4
27
d4
d8
d2
d1
d5
d3
d6
d7
2Depict the Tree Structure in a better way
Terminologies come to play
The root
the links are directed
d11
height of d9 4
1
height of d10 1
Ancestors
d10
d9
1
1
2
leaf
d8
Parent
Descendants
2
depth of d82
Children
d5
d4
d3
d6
d7
3
leaf
leaf
leaf
leaf
d2
d1
4
leaf
Siblings
leaf
3More Terminologies
- In-degree the number of links pointing to a node
(always 1 or 0) the root is the only node with
in-degree 0. -
- 2. Out-degree the number of link pointing out
from a node leaves are node with out-degree 0 - 3 . Degree of a tree (arity) the maximum
out-degree of nodes in the tree
23
27
d4
d8
d5
d4
d3
d6
d7
d2
d1
4Recursive Definition of Tree Structures
- Empty is a tree the root is null
- A single node is a tree the node is the root of
the tree. - A node points to a finite number of the roots of
some other trees form a tree the node is the
root of the tree. (so, the node cant point to a
node that is not the root.)
23
d4
27
d4
d8
d2
d1
d5
d3
d6
d7
5Tree Implementation linked lists ?? arrays
data
- data field
- enough fields for pointers pointing the children
Usually, we use the degree of the tree as the
number of the pointer fields. Fixed, if the
degree is small, e.g., binary tree (degree is 2).
6Tree Implementation
Using Array
- Array is a good choice if
- the degree is small
- the tree is rather full
- the size does not change too often
height 3 degree 2
size 2height - 1
d1
0
2
1
d2
d3
What is the advantage and disadvantage of using
array?
5
d4
d5
d6
3
4
12
d7
1st child (left-child) of i is 2i1 2nd child
(right-child) of i is 2i2. The parent of i is
(i-1)/2
height 4
7A full-tree or complete tree is a perfect
example for using array
d1
d1
d2
d3
d2
d3
d4
d5
d6
d7
d7
d4
d5
d6
d8
d10
d9
d8
d9
d10
d11
d12
d13
d14
d15
complete tree
full-tree
8A random binary tree
Randomly insert data into a binary tree
2
13
34
13
24
23
4
17
34
9A Binary Tree Node in Java
- A data field
- Two pointers to the left-child and right-child
public class BinaryTree ltE extends ComparableltEgtgt
/ This is an inner class for tree
nodes/ private static class TNodeltEgt
private E data private TNodeltEgt
left,right private TNode(E data, TNodeltEgt left,
TNodeltEgt right) //Construct a node with two
children this.data data this.left
left this.right right /
This is the end of the inner class NodeltEgt
/ private TNodeltEgt root public
BinaryTree() root null .....
10Add a new data to the BinaryTree
//add data to this BST, return false if data is
already in the tree public boolean add(E data)
if (root ! null) return add(root,
data) root new TNodeltEgt(data, null,
null) return true
A private overloaded add method root is private
11Randomly Insert (add) a data into a binary tree
data
data
data
toss a coin
head
private boolean add(TNodeltEgt t, E data) if
(Math.random() lt 0.5) // toss a coin if (t.left
NULL) t.left new TNodeltEgt(data,null,null)
else add(t.left, data) else if (t.right
NULL) t.right new TNodeltEgt(data,null,null)
else add(t.right,data) return true
12Tree traversal preorder, inorder, postorder
X
L
R
X
X
L
R
L
R
inorder L ? X ? R
preorder X ? L ? R
postorder L ? R ? X
13// Three ways of traversals ..... public void
inorder() inorder(root) public void
preorder() preorder(root) public void
postorder() postorder(root) .....
private void inorder(TNodeltEgt t) if
(tNULL) return inorder(t.left)
System.out.print(t.data", ")
inorder(t.right)
// (1) Travel to the left-child // (2) The way we
visit the node // (3) Travel to the right-child
private void preorder(TNodeltEgt t) if
(tNULL) return System.out.print(t.data",
") preorder(t.left)
preorder(t.right)
// (1) The way we visit the node // (2) Travel to
the left-child // (3) Travel to the right-child
private void postorder(TNodeltEgt t) if
(tNULL) return postorder(t.left)
postorder(t.right) System.out.print(t.data
", ")
// (1) Travel to the left-child // (2) Travel to
the right-child // (3) The way we visit the node
14Calculate the size of the tree, i.e., the number
of nodes
X
Ln 1 Rn
root NULL 0
Ln
Rn
// Return the number nodes in the
tree ..... public int size() return size(root)
private int size(TNodeltEgt t) if (tNULL)
return 0 return 1 size(t.left)
size(t.right)
15Count how many ks in the tree
X
root NULL 0
if x k, Ln 1 Rn otherwise, Ln Rn
Ln
Rn
// Return the number of ks in the
tree ..... public int count(E k) return
count(root, k)
private int count(TNodeltEgt t, E k) if
(tNULL) return 0 return (k.compareTo(t.data
) 0 ? 1 0) count(t.left, k)
count(t.right, k)
16Height of a Tree The number of nodes in the
longest path from the root.
height 5
root
d11
1
// Return the heights of the tree ..... public
int height() return height(root)
private height(TNodeltTgt t) if (tNULL)
return 0 int l height(t.left) int r
height(t.right) return 1 (l gt r ? l
r)
d10
d9
2
3
d8
d5
d4
4
d2
d1
5
17Copy and Clone
d8
d8
root
root
d5
d3
d5
d3
d2
d1
d2
d1