public treeNode GenerateTree(int[] data) { treeNode head = null; treeNode nextNode = null; foreach (var item in data) { if (head == null) { head = new treeNode().createTreeNode(item); } else { nextNode = head; if (nextNode.leftchild == null) { nextNode.leftchild = new treeNode().createTreeNode(item); continue; } if (nextNode.rightchild == null) { nextNode.rightchild = new treeNode().createTreeNode(item); continue; } } } return(head); }
public treeNode createTreeNode(int data1) { treeNode temp = new treeNode(); temp.data = data1; return(temp); }
public void inorder(treeNode btree) { if (btree != null) { inorder(btree.leftchild); Console.WriteLine(btree.data); inorder(btree.rightchild); } }
public treeNode GenerateTreeFromArray(int[] data) { treeNode headNode = null; foreach (var item in data) { headNode = insertNode(headNode, item); } return(headNode); }
public treeNode insertNode(treeNode tree, int item) { if (tree == null) { tree = createTreeNode(item); tree.data = item; } else if (item < tree.data) { tree.leftchild = this.insertNode(tree.leftchild, item); } else if (item > tree.data) { tree.rightchild = this.insertNode(tree.rightchild, item); } return(tree); }
//Get constructor to initialize the values during instanition public treeNode() { this.data = 0; this.leftchild = null; this.rightchild = null; }