Title: Pointers and Linked Lists
1Tree Structures
2Trees
- A very important, flexible, and powerful
datastructure that can be constructed using
pointers. - A tree data structure is a hierarchical data
structurethat resembles a live, growing tree
except that itgrows upside down. - A top node, or root node points to zero or
moresubordinate nodes appearing below. - Each of these nodes in turn can point to zeroor
more nodes called subtrees. - A root node is a special importance since it is
the onlynode from which every other node can be
reached. - A tree contains not only pointers to other nodes,
but also data values of interest.
3Trees
- A tree is a two-dimensional structure because you
canmove not only forward and backward in the
data structure but also up and down. - The subnodes pointed to by a particular node are
referred to as its children. - The children of a node are called siblings.
- The node doing the pointing is referred to as the
parent. - A terminal node(that is a node with no children)
is referred to as a leaf. - The link binding a child to its parent is called
a branch.
4Binary Trees
- Binary trees contain two links per node.
- A binary search tree has the characteristic that
the valuein the right child of a node is grater
than or equal to thevalue in its parent node. If
there are no duplicate values,the value in the
right child is simply greater than the value in
its parent node.
5Traversing a Binary Trees
- A preorder traversal processes the value in the
root node,traverses the left subtree preorder,
then traverses theright subtree preorder. The
value in each nodes is processed as the node is
encountered. - An inorder traversal of a binary tree traverses
the leftsubtree inorder, processes the value in
the root node, thentraverses the right subtree
in order. The value in a node is not processed
until the value in its left subtree are
processed. - A postorder traversal traverses the left subtree
postorder, traverses the right subtree
postorder, then processes the value in the root
node. The value in each node is not
processeduntil the values in both its subtrees
are processed.
6class Tree class TreeNode public
TreeNode(int number,TreeNode r, TreeNode l)
TreeNode() int number TreeNode
right TreeNode left
public Tree() Tree() void
InsertNode(int newnumber) void
preordertraversal() const void
inordertraversal() const void
postordertraversal() const void
searchtree() const private void
InsertNodeHelper(TreeNode current, int
newnumber) void preorderhelper(TreeNode
current) const void inorderhelper(TreeNode
current) const void postorderhelper(TreeNod
e current) const bool searchtreehelper(Tree
Node current, int findnumber) const TreeNode
RootPtr
7int main() Tree integers int intval
cout ltlt "Enter 10 integer values\n" for (int
i 0 i lt 10 i) cin gtgt intval
integers.InsertNode(intval) cout ltlt
"Pre Order Traversal" ltlt endl
integers.preordertraversal() cout ltlt endl
cout ltlt "In Order Traversal" ltlt endl
integers.inordertraversal() cout ltlt endl
cout ltlt "Post Order Traversal" ltlt endl
integers.postordertraversal() cout ltlt endl
integers.searchtree() return 0
8void TreeInsertNode(int number)
InsertNodeHelper(RootPtr,number) void
TreeInsertNodeHelper(TreeNode current, int
newnumber) if(current NULL) current
new TreeNode(newnumber,NULL,NULL) //check space
else if(newnumber gt current-gtnumber)
InsertNodeHelper(current-gtright,newnumber)
else if(newnumber lt current-gtnumber)
InsertNodeHelper(current-gtleft,newnumber)
else cout ltlt newnumber ltlt " Is a duplicate
" ltlt endl
9void Treepreordertraversal() const
preorderhelper(RootPtr) void
Treepreorderhelper(TreeNode current) const
if (current !NULL) cout ltlt
current-gtnumber preorderhelper(current-gtleft)
preorderhelper(current-gtright)
10void Treeinordertraversal() const
inorderhelper(RootPtr) void
Treeinorderhelper(TreeNode current) const
if (current !NULL) inorderhelper(curren
t-gtleft) cout ltlt current-gtnumber
inorderhelper(current-gtright)
11void Treepostordertraversal() const
postorderhelper(RootPtr) void
Treepostorderhelper(TreeNode current) const
if (current !NULL) postorderhelper(curr
ent-gtleft) postorderhelper(current-gtright)
cout ltlt current-gtnumber
12void Treesearchtree() const bool found
int searchnumber cout ltlt "Enter the number to
be searched" cin gtgt searchnumber found
searchtreehelper(RootPtr,searchnumber) if
(found) cout ltlt "The number you entered " ltlt
searchnumber ltlt " was found in the list" else
cout ltlt "The number you entered " ltlt
searchnumber ltlt " was not found in the
list" bool Treesearchtreehelper(TreeNode
current, int findnumber) const if (current
NULL) return (false) else if
(current-gtnumber findnumber) return
(true) else if (current-gtnumber gt findnumber)
return(searchtreehelper(current-gtleft,findnumber)
) else if (current-gtnumber lt findnumber)
return(searchtreehelper(current-gtright,findnumber)
) return(false)