Trees (Binary Trees, 2-3 Trees, AVL Trees)
Trees (Binary Trees, 2-3 Trees, AVL Trees)
Tree
Tree is a non-linear data structure which organizes
data in a hierarchical structure and this is a recursive
definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between
every pair of vertices, then graph is called as a tree.
Tree
Tree Terminology
Root
The first node from where the tree originates is called
as a root node. In any tree, there must be only one root
node. We can never have multiple root nodes in a tree
data structure.
Chapter 8: Trees 40
Binary Tree Traversals
Preorder: Visit root, traverse left, traverse right
Inorder: Traverse left, visit root, traverse right
Postorder: Traverse left, traverse right, visit root
41
Visualizing Tree Traversals
Preorder ?
Preorder: a, b, d, g, e, h, c, f, i, j
Inorder ?
Inorder: d, g, b, h, e, a, i, f, j, c
Postorder ?
Postorder: g, d, h, e, b, i, j, f, c, a
Traversals of Expression Trees
Inorder traversal can insert parentheses where they belong
for infix form
Postorder traversal results in postfix form
Prefix traversal results in prefix form
Postfix form: x y + a b + c / *
Prefix form: * + x y / + a b c
Traversing Trees
Preorder: Root, then Children
+A*B/CD
Postorder: Children, then Root +
ABCD/*+
Inorder: Left child, Root, Right child A *
A+B*C/D
B /
C D
45
Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/ABCDE
* D prefix expression
postorder traversal
AB/C*D*E+
/ C
postfix expression
level order traversal
A B +*E*D/CAB
2-3 Trees
Each internal node has either 2 or 3 children
All leaves are at the same level
2-3 tree is a tree, in which every internal node (non-
leaf) has either one data element and two children or
two data elements and three children.
If a node contains one data element leftVal, it has two
subtrees (children) namely left and middle. Whereas if
a node contains two data elements leftVal and
rightVal, it has three subtrees namely left, middle and
right.
2-3 Trees with Ordered Nodes
2-node 3-node
CSCI 3110 49
Example of 2-3 Tree
CSCI 3110 50
2-3 Tree insertion Algo
In a two-three tree, the algorithm will be as follows:
If the tree is empty, create a node and put value into
the node.
Otherwise find the leaf node where the value belongs.
If the leaf node has only one value, put the new value
into the node.
If the leaf node has more than two values, split the
node and promote the median of the three values to
parent.
If the parent then has three values, continue to split
and promote, forming a new root node if necessary
Inserting Items
Insert 38
CSCI 3110 52
Inserting Items
Insert 38
divide leaf
insert in leaf and move middle result
value up to parent
CSCI 3110 53
Inserting Items
Insert 37
CSCI 3110 54
Inserting Items
Insert 36
divide leaf
insert in leaf and move middle
value up to parent
overcrowded
node
CSCI 3110 55
Inserting Items
... still inserting 36
CSCI 3110 56
Inserting Items
After Insertion of 35, 34, 33
CSCI 3110 57
3 cases of insertion
Insertion: There are 3 possible cases in insertion
which have been discussed below:
Case 1: Insert in a node with only one data element
3 cases of insertion
Case 2: Insert in a node with two data elements whose
parent contains only one data element.
-
Left-Right Rotation
State Action
A node has been inserted into the right subtree of the left
subtree. This makes C an unbalanced node. These
scenarios cause AVL tree to perform left-right rotation.
A node has been inserted into the left subtree of the right
subtree. This makes A, an unbalanced node with balance
factor 2.
14
11 17
7 53
4
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
14
7 17
4 11 53
13
AVL Tree Example:
• Now insert 12
14
7 17
4 11 53
13
12
AVL Tree Example:
• Now insert 12
14
7 17
4 11 53
12
13
AVL Tree Example:
• Now the AVL tree is balanced.
14
7 17
4 12 53
11 13
AVL Tree Example:
• Now insert 8
14
7 17
4 12 53
11 13
8
AVL Tree Example:
• Now insert 8
14
7 17
4 11 53
8 12
13
AVL Tree Example:
• Now the AVL tree is balanced.
14
11 17
7 12 53
4 8 13
AVL Tree Example:
• Now remove 53
14
11 17
7 12 53
4 8 13
AVL Tree Example:
• Now remove 53, unbalanced
14
11 17
7 12
4 8 13
AVL Tree Example:
• Balanced! Remove 11
11
7 14
4 8 12 17
13
AVL Tree Example:
• Remove 11, replace it with the largest in its left branch
7 14
4 12 17
13
AVL Tree Example:
• Remove 8, unbalanced
4 14
12 17
13
Binary Tree Representation
A tree is represented by a pointer to the topmost node
in tree. If the tree is empty, then value of root is NULL.
A Tree node contains following parts.
1. Data
2. Pointer to left child
3. Pointer to right child
Below is example of a tree node with an integer data.
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
Ex01 – in memory trees
class Node {
int key;
Node left, right;
public Node(int item) {
key = item; In memory Tree:
left = right = null;
} 1
} / \
public class BinaryTree { 2 3
// Root of Binary Tree
/ \ / \
Node root;
// Constructors 4 null null null
BinaryTree(int key) { / \
root = new Node(key); null null
}
BinaryTree() {
root = null;
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
}
}
Ex01b – Very Simple BT
class BinarySearchTreeSimple {
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
Node root; // Root of BST
BinarySearchTreeSimple() {// Constructor
root = null;
}
// This method mainly calls insertRec()
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {/* recursive function to insert new key in BST */
if (root == null) {/* If the tree is empty, return a new node */
root = new Node(key);
return root;
}
if (key < root.key) /* Otherwise, recur down the tree */
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
/* return the (unchanged) node pointer */
return root;
}
Ex01b – Very Simple BT
// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
// Driver Program to test above functions
public static void main(String[] args) {
BinarySearchTreeSimple tree = new BinarySearchTreeSimple();
/* Let us create following BST
50
/ \
30 70 output:
/ \ / \
20 40 60 80 */
20
tree.insert(50); 30
tree.insert(30);
tree.insert(20);
40
tree.insert(40); 50
tree.insert(70);
tree.insert(60);
60
tree.insert(80); 70
// print inorder traversal of the BST
tree.inorder();
80
}
}
Lab exercises
Provided Practice code for lab:
BinaryTree.java – simple in memory nodes of trees.
BinarySearchTreeSimple.java – A bit simpler code
BinarySearchTree.java – complete program
BinarySearchTreeInteractive.java – out of scope of this
course as code is complex, but still check it out, run
compile and capture screen shots of operations.
Lab 12 task:
Compile and Run BinarySearchTreeInteractive.java
Capture screen shots of operations and submit it to google forms before 11:55 PM
today.