Permalink
Browse files

implemented insert for binary search tree

  • Loading branch information...
1 parent 2f8cede commit c79cb9c6339bf84fde069eeda88afa63774889d2 @eggie5 committed Apr 24, 2012
View
BIN ...codeproj/project.xcworkspace/xcuserdata/eggie5.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
View
33 BST/BST.xcodeproj/xcuserdata/eggie5.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Bucket
+ type = "1"
+ version = "1.0">
+ <FileBreakpoints>
+ <FileBreakpoint
+ shouldBeEnabled = "Yes"
+ ignoreCount = "0"
+ continueAfterRunningActions = "No"
+ filePath = "BST/main.cpp"
+ timestampString = "356945160.466865"
+ startingColumnNumber = "9223372036854775807"
+ endingColumnNumber = "9223372036854775807"
+ startingLineNumber = "20"
+ endingLineNumber = "20"
+ landmarkName = "main(int argc, const char * argv[])"
+ landmarkType = "7">
+ </FileBreakpoint>
+ <FileBreakpoint
+ shouldBeEnabled = "Yes"
+ ignoreCount = "0"
+ continueAfterRunningActions = "No"
+ filePath = "BST/main.cpp"
+ timestampString = "356945145.106929"
+ startingColumnNumber = "9223372036854775807"
+ endingColumnNumber = "9223372036854775807"
+ startingLineNumber = "21"
+ endingLineNumber = "21"
+ landmarkName = "main(int argc, const char * argv[])"
+ landmarkType = "7">
+ </FileBreakpoint>
+ </FileBreakpoints>
+</Bucket>
View
53 BST/BST/BST.cpp
@@ -6,4 +6,57 @@
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
+
+
#include <iostream>
+#include "BST.h"
+
+
+using namespace std;
+
+
+
+BST::BST()
+{
+ root = 0;
+ current_size = 0;
+}
+
+
+bool BST::add(int value)
+{
+ if (root == 0)
+ root = new TreeNode(value);
+ else
+ insert(value, root, NULL, false);
+
+ current_size++;
+ mod_counter++;
+ return true;
+}
+
+void BST::insert(int value, TreeNode * n, TreeNode *parent, bool was_left)
+{
+ if (n == NULL) { // if you reached the end of a branch (at a leaf) - insert here
+ if (was_left)
+ parent->Lchild = new TreeNode(value);
+ else
+ parent->Rchild = new TreeNode(value);
+ }
+ else if (value < root->item)
+ insert(value, n->Lchild, n, true); //go down tree to the left
+ else
+ insert(value, n->Rchild, n, false); // go down right
+}
+
+void BST::remove()
+{
+
+}
+
+TreeNode BST::search(int value)
+{
+ TreeNode * tn = new TreeNode();
+ return *tn;
+}
+
View
22 BST/BST/BST.h
@@ -5,10 +5,32 @@
// Created by Alex Egg on 4/23/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
+#include "TreeNode.h"
#ifndef BST_BST_h
#define BST_BST_h
+using namespace std;
+
+class BST
+{
+public:
+ BST();
+ bool add(int value);
+ void insert(int value, TreeNode * root, TreeNode * parent, bool was_left);
+ void remove();
+ TreeNode search(int value);
+
+
+private:
+ TreeNode * root;
+
+ int current_size;
+ int mod_counter;
+
+};
+
+
#endif
View
38 BST/BST/TreeNode.h
@@ -6,15 +6,29 @@
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
+
#ifndef BST_TreeNode_h
#define BST_TreeNode_h
-Class TreeNode {
- Friend class BST;
-Public:
- TreeNode(); //default constructor
- TreeNode(int i, TreeNode* L = 0; TreeNode* R = 0); //explicit value constructor
- int getItem () const; // accessor function
+using namespace std;
+
+class TreeNode {
+ friend class BST;
+public:
+ TreeNode()
+ {
+ Lchild = Rchild = NULL;
+ }; //default constructor
+ TreeNode(int i)
+ {
+ item=i;
+ Lchild = Rchild = NULL;
+
+ }
+ int getItem () const
+ {
+ return item;
+ }; // accessor function
private:
int item;
@@ -23,15 +37,11 @@ Class TreeNode {
};
-TreeNode::TreeNode()
-{
- Lchild = Rchild = NULL;
-}
-TreeNode::TreeNode(int i, TreeNode *L = 0, TreeNode *R = 0) : item(i), Lchild(L), Rchild(R)
-{}
-int TreeNode::getItem() const
-{ return item;}
+//TreeNode::TreeNode(int i, TreeNode *L = 0, TreeNode *R = 0) : item(i), Lchild(L), Rchild(R)
+//{}
+
+
#endif
View
9 BST/BST/main.cpp
@@ -7,14 +7,17 @@
//
#include <iostream>
+#include "BST.h"
using namespace std;
int main(int argc, const char * argv[])
{
- // insert code here...
- std::cout << "Hello, World!\n";
- return 0;
+ BST * tree=new BST();
+ tree->add(5);
+ tree->add(1);
+ tree->add(8);
+ cout << "Hi"<<endl;
}

0 comments on commit c79cb9c

Please sign in to comment.