예제 #1
0
        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);
        }
예제 #2
0
        public treeNode createTreeNode(int data1)
        {
            treeNode temp = new treeNode();

            temp.data = data1;
            return(temp);
        }
예제 #3
0
 public void inorder(treeNode btree)
 {
     if (btree != null)
     {
         inorder(btree.leftchild);
         Console.WriteLine(btree.data);
         inorder(btree.rightchild);
     }
 }
예제 #4
0
        public treeNode GenerateTreeFromArray(int[] data)
        {
            treeNode headNode = null;


            foreach (var item in data)
            {
                headNode = insertNode(headNode, item);
            }

            return(headNode);
        }
예제 #5
0
        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);
        }
예제 #6
0
 //Get constructor to initialize the values during instanition
 public treeNode()
 {
     this.data       = 0;
     this.leftchild  = null;
     this.rightchild = null;
 }