Download presentation
Presentation is loading. Please wait.
Published byOlivia Washington Modified over 6 years ago
1
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
2
Nature Lover’s View Of A Tree
leaves branches root
3
Computer Scientist’s View
root leaves branches nodes Branches meet at nodes.
4
Linear Lists And Trees Linear lists are useful for serially ordered data. (e0, e1, e2, …, en-1) Days of week. Months in a year. Students in this class. Trees are useful for hierarchically ordered data. Employees of a corporation. President, vice presidents, managers, and so on. Family tree
5
Example Tree root President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee children of root grand children of root great grand child of root
6
Hierarchical Data And Trees
The element at the top of the hierarchy is the root. Elements next in the hierarchy are the children of the root. Elements next in the hierarchy are the grandchildren of the root, and so on. Elements that have no children are leaves.
7
Example Tree root President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee children of root grand children of root great grand child of root
8
Definition A tree t is a finite nonempty set of elements.
One of these elements is called the root. The remaining elements, if any, are partitioned into trees, which are called the subtrees of t.
9
Subtrees root President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee
10
Leaves Elements that have no children are leaves. President VP3 VP1
Manager Manager1 Manager2 Manager Worker Bee Elements that have no children are leaves.
11
Parent, Grandparent, Siblings, Ancestors, Descendants
President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee
12
Levels Level 1 President VP1 VP2 VP3 Manager1 Manager2 Manager
Worker Bee Level 2 Level 3 Level 4
13
Caution Some texts start level numbers at 0 rather than at 1.
Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We shall number levels with the root at level 1.
14
height = depth = number of levels
President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee Level 1 The height of the tree is the number of levels in it
15
Node Degree = Number Of Children
President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 1 1
16
Tree Degree = Max Node Degree
President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 Note that it is possible to have a tree whose degree is (say) 3 and whose root has a degree that is < 3. Degree of tree = 3.
17
Binary Tree Finite (possibly empty) collection of elements.
A nonempty binary tree has a root element. The remaining elements (if any) are partitioned into two binary trees. These are called the left and right subtrees of the binary tree.
18
Binary tree
19
Differences Between A Tree & A Binary Tree
No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. A binary tree may be empty; a tree cannot be empty.
20
Differences Between A Tree & A Binary Tree
The subtrees of a binary tree are ordered; those of a tree are not ordered. a b Are different when viewed as binary trees. Are the same when viewed as trees.
21
Binary Tree Properties & Representation
22
Minimum Number Of Nodes
Minimum number of nodes in a binary tree whose height is h. At least one node at each of first h levels. minimum number of nodes is h
23
Maximum Number Of Nodes
All possible nodes at first h levels are present. Maximum number of nodes = … + 2h-1 = 2h - 1
24
Number Of Nodes & Height
Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2h – 1 log2(n+1) <= h <= n
25
Full Binary Tree A full binary tree of a given height h has 2h – 1 nodes. Height 4 full binary tree.
26
Numbering Nodes In A Full Binary Tree
Number the nodes 1 through 2h – 1. Number by levels from top to bottom. Within a level number from left to right. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15
27
Node Number Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Parent of node i is node floor(i / 2), unless i = 1. Node 1 is the root and has no parent.
28
Node Number Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child.
29
Node Number Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child.
30
Complete Binary Tree With n Nodes
Start with a full binary tree that has at least n nodes. Number the nodes as described earlier. The binary tree defined by the nodes numbered 1 through n is the unique n node complete binary tree.
31
Example (Complete binary tree with 10 nodes)
2 3 4 5 6 7 8 9 10 11 12 13 14 15
32
A tree with 3 nodes How many possible binary tree structures are there for a tree with 3 nodes What is the min/max height/level of the tree with 3 nodes Is there a tree structure a complete binary tree for a tree with 3 nodes? Is there a tree structure a full binary tree for a tree with 3 nodes?
33
A tree with height = 2 or 3 What is the min/max nodes of the tree
How many possible binary tree structures are there for a tree with height = 2 or 3
34
Binary Tree Representation
Array representation. Linked representation.
35
Array Representation Number the nodes using the numbering scheme for a full binary tree. The node that is numbered i is stored in tree[i]. b a c d e f g h i j 1 2 3 4 5 6 7 8 9 10 tree[] 5 10 a b c d e f g h i j
36
Right-Skewed Binary Tree
1 3 c 7 d 15 tree[] 5 10 a - b c 15 d An n node binary tree needs an array whose length is between n+1 and 2n.
37
T* tree; // 1D array to hold list elements
int arrayLength; // capacity of the 1D array int treeSize; // number of elements in list int height; // int level; Given treeSize = n; one can determine: maxLength = pow(2,height); maxLength = pow(2,treeSize); arrayLength = 2 * maxLength; Tree = new[arrayLength];
38
methods rightChild(int i) leftChild(int i) parent(int i) getHeight();
getLevel(); getSize();
39
Linked Representation
Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required by an n node binary tree is n * (space required by one node). Notice that a linked representation always take space that is linear in the number of elements in the tree. An array representation may take an exponential amount of space!
40
The Struct binaryTreeNode
template <class T> struct binaryTreeNode { T element; binaryTreeNode<T> *leftChild, *rightChild; binaryTreeNode() {leftChild = rightChild = NULL;} // other constructors come here }; binaryTreeNode has 2 additional constructors. One sets the element field to a user-specified value and the child fields to NULL; the other sets all 3 fields to user-specified values.
41
Linked Representation Example
c b d f e g h leftChild element rightChild root
42
Some Binary Tree Operations
Determine the height. Determine the number of nodes. Make a clone. Determine if two binary trees are clones. Display the binary tree.
43
Binary Tree Traversal Many binary tree operations are done by performing a traversal of the binary tree. In a traversal, each element of the binary tree is visited exactly once. During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.
44
Binary Tree Traversal Methods
Preorder Inorder Postorder Level order
45
Preorder Traversal template <class T>
void preOrder(binaryTreeNode<T> *t) { if (t != NULL) visit(t); preOrder(t->leftChild); preOrder(t->rightChild); } t is the root of the subtree being traversed.
46
Preorder Example (visit = print)
b c a b c
47
Preorder Example (visit = print)
b c d e f g h i j During the traversal, t starts at the root, moves to the left child b of the root, then to the left child d of b. When the traversal of the left subtree of b is complete, t, once again, points to the node b. The t moves into the right subtree of b. When the traversal of this right subtree is complete, t again points to b. Following this, t points to a. We see that t points to every node in the binary tree three times – once when you get to the node from its parent (or in the case of the root, t is initially at the root), once when you return from the left subtree of the node, and once when you return from the node’s right subtree. Of these three times that t points to a node, the node is visited the first time. a b d g h e i c f j
48
Preorder Of Expression Tree
+ a b - c d e f * / / * + a b - c d + e f
50
Inorder Traversal template <class T>
void inOrder(binaryTreeNode<T> *t) { if (t != NULL) inOrder(t->leftChild); visit(t); inOrder(t->rightChild); } t is the root of the subtree being traversed.
51
Inorder Example (visit = print)
b c b a c
52
Inorder Example (visit = print)
b c d e f g h i j g d h b e i a f j c
53
Inorder By Projection a b c d e f g h i j g d h b e i a f j c
54
Inorder Of Expression Tree
+ a b - c d e f * / e a + b * c d / f -
56
Postorder Traversal template <class T>
void postOrder(binaryTreeNode<T> *t) { if (t != NULL) postOrder(t->leftChild); postOrder(t->rightChild); visit(t); } t is the root of the subtree being traversed.
57
Postorder Example (visit = print)
b c b c a
58
Postorder Example (visit = print)
b c d e f g h i j g h d i e b j f c a
59
Postorder Of Expression Tree
+ a b - c d e f * / a b + c d - * e f + /
61
Level Order Let t be the tree root. while (t != NULL) {
visit t and put its children on a FIFO queue; if FIFO queue is empty, set t = NULL; otherwise, pop a node from the FIFO queue and call it t; }
62
Level-Order Example (visit = print)
b c d e f g h i j a b c d e f g h i j
63
exercise List the nodes in pre-, in-, post- and level order
64
Pre-order A,b,d,e,h,I,c,f,g
65
in-order d,b,h,e,I,a,f,c,g
66
post-order D,h,I,e,b,f,g,c,a
67
level-order A,b,c,d,e,f,g,h,i
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.