Chapter 6: Trees
Concepts
Tree: is a set of nodes that is either empty or has a designated node called (the root) from which descend zero or more subtrees.
Root node: the first or top node in the tree.
Child node: a node that descends from another node in the tree.
Parent tree(node): the node that is pointing to its children.
Siblings: the child nodes of a given node.
Branch: a link between a parent and its child node.
Ancestor: a tree node that is hierarchically related to another tree node at a lower level in the tree.
Leaf node: a node that has no children.
Subtree: a subset of a tree that is itself a tree.
Binary tree as ADT
Binary tree: is a tree such that each node can point to at most two children.
Vector implementation of the previous expression: (A-B)+C*(E/F).
Node Data Left Right 1 + 2 5 2 - 3 4 3 A Null Null 4 B Null Null 5 * 6 7 6 C Null Null 7 / 8 9 8 E Null Null 9 F Null Null
Heap Property
Heap property: a binary tree has the heap property when the data of any given node are greater than or
equal to the data in its left and right subtree.
EX:
Binary Search Tree
Is a tree with ordering property, the data in each node of the tree are greater than all of the data in that node's left subtree and less than or equal to all of the data in the right subtree.
EX:
Binary Expression Tree
EX: (A-B)+C*(E/F)
EX: (A-B)+C*E/F
Operations on Binary Trees
1. Create operation:
Function: To create an empty binary tree.
Precondition: The binary tree is an unpredictable state .
Postcondition: The binary tree is initialized to be empty.
2. Empty operation:
Function: To return true if the tree is empty and false otherwise.
Precondition: The binary tree is initialized.
Postcondition: Returns true if the tree is empty, false otherwise.
3. Insertion operation
Function: To add item to the tree.
Preconditions: - The binary tree is initialized.
- There is memory available in the tree for the new item.
- Item is a value of type E to be inserted in the tree.
Postcondition: Item is added to the tree in a way that maintains the tree in hierarchical property.
EX:
16 8 -5 20 30 101 0 10 18
4. Preorder traverse operation
Function: To traverse each node in the tree in preorder way.
Preconditions: - The tree is initialized.
- Process is an algorithmic process that can be applied to each node in the tree.
Postcondition: Each node at the tree is visited in the following order: First visit the root at the tree, then visit recursively all nodes in the left subtree, then visit recursively all nodes in the right subtree. As each node is visited, process is applied to it.
Preorder Traverse
A Root B Left subtree
D C
E
G Right subtree H
F
5. Inorder traverse operation
Function: To traverse each node in the tree in inorder way.
Preconditions: - The tree is initialized
- The process is an algorithmic process that can be applied to each node in the tree.
Postcondition: Each node in the tree is visited in the following order: First visit recursively all nodes in the left subtree of the tree, then visit the root of the tree, then visit recursively all nodes in the right subtree. As each node is visited, process is applied to it.
Inorder Traverse
D Left subtree B
A Root
G E
H Right subtree C
F
6. Postorder traverse operation
Function: To traverse each node in the tree in postorder way.
Preconditions: - The tree is initialized.
- Process is an algorithmic process that can be applied to each node in the tree.
Postcondition: Each node at the tree is visited in the following order: First visit recursively all nodes in the left subtree of the tree, then visit recursively all nodes in the right subtree, then visit the root of the tree.
As each node is visited, process is applied to it.
Postorder Traverse
D Left subtree B
G H
E Right subtree F
C
A Root .
7. Copying a Binary Tree
Function: To copy all of the data elements in the original (parameter) tree to the new (receiver) tree.
Preconditions: - The original tree is initialized.
- The tree is not empty.
Postcondition: A new (receiver) tree is created as a copy of the original tree.
8. Destroying a Binary Tree
Function: To return any memory used for nodes to the system heap.
Preconditions: - The tree is initialized.
- The tree is not empty.
Postcondition: All memory nodes of the tree are returned to the system heap.
Linear implementation of the binary tree ADT
The linear implementation of a binary tree uses vector of size [2(d+1)-1], where "d" is the depth of the tree, that is, the maximum level of any node in the tree.
EX:
d = 3
Size = 2(3+1)-1=15
How to represent the tree in a vector
1. Store the root in the first location of the vector.
2. If a node is in location "n" at the vector, store its left child at location (2n+1), and its right child at location (2n+2).
EX:
Represent the previous tree in a vector.
Notes:-
1. The create operation sets the numberNode data member to zero and initializes the null member of all nodes in the vector location to true.
2. A tree node at location "n" has a left subtree if and only if the node at location (2n+1) contains a null flag that is false.
3. A tree node at location "n" has a right subtree if and only if the node at a location (2n+2) contains a null flag that is false.
4. If a child node is at location "n" in the vector, then its parent node is at location (n-1)/2.
Using the linear implementation for a heap
EX: To insert 40:
General trees
General tree is a tree with an unrestricted numbers of children.
EX: Genealogical Tree:
In a linked representation of a general tree, we have two pointers fields associated with each node: one of the pointers fields is to be viewed as a pointer to the leftmost child of a node in a general tree, the second pointer identifies the next sibling to the right of the node under consideration in the general tree.
EX:
Implementation of the above tree using a vector of records
Location Data First Sibling .0 Jones 1 0
1 Bill 2 7
2 Dave 0 3
3 Mary 4 0
4 Larry 0 5
5 Paul 0 6
6 Penny 0 0
7 Katy 8 9
8 Leo 0 0
9 Mike 10 13
10 Betty 11 12
11 Don 0 0
12 Roger 0 0
13 Jon 0 0
Traversal of a general tree implementation via a binary tree
Preorder traverse: recursively process a parent node and then process the child node from left to right.
EX: Apply the preorder traverse for the previous tree.
Jones Bill Dave Mary Larry Paul Penny Katy Leo Mike Betty Don Roger Tom
Postorder traverse: start from the leaf node at a tree, ensuring that no given node is processed until all nodes in the subtree below is have been processed.
EX: Apply the postorder traverse for the previous tree.
Dave Larry Paul Penny Mary Bill Leo Katy Don Betty Roger Mike Tom Jones