Trees Basics
CSC 3102
B.B. Karki, LSU
Definition and Concepts
A tree consists of a finite set of elements, called nodes, and a finite set of directed
lines, called branches, that connect the nodes.
Degree of node = Number of branches = Sum of indegree and outdegree branches Each node can have an indegree of exactly one but an outdegree of zero, one or more. Only one predecessor but multiple successors.
The level of the node is its distance from the root. The height of a tree is the level
of the leaf in the longest path from the root plus one.
Height (or depth) = 3
Different nodes: Branch AF Parents: A, B, F Children: B, E, F, C, D, G, H, I F E B Branch Siblings: {B, E, F}, {C,D}, {G,H,I} FI Leaves: C, D, E, G, H, I Internal nodes: B, F I H C G D Path from the root to the leaf I is AFI. Subtree: any connected structure below the root BCD, E, FGHI Recursive definition: A tree is a set of nodes that is either empty or has a designated node called the root from which hierarchically descend zero or more subtrees, which are also trees.
CSC 3102
2
Root at level 0
B.B. Karki, LSU
Binary Trees
A
A binary tree is one in which a node
can have zero or one or two subtrees and each subtree is itself a binary tree.
A binary tree node can not have more
than two subtrees. Symmetry is not a tree requirement.
F Right subtree
Left subtree
Height of a binary tree: hmax = N; hmin = log2 N + 1 For a given height h, Nmin = h and Nmax = 2h -1 Balance factor is the difference in height
between the left and right subtrees: B = hL - hR
B = 0 for the above binary tree but B is not zero
Node structure: Node leftSubTree <pointer to Node> data <dataType> rightSubTree <pointer to Node> End Node Class Node left <reference to Node> data <dataType> right <reference Node> End Node B.B. Karki, LSU
for all its eight subtrees. A tree is balanced if its B is zero, -1 or 1 and its subtrees are also balanced.
CSC 3102
3
Binary Tree Traversals
Two general approaches for visiting nodes of a tree: DFT: Depth-First-Traversal Processing proceeds along a path from the root through one of its child to the most distant descendent of that first child before processing a second child. So all of descendents of a child are processed before the next child.
Three types: Preorder (NLR), inorder (LNR) and postorder (LRN) traversal
Uses stack.
BFT: Breadth-First-Traversal Processing proceeds horizontally from the root to all of its children, then of its childrens children, and so forth until all nodes have been processed. So, each level is completely processed before the next level is started. Uses queue.
CSC 3102
B.B. Karki, LSU
Preorder (NLR) Traversal
The root node is processed first, followed by the left subtree and then the
right subtree.
Processing order:A [B[C][D]] [E [ ][F]] ] Six calls are needed: one at level 0, two at level 1 and three at level 2. Calls represented by six triangles.
A
Algorithm preOrder (val root <node pointer>) //Traverse a binary tree in node-left-right sequence //Input: root is the entry node of a tree or subtree //Output: each node has been processed in order If (root is not null) process (root) preOrder (root->leftSubTree) preOrder (root->rightSubTree) return
D Walking order
CSC 3102
B.B. Karki, LSU
BFT Traversal
Given a root at level n, we process all nodes at level n
before processing with the nodes at level n + 1
Processing order:
[A] [B E] [C D F]
Algorithm bft (val root <node pointer>) //Process tree using breadth-first traversal //Input: root is the entry node of a tree or subtree //Output: each node has been processed in order pointer = root while (pointer not null) process (pointer) if (pointer->left not null) enqueue (pointer->left) if (pointer->right not null) enqueue (pointer->right) if (not emptyQueue) C dequeue (pointer) else pointer null Left return
CSC 3102
6
D subtree
F Walking Order
B.B. Karki, LSU
Binary Search Tree
BST serves as an efficient search algorithm
Nearly completed and balanced 6
17
and at the same time serve as efficient insert and delete algorithms.
A BST is a binary tree with the following
19
properties
All items (or key values) in the left subtree are
14
less than the root All items (or key values) in the right subtree are greater than or equal to the root. Each subtree itself is a BST. In BST, the node with the smallest value is
17
the leftmost node in the tree whereas the node with the largest value is the rightmost node. 3
CSC 3102
7
6 Neither complete nor balanced
B.B. Karki, LSU
BST ADT
BST data structure Head structure contains a count, a root pointer and address of the compare function needed to search the list. Data node contains two pointers or references to identify each nodes successors (left and right subtrees), programmer-defined key and data. Algorithms includes basic insertion,
BST count <integer> root <pointer> compare (arg1,arg2) end BST node key data leftSubTree rightSubTree end node
deletion and search operations, and others.
Our view of the BST tree is a pointer or
<keyType> <dataType> <pointer> <pointer>
reference to the head structure, which is allocated from dynamic memory, when the tree is created.
CSC 3102
B.B. Karki, LSU
BST Traversal
Preorder (NLR) traversal gives 23 18 12 20 44 35 52 Postorder (LRN) traversal gives 12 20 18 35 52 44 23 Inorder (LNR) traversal produces an ordered list. 12 18 20 23 35 44 52 23
18
44
12
CSC 3102
9
20
35
52
B.B. Karki, LSU
BST Search
Three searches Find the smallest node Find the largest node Find a requested node. Recursive algorithm to find the smallest node: Simply follow the left branches until we get to a leaf.
Algorithm findSmallestBST (val root <pointer>) //Finds the smallest node in a BST //Input: root is a pointer to a non-empty BST //Output: address of smallest node If (root->left null) return (root) return findSmallestBST (root->left)
CSC 3102
10
B.B. Karki, LSU
BST Insert
All BST inserts take place at a leaf or leaflike node (a node that has one null branch) Iterative insert: Search the tree to locate the insertion point (follow the left or right branch down until we find a null subtree) Insert the new data at a leaf node Recursive insert node: If we are at a null tree or subtree, simply assign the new nodes address to replace the null tree Otherwise, determine which branch we need to follow and call ourself recursively to determine if we are at a leaf yet.
CSC 3102
Algorithm insertBST (root<pointer>, new<pointer) //Input: root is address of first node in a BST // new is address of node containing the data // to be inserted //Output: new node inserted into the tree If (root is null) root = new root->left = null root->right = null else Locate null subtree for insertion if (new->key < root->key) addBST(root->left, new) else addBST(root->right, new) return
11
B.B. Karki, LSU
BST Delete
Search for and locate the node and
delete it.
Node with no children Set the deleted nodes parent to null. Node with one child subtree Attach the right (or left) child subtree to the deleted nodes parent. Node with both subtrees: Replace
Algorithm deleteBST (root<pointer>, dltKey <key>) If (root is null) return false Locate null subtree for insertion if (dltKey < root->key) deleteBST(root->left, dltKey) else if (dltKey > root->key) deleteBST(root->right, dltKey) else Delete node found -- carry out test for the leaf node If not a leaf, find out the largest node on left subtree dltPtr = root->left loop (dltPtr->right not null) dltPtr = dltPtr->right Node found. Move data and delete leaf node root->data = dltPtr->data return deleteBST(root->left, dltPtr->data.key)
the deleted nodes data by moving the data of either
the largest node in the deleted
nodes left subtree or the smallest node in the deleted nodes right subtree.
CSC 3102
12
B.B. Karki, LSU
A Java Class for Binary Tree Nodes
Declaring a class for nodes public class BTNode { private int data private BTNode left private BTNode righ } Declaring head node BTNode head Constructor for the BTNode public BTNode(int initialData, BTNode initialLeft, BTNode initialRight) { data = initialData left = initialLeft right = initialRight }
CSC 3102
13
Accessor and modification
methods for manipulating nodes
Get and set the data and
references to one of the children public Object getData() public void setData(newData)
Add a new node Remove a node Traverse a BT
public void inorderPrint()
Search a node Copy a BT
..
B.B. Karki, LSU